1
0
Fork 0

🎨 Moved updates channel as new type

This commit is contained in:
Maxim Lebedev 2020-06-04 15:22:33 +05:00
parent 71b21ef0a5
commit eb2e4a8f43
No known key found for this signature in database
GPG Key ID: F8978F46FF0FFA4F
2 changed files with 17 additions and 10 deletions

24
bot.go
View File

@ -20,7 +20,7 @@ import (
type Bot struct {
*User
AccessToken string
Updates chan *Update
Updates UpdatesChannel
client *http.Client
marshler json.API
@ -249,7 +249,7 @@ func (b Bot) NewRedirectURL(param string, group bool) *http.URI {
}
// NewLongPollingChannel creates channel for receive incoming updates using long polling.
func (b *Bot) NewLongPollingChannel(params *GetUpdates) chan *Update {
func (b *Bot) NewLongPollingChannel(params *GetUpdates) UpdatesChannel {
if params == nil {
params = &GetUpdates{
Offset: 0,
@ -258,7 +258,7 @@ func (b *Bot) NewLongPollingChannel(params *GetUpdates) chan *Update {
}
}
b.Updates = make(chan *Update, params.Limit)
b.Updates = make(UpdatesChannel, params.Limit)
go func() {
for {
@ -284,16 +284,20 @@ func (b *Bot) NewLongPollingChannel(params *GetUpdates) chan *Update {
return b.Updates
}
// NewWebhookChannel creates channel for receive incoming updates via an outgoing webhook.
// NewWebhookChannel creates channel for receive incoming updates via an outgoing webhook. Returns updates channel and
// shutdown func.
//
// If cert argument is provided by two strings (["path/to/cert.file", "path/to/cert.key"]), then TLS server will be created by this filepaths.
func (b *Bot) NewWebhookChannel(u *http.URI, p SetWebhook, ln net.Listener, crt ...string) (chan *Update, func() error) {
b.Updates = make(chan *Update, 100)
// If cert argument is provided by two strings (["path/to/cert.file", "path/to/cert.key"]), then TLS server will be
// created by this filepaths.
func (b *Bot) NewWebhookChannel(u *http.URI, p SetWebhook, ln net.Listener, crt ...string) (UpdatesChannel,
func() error) {
b.Updates = make(UpdatesChannel, 100) // NOTE(toby3d): channel size by default GetUpdates.Limit parameter
handleFunc := func(ctx *http.RequestCtx) {
dlog.Ln("Request path:", string(ctx.Path()))
if !bytes.HasPrefix(ctx.Path(), u.Path()) {
dlog.Ln("Unsupported request path:", string(ctx.Path()))
return
}
@ -314,9 +318,9 @@ func (b *Bot) NewWebhookChannel(u *http.URI, p SetWebhook, ln net.Listener, crt
ReduceMemoryUsage: true,
}
var err error
go func() {
var err error
switch {
case len(crt) == 2:
dlog.Ln("Creating TLS router...")
@ -331,7 +335,7 @@ func (b *Bot) NewWebhookChannel(u *http.URI, p SetWebhook, ln net.Listener, crt
}
}()
if _, err = b.SetWebhook(p); err != nil {
if _, err := b.SetWebhook(p); err != nil {
log.Fatalln(err.Error())
}

View File

@ -108,6 +108,9 @@ type (
// Please note that this parameter doesn't affect updates created before the call to the setWebhook, so unwanted updates may be received for a short period of time.
AllowedUpdates []string `json:"allowed_updates,omitempty"`
}
// UpdatesChannel represents channel for incoming updates.
UpdatesChannel chan<- *Update
)
// GetUpdates receive incoming updates using long polling. An Array of Update objects is returned.