1
0
telegram/helpers_updates.go
2017-11-23 13:59:25 +05:00

106 lines
1.9 KiB
Go

package telegram
import (
"strings"
"time"
// "net/url"
log "github.com/kirillDanshin/dlog"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
type UpdatesChannel <-chan Update
func (bot *Bot) NewLongPollingChannel(params *GetUpdatesParameters) UpdatesChannel {
if params == nil {
params = &GetUpdatesParameters{
Offset: 0,
Limit: 100,
Timeout: 60,
}
}
channel := make(chan Update, params.Limit)
go func() {
for {
updates, err := bot.GetUpdates(params)
if err != nil {
log.Ln(err.Error())
log.Ln("Failed to get updates, retrying in 3 seconds...")
time.Sleep(time.Second * 3)
continue
}
for _, update := range updates {
if update.ID >= params.Offset {
params.Offset = update.ID + 1
channel <- update
}
}
}
}()
return channel
}
func (bot *Bot) NewWebhookChannel(
params *SetWebhookParameters,
certFile, keyFile, set, listen, serve string,
) (updates UpdatesChannel) {
if params == nil {
params = &SetWebhookParameters{
URL: set,
MaxConnections: 40,
}
}
if _, err := bot.SetWebhook(params); err != nil {
panic(err.Error())
}
channel := make(chan Update, params.MaxConnections)
go func() {
var err error
if params.Certificate != nil {
err = http.ListenAndServeTLS(
serve,
certFile,
keyFile,
func(ctx *http.RequestCtx) {
if strings.HasPrefix(string(ctx.Path()), listen) {
var update Update
err = json.Unmarshal(ctx.Request.Body(), &update)
if err != nil {
return
}
channel <- update
}
},
)
} else {
err = http.ListenAndServe(
serve,
func(ctx *http.RequestCtx) {
if strings.HasPrefix(string(ctx.Path()), listen) {
var update Update
err = json.Unmarshal(ctx.Request.Body(), &update)
if err != nil {
return
}
channel <- update
}
},
)
}
if err != nil {
panic(err.Error())
}
}()
return channel
}