1
0
telegram/utils_update.go

171 lines
4.3 KiB
Go
Raw Normal View History

package telegram
import (
"bytes"
"log"
"time"
"github.com/kirillDanshin/dlog"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
2018-04-19 13:02:15 +00:00
// UpdatesChannel is a channel for reading updates of bot.
type UpdatesChannel <-chan Update
2018-04-19 13:02:15 +00:00
// NewLongPollingChannel creates channel for receive incoming updates using long
// polling.
2018-08-15 11:05:58 +00:00
func (b *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 {
2018-08-15 11:05:58 +00:00
updates, err := b.GetUpdates(params)
if err != nil {
dlog.Ln(err.Error())
dlog.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
}
2018-04-19 13:02:15 +00:00
// NewWebhookChannel creates channel for receive incoming updates via an outgoing
// webhook.
func (b *Bot) NewWebhookChannel(setURL *http.URI, params *SetWebhookParameters, certFile, keyFile, serveAddr string) (updates UpdatesChannel) {
if params == nil {
params = &SetWebhookParameters{
URL: setURL.String(),
MaxConnections: 40,
}
}
var err error
channel := make(chan Update, 100)
handleFunc := func(ctx *http.RequestCtx) {
dlog.Ln("Request path:", string(ctx.Path()))
if !bytes.HasPrefix(ctx.Path(), setURL.Path()) {
dlog.Ln("Unsupported request path:", string(ctx.Path()))
return
}
dlog.Ln("Catched supported request path:", string(ctx.Path()))
var update Update
if err = json.Unmarshal(ctx.Request.Body(), &update); err != nil {
return
}
channel <- update
}
go func() {
if certFile != "" && keyFile != "" {
dlog.Ln("Creating TLS router...")
err = http.ListenAndServeTLS(serveAddr, certFile, keyFile, handleFunc)
} else {
dlog.Ln("Creating simple router...")
err = http.ListenAndServe(serveAddr, handleFunc)
}
if err != nil {
log.Fatalln(err.Error())
}
}()
2018-08-15 11:05:58 +00:00
if _, err = b.SetWebhook(params); err != nil {
log.Fatalln(err.Error())
}
return channel
}
2018-04-19 13:02:15 +00:00
// IsMessage checks that the current update is a message creation event.
2018-08-15 11:05:58 +00:00
func (u *Update) IsMessage() bool {
return u != nil && u.Message != nil
}
2018-04-19 13:02:15 +00:00
// IsEditedMessage checks that the current update is a editing message event.
2018-08-15 11:05:58 +00:00
func (u *Update) IsEditedMessage() bool {
return u != nil && u.EditedMessage != nil
}
2018-04-19 13:02:15 +00:00
// IsChannelPost checks that the current update is a post channel creation event.
2018-08-15 11:05:58 +00:00
func (u *Update) IsChannelPost() bool {
return u != nil && u.ChannelPost != nil
}
2018-04-19 13:02:15 +00:00
// IsEditedChannelPost checks that the current update is a editing post channel
// event.
2018-08-15 11:05:58 +00:00
func (u *Update) IsEditedChannelPost() bool {
return u != nil && u.EditedChannelPost != nil
}
2018-04-19 13:02:15 +00:00
// IsInlineQuery checks that the current update is a inline query update.
2018-08-15 11:05:58 +00:00
func (u *Update) IsInlineQuery() bool {
return u != nil && u.InlineQuery != nil
}
2018-04-19 13:02:15 +00:00
// IsChosenInlineResult checks that the current update is a chosen inline result
// update.
2018-08-15 11:05:58 +00:00
func (u *Update) IsChosenInlineResult() bool {
return u != nil && u.ChosenInlineResult != nil
}
2018-04-19 13:02:15 +00:00
// IsCallbackQuery checks that the current update is a callback query update.
2018-08-15 11:05:58 +00:00
func (u *Update) IsCallbackQuery() bool {
return u != nil && u.CallbackQuery != nil
}
2018-04-19 13:02:15 +00:00
// IsShippingQuery checks that the current update is a shipping query update.
2018-08-15 11:05:58 +00:00
func (u *Update) IsShippingQuery() bool {
return u != nil && u.ShippingQuery != nil
}
2018-04-19 13:02:15 +00:00
// IsPreCheckoutQuery checks that the current update is a pre checkout query
// update.
2018-08-15 11:05:58 +00:00
func (u *Update) IsPreCheckoutQuery() bool {
return u != nil && u.PreCheckoutQuery != nil
}
// Type return update type for current update.
2018-08-15 11:05:58 +00:00
func (u *Update) Type() string {
switch {
2018-08-15 11:05:58 +00:00
case u.IsCallbackQuery():
return UpdateCallbackQuery
2018-08-15 11:05:58 +00:00
case u.IsChannelPost():
return UpdateChannelPost
2018-08-15 11:05:58 +00:00
case u.IsChosenInlineResult():
return UpdateChosenInlineResult
2018-08-15 11:05:58 +00:00
case u.IsEditedChannelPost():
return UpdateEditedChannelPost
2018-08-15 11:05:58 +00:00
case u.IsEditedMessage():
return UpdateEditedMessage
2018-08-15 11:05:58 +00:00
case u.IsInlineQuery():
return UpdateInlineQuery
2018-08-15 11:05:58 +00:00
case u.IsMessage():
return UpdateMessage
2018-08-15 11:05:58 +00:00
case u.IsPreCheckoutQuery():
return UpdatePreCheckoutQuery
2018-08-15 11:05:58 +00:00
case u.IsShippingQuery():
return UpdateShippingQuery
default:
return ""
}
}