Skip to content

Commit

Permalink
docs(tgb): bot, handler, middleware, update
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-linch committed Jun 28, 2022
1 parent 7bd14e9 commit d6333ae
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tgb/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type registeredHandler struct {
Filter Filter
}

// Bot is a router for incoming Updates.
// tg.Update should be wrapped into tgb.Update with binded Client and Update.
type Bot struct {
chain chain
messageHandler []*registeredHandler
Expand All @@ -34,6 +36,7 @@ type Bot struct {
errorHandler ErrorHandler
}

// New creates new Bot.
func New() *Bot {
return &Bot{
chain: chain{},
Expand All @@ -49,11 +52,14 @@ func compactFilter(filters ...Filter) Filter {
return nil
}

// Use add middleware to chain handlers.
// Should be called before any other register handler.
func (bot *Bot) Use(mws ...Middleware) *Bot {
bot.chain = bot.chain.Append(mws...)
return bot
}

// Message register handlers for Update with not empty Message field.
func (bot *Bot) Message(handler MessageHandler, filters ...Filter) *Bot {
bot.messageHandler = append(bot.messageHandler, &registeredHandler{
Handler: bot.chain.Then(handler),
Expand All @@ -62,6 +68,7 @@ func (bot *Bot) Message(handler MessageHandler, filters ...Filter) *Bot {
return bot
}

// EditedMessage register handlers for Update with not empty EditedMessage field.
func (bot *Bot) EditedMessage(handler MessageHandler, filters ...Filter) *Bot {
bot.editedMessageHandler = append(bot.editedMessageHandler, &registeredHandler{
Handler: bot.chain.Then(handler),
Expand All @@ -70,6 +77,7 @@ func (bot *Bot) EditedMessage(handler MessageHandler, filters ...Filter) *Bot {
return bot
}

// ChannelPost register handlers for Update with not empty ChannelPost field.
func (bot *Bot) ChannelPost(handler MessageHandler, filters ...Filter) *Bot {
bot.channelPostHandler = append(bot.channelPostHandler, &registeredHandler{
Handler: bot.chain.Then(handler),
Expand All @@ -78,6 +86,7 @@ func (bot *Bot) ChannelPost(handler MessageHandler, filters ...Filter) *Bot {
return bot
}

// EditedChannelPost register handlers for Update with not empty EditedChannelPost field.
func (bot *Bot) EditedChannelPost(handler MessageHandler, filters ...Filter) *Bot {
bot.editedChannelPostHandler = append(bot.editedChannelPostHandler, &registeredHandler{
Handler: bot.chain.Then(handler),
Expand All @@ -86,6 +95,7 @@ func (bot *Bot) EditedChannelPost(handler MessageHandler, filters ...Filter) *Bo
return bot
}

// InlineQuery register handlers for Update with not empty InlineQuery field.
func (bot *Bot) InlineQuery(handler InlineQueryHandler, filters ...Filter) *Bot {
bot.inlineQueryHandler = append(bot.inlineQueryHandler, &registeredHandler{
Handler: bot.chain.Then(handler),
Expand All @@ -94,6 +104,7 @@ func (bot *Bot) InlineQuery(handler InlineQueryHandler, filters ...Filter) *Bot
return bot
}

// ChosenInlineResult register handlers for Update with not empty ChosenInlineResult field.
func (bot *Bot) ChosenInlineResult(handler ChosenInlineResultHandler, filters ...Filter) *Bot {
bot.chosenInlineResultHandler = append(bot.chosenInlineResultHandler, &registeredHandler{
Handler: bot.chain.Then(handler),
Expand All @@ -102,6 +113,7 @@ func (bot *Bot) ChosenInlineResult(handler ChosenInlineResultHandler, filters ..
return bot
}

// CallbackQuery register handlers for Update with not empty CallbackQuery field.
func (bot *Bot) CallbackQuery(handler CallbackQueryHandler, filters ...Filter) *Bot {
bot.callbackQueryHandler = append(bot.callbackQueryHandler, &registeredHandler{
Handler: bot.chain.Then(handler),
Expand All @@ -110,6 +122,7 @@ func (bot *Bot) CallbackQuery(handler CallbackQueryHandler, filters ...Filter) *
return bot
}

// ShippingQuery register handlers for Update with not empty ShippingQuery field.
func (bot *Bot) ShippingQuery(handler ShippingQueryHandler, filters ...Filter) *Bot {
bot.shippingQueryHandler = append(bot.shippingQueryHandler, &registeredHandler{
Handler: bot.chain.Then(handler),
Expand All @@ -118,6 +131,7 @@ func (bot *Bot) ShippingQuery(handler ShippingQueryHandler, filters ...Filter) *
return bot
}

// PreCheckoutQuery register handlers for Update with not empty PreCheckoutQuery field.
func (bot *Bot) PreCheckoutQuery(handler PreCheckoutQueryHandler, filters ...Filter) *Bot {
bot.preCheckoutQueryHandler = append(bot.preCheckoutQueryHandler, &registeredHandler{
Handler: bot.chain.Then(handler),
Expand All @@ -126,6 +140,7 @@ func (bot *Bot) PreCheckoutQuery(handler PreCheckoutQueryHandler, filters ...Fil
return bot
}

// Poll register handlers for Update with not empty Poll field.
func (bot *Bot) Poll(handler PollHandler, filters ...Filter) *Bot {
bot.pollHandler = append(bot.pollHandler, &registeredHandler{
Handler: bot.chain.Then(handler),
Expand All @@ -134,6 +149,7 @@ func (bot *Bot) Poll(handler PollHandler, filters ...Filter) *Bot {
return bot
}

// PollAnswer register handlers for Update with not empty PollAnswer field.
func (bot *Bot) PollAnswer(handler PollAnswerHandler, filters ...Filter) *Bot {
bot.pollAnswerHandler = append(bot.pollAnswerHandler, &registeredHandler{
Handler: bot.chain.Then(handler),
Expand All @@ -142,6 +158,7 @@ func (bot *Bot) PollAnswer(handler PollAnswerHandler, filters ...Filter) *Bot {
return bot
}

// MyChatMember register handlers for Update with not empty MyChatMember field.
func (bot *Bot) MyChatMember(handler ChatMemberUpdatedHandler, filters ...Filter) *Bot {
bot.myChatMemberHandler = append(bot.myChatMemberHandler, &registeredHandler{
Handler: bot.chain.Then(handler),
Expand All @@ -150,6 +167,7 @@ func (bot *Bot) MyChatMember(handler ChatMemberUpdatedHandler, filters ...Filter
return bot
}

// ChatMember register handlers for Update with not empty ChatMember field.
func (bot *Bot) ChatMember(handler ChatMemberUpdatedHandler, filters ...Filter) *Bot {
bot.chatMemberHandler = append(bot.chatMemberHandler, &registeredHandler{
Handler: bot.chain.Then(handler),
Expand All @@ -158,6 +176,7 @@ func (bot *Bot) ChatMember(handler ChatMemberUpdatedHandler, filters ...Filter)
return bot
}

// ChatJoinRequest register handlers for Update with not empty ChatJoinRequest field.
func (bot *Bot) ChatJoinRequest(handler ChatJoinRequestHandler, filters ...Filter) *Bot {
bot.chatJoinRequestHandler = append(bot.chatJoinRequestHandler, &registeredHandler{
Handler: bot.chain.Then(handler),
Expand Down Expand Up @@ -204,6 +223,7 @@ func (bot *Bot) pickAndHandle(ctx context.Context, update *Update, group []*regi
return nil
}

// Handle handles an Update.
func (bot *Bot) Handle(ctx context.Context, update *Update) error {
group := append([]*registeredHandler{}, bot.updateHandler...)

Expand Down
11 changes: 11 additions & 0 deletions tgb/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func (handler MessageHandler) Handle(ctx context.Context, update *Update) error
return nil
}

// InlineQueryHandler it's typed handler for InlineQuery.
// Impliment Handler interface.
type InlineQueryHandler func(context.Context, *InlineQueryUpdate) error

func (handler InlineQueryHandler) Handle(ctx context.Context, update *Update) error {
Expand All @@ -48,6 +50,8 @@ func (handler InlineQueryHandler) Handle(ctx context.Context, update *Update) er
})
}

// ChosenInlineResultHandler it's typed handler for ChosenInlineResult.
// Impliment Handler interface.
type ChosenInlineResultHandler func(context.Context, *ChosenInlineResultUpdate) error

func (handler ChosenInlineResultHandler) Handle(ctx context.Context, update *Update) error {
Expand All @@ -58,6 +62,7 @@ func (handler ChosenInlineResultHandler) Handle(ctx context.Context, update *Upd
})
}

// CallbackQueryHandler it's typed handler for CallbackQuery.
type CallbackQueryHandler func(context.Context, *CallbackQueryUpdate) error

func (handler CallbackQueryHandler) Handle(ctx context.Context, update *Update) error {
Expand All @@ -68,6 +73,7 @@ func (handler CallbackQueryHandler) Handle(ctx context.Context, update *Update)
})
}

// ShippingQueryHandler it's typed handler for ShippingQuery.
type ShippingQueryHandler func(context.Context, *ShippingQueryUpdate) error

func (handler ShippingQueryHandler) Handle(ctx context.Context, update *Update) error {
Expand All @@ -78,6 +84,7 @@ func (handler ShippingQueryHandler) Handle(ctx context.Context, update *Update)
})
}

// PreCheckoutQueryHandler it's typed handler for PreCheckoutQuery.
type PreCheckoutQueryHandler func(context.Context, *PreCheckoutQueryUpdate) error

func (handler PreCheckoutQueryHandler) Handle(ctx context.Context, update *Update) error {
Expand All @@ -88,6 +95,7 @@ func (handler PreCheckoutQueryHandler) Handle(ctx context.Context, update *Updat
})
}

// PollHandler it's typed handler for Poll.
type PollHandler func(context.Context, *PollUpdate) error

func (handler PollHandler) Handle(ctx context.Context, update *Update) error {
Expand All @@ -98,6 +106,7 @@ func (handler PollHandler) Handle(ctx context.Context, update *Update) error {
})
}

// PollAnswerHandler it's typed handler for PollAnswer.
type PollAnswerHandler func(context.Context, *PollAnswerUpdate) error

func (handler PollAnswerHandler) Handle(ctx context.Context, update *Update) error {
Expand All @@ -108,6 +117,7 @@ func (handler PollAnswerHandler) Handle(ctx context.Context, update *Update) err
})
}

// UpdateHandler it's typed handler for ChatMemberUpdate subtype.
type ChatMemberUpdatedHandler func(context.Context, *ChatMemberUpdatedUpdate) error

func (handler ChatMemberUpdatedHandler) Handle(ctx context.Context, update *Update) error {
Expand All @@ -125,6 +135,7 @@ func (handler ChatMemberUpdatedHandler) Handle(ctx context.Context, update *Upda
return nil
}

// ChatJoinRequestHandler it's typed handler for ChatJoinRequest.
type ChatJoinRequestHandler func(context.Context, *ChatJoinRequestUpdate) error

func (handler ChatJoinRequestHandler) Handle(ctx context.Context, update *Update) error {
Expand Down
1 change: 1 addition & 0 deletions tgb/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func (c chain) Append(mws ...Middleware) chain {
return result
}

// Then wraps handler with middleware chain.
func (c chain) Then(handler Handler) Handler {
for i := range c {
handler = c[len(c)-1-i](handler)
Expand Down
25 changes: 25 additions & 0 deletions tgb/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/mr-linch/go-tg"
)

// Update wraps around a tg.Update.
// Also contains Client which is used to send responses.
type Update struct {
*tg.Update
Client *tg.Client
Expand All @@ -18,12 +20,14 @@ func (update *Update) isWebhook() bool {
return update.webhookResponse != nil
}

// UpdateRespond defines interface for responding to an update via Webhook.
type UpdateRespond interface {
json.Marshaler
DoVoid(ctx context.Context) error
Bind(client *tg.Client)
}

// Respond to Webhook, if possible or make usual call via Client.
func (update *Update) Respond(ctx context.Context, v UpdateRespond) error {
if update.isWebhook() {
update.webhookResponse <- v
Expand All @@ -35,88 +39,109 @@ func (update *Update) Respond(ctx context.Context, v UpdateRespond) error {
return v.DoVoid(ctx)
}

// MessageUpdate it's extend wrapper around tg.Message.
type MessageUpdate struct {
*tg.Message
Client *tg.Client
Update *Update
}

// Answer calls sendMessage with pre-defined chatID to incoming message chat.
func (msg *MessageUpdate) Answer(text string) *tg.SendMessageCall {
return msg.Client.SendMessage(msg.Chat, text)
}

// AnswerPhoto calls sendPhoto with pre-defined chatID to incoming message chat.
func (msg *MessageUpdate) AnswerPhoto(photo tg.FileArg) *tg.SendPhotoCall {
return msg.Client.SendPhoto(msg.Chat, photo)
}

// AnswerAudio calls sendAudio with pre-defined chatID to incoming message chat.
func (msg *MessageUpdate) AnswerAudio(audio tg.FileArg) *tg.SendAudioCall {
return msg.Client.SendAudio(msg.Chat, audio)
}

// AnswerAnimation calls sendAnimation with pre-defined chatID to incoming message chat.
func (msg *MessageUpdate) AnswerAnimation(animation tg.FileArg) *tg.SendAnimationCall {
return msg.Client.SendAnimation(msg.Chat, animation)
}

// AnswerDocument calls sendDocument with pre-defined chatID to incoming message chat.
func (msg *MessageUpdate) AnswerDocument(document tg.FileArg) *tg.SendDocumentCall {
return msg.Client.SendDocument(msg.Chat, document)
}

// AnswerVideo calls sendVideo with pre-defined chatID to incoming message chat.
func (msg *MessageUpdate) AnswerVideo(video tg.FileArg) *tg.SendVideoCall {
return msg.Client.SendVideo(msg.Chat, video)
}

// AnswerVoice calls sendVoice with pre-defined chatID to incoming message chat.
func (msg *MessageUpdate) AnswerVoice(voice tg.FileArg) *tg.SendVoiceCall {
return msg.Client.SendVoice(msg.Chat, voice)
}

// AnswerVideoNote calls sendVideoNote with pre-defined chatID to incoming message chat.
func (msg *MessageUpdate) AnswerVideoNote(videoNote tg.FileArg) *tg.SendVideoNoteCall {
return msg.Client.SendVideoNote(msg.Chat, videoNote)
}

// AnswerLocation calls sendLocation with pre-defined chatID to incoming message chat.
func (msg *MessageUpdate) AnswerLocation(latitude float64, longitude float64) *tg.SendLocationCall {
return msg.Client.SendLocation(msg.Chat, latitude, longitude)
}

// AnswerVenue calls sendVenue with pre-defined chatID to incoming message chat.
func (msg *MessageUpdate) AnswerVenue(latitude float64, longitude float64, title string, address string) *tg.SendVenueCall {
return msg.Client.SendVenue(msg.Chat, latitude, longitude, title, address)
}

// AnswerContact calls sendContact with pre-defined chatID to incoming message chat.
func (msg *MessageUpdate) AnswerContact(phoneNumber string, firstName string) *tg.SendContactCall {
return msg.Client.SendContact(msg.Chat, phoneNumber, firstName)
}

// AnswerSticker calls sendSticker with pre-defined chatID to incoming message chat.
func (msg *MessageUpdate) AnswerSticker(sticker tg.FileArg) *tg.SendStickerCall {
return msg.Client.SendSticker(msg.Chat, sticker)
}

// AnswerPoll calls sendPoll with pre-defined chatID to incoming message chat.
func (msg *MessageUpdate) AnswerPoll(question string, options []string) *tg.SendPollCall {
return msg.Client.SendPoll(msg.Chat, question, options)
}

// AnswerDice calls sendDice with pre-defined chatID to incoming message chat.
func (msg *MessageUpdate) AnswerDice(emoji string) *tg.SendDiceCall {
return msg.Client.SendDice(msg.Chat).Emoji(emoji)
}

// AnswerChatAction calls sendChatAction with pre-defined chatID to incoming message chat.
func (msg *MessageUpdate) AnswerChatAction(action string) *tg.SendChatActionCall {
return msg.Client.SendChatAction(msg.Chat, action)
}

// Forward incoming message to another chat.
func (msg *MessageUpdate) Forward(to tg.PeerID) *tg.ForwardMessageCall {
return msg.Client.ForwardMessage(to, msg.Chat, msg.ID)
}

// Copy incoming message to another chat.
func (msg *MessageUpdate) Copy(to tg.PeerID) *tg.CopyMessageCall {
return msg.Client.CopyMessage(to, msg.Chat, msg.ID)
}

// EditText of incoming message.
func (msg *MessageUpdate) EditText(text string) *tg.EditMessageTextCall {
return msg.Client.EditMessageText(msg.Chat, msg.ID, text)
}

// EditCaption of incoming message.
func (msg *MessageUpdate) EditCaption(caption string) *tg.EditMessageCaptionCall {
return msg.Client.EditMessageCaption(msg.Chat, msg.ID, caption)
}

// EditReplyMarkup of incoming message.
func (msg *MessageUpdate) EditReplyMarkup(markup tg.InlineKeyboardMarkup) *tg.EditMessageReplyMarkupCall {
return msg.Client.EditMessageReplyMarkup(msg.Chat, msg.ID).ReplyMarkup(markup)
}
Expand Down

0 comments on commit d6333ae

Please sign in to comment.