Skip to content

Commit

Permalink
Bot API 7.2 support (#155)
Browse files Browse the repository at this point in the history
* Update to bot API 7.2

* Add businessconnection filters

* Update effectiveX calls to support business messages

* Latest regen
  • Loading branch information
PaulSonOfLars authored May 4, 2024
1 parent 516bfcd commit ae21465
Show file tree
Hide file tree
Showing 11 changed files with 509 additions and 126 deletions.
16 changes: 16 additions & 0 deletions ext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ func NewContext(update *gotgbot.Update, data map[string]interface{}) *Context {
msg = update.EditedChannelPost
chat = &update.EditedChannelPost.Chat

case update.BusinessConnection != nil:
user = &update.BusinessConnection.User

case update.BusinessMessage != nil:
msg = update.BusinessMessage
chat = &update.BusinessMessage.Chat
user = update.BusinessMessage.From

case update.EditedBusinessMessage != nil:
msg = update.EditedBusinessMessage
chat = &update.EditedBusinessMessage.Chat
user = update.EditedBusinessMessage.From

case update.DeletedBusinessMessages != nil:
chat = &update.DeletedBusinessMessages.Chat

case update.MessageReaction != nil:
user = update.MessageReaction.User
chat = &update.MessageReaction.Chat
Expand Down
26 changes: 26 additions & 0 deletions ext/handlers/businessconnection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package handlers

import (
"fmt"

"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters"
)

type BusinessConnection struct {
Filter filters.BusinessConnection
Response Response
}

func (bc BusinessConnection) CheckUpdate(b *gotgbot.Bot, ctx *ext.Context) bool {
return ctx.BusinessConnection != nil && bc.Filter(ctx.BusinessConnection)
}

func (bc BusinessConnection) HandleUpdate(b *gotgbot.Bot, ctx *ext.Context) error {
return bc.Response(b, ctx)
}

func (bc BusinessConnection) Name() string {
return fmt.Sprintf("businessconnection_%p", bc.Response)
}
32 changes: 32 additions & 0 deletions ext/handlers/filters/businessconnection/businessconnection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package message

import (
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters"
)

func All(_ *gotgbot.BusinessConnection) bool {
return true
}

func BusinessId(businessId string) filters.BusinessConnection {
return func(bc *gotgbot.BusinessConnection) bool {
return bc.Id == businessId
}
}

func FromUserID(userId int64) filters.BusinessConnection {
return func(bc *gotgbot.BusinessConnection) bool {
return bc.User.Id == userId
}
}

func ChatID(bc *gotgbot.BusinessConnection, chatId int64) filters.BusinessConnection {
return func(bc *gotgbot.BusinessConnection) bool {
return bc.UserChatId == chatId
}
}

func CanReply(bc *gotgbot.BusinessConnection) bool {
return bc.CanReply
}
4 changes: 4 additions & 0 deletions ext/handlers/filters/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func Channel(msg *gotgbot.Message) bool {
return msg.Chat.Type == "channel"
}

func Business(msg *gotgbot.Message) bool {
return msg.BusinessConnectionId != ""
}

func Forwarded(msg *gotgbot.Message) bool {
return msg.ForwardOrigin != nil
}
Expand Down
1 change: 1 addition & 0 deletions ext/handlers/filters/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ type (
PreCheckoutQuery func(pcq *gotgbot.PreCheckoutQuery) bool
ShippingQuery func(sq *gotgbot.ShippingQuery) bool
Reaction func(mru *gotgbot.MessageReactionUpdated) bool
BusinessConnection func(bc *gotgbot.BusinessConnection) bool
)
25 changes: 20 additions & 5 deletions ext/handlers/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
)

type Message struct {
AllowEdited bool
AllowChannel bool
Filter filters.Message
Response Response
AllowEdited bool
AllowChannel bool
AllowBusiness bool
Filter filters.Message
Response Response
}

func NewMessage(f filters.Message, r Response) Message {
Expand All @@ -36,15 +37,21 @@ func (m Message) SetAllowChannel(allow bool) Message {
return m
}

// SetAllowBusiness Enables business messages for this handler.
func (m Message) SetAllowBusiness(allow bool) Message {
m.AllowBusiness = allow
return m
}

func (m Message) CheckUpdate(b *gotgbot.Bot, ctx *ext.Context) bool {
if ctx.Message != nil {
return m.Filter == nil || m.Filter(ctx.Message)
}

// If edits are allowed, and message is edited.
if m.AllowEdited && ctx.EditedMessage != nil {
return m.Filter == nil || m.Filter(ctx.EditedMessage)
}

// If channel posts are allowed, and message is channel post.
if m.AllowChannel && ctx.ChannelPost != nil {
return m.Filter == nil || m.Filter(ctx.ChannelPost)
Expand All @@ -54,6 +61,14 @@ func (m Message) CheckUpdate(b *gotgbot.Bot, ctx *ext.Context) bool {
return m.Filter == nil || m.Filter(ctx.EditedChannelPost)
}

// Same logic, for business messages
if m.AllowBusiness && ctx.BusinessMessage != nil {
return m.Filter == nil || m.Filter(ctx.BusinessMessage)
}
if m.AllowBusiness && m.AllowEdited && ctx.EditedBusinessMessage != nil {
return m.Filter == nil || m.Filter(ctx.EditedBusinessMessage)
}

return false
}

Expand Down
52 changes: 34 additions & 18 deletions gen_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@ package gotgbot

// The consts listed below represent all the update types that can be requested from telegram.
const (
UpdateTypeMessage = "message"
UpdateTypeEditedMessage = "edited_message"
UpdateTypeChannelPost = "channel_post"
UpdateTypeEditedChannelPost = "edited_channel_post"
UpdateTypeMessageReaction = "message_reaction"
UpdateTypeMessageReactionCount = "message_reaction_count"
UpdateTypeInlineQuery = "inline_query"
UpdateTypeChosenInlineResult = "chosen_inline_result"
UpdateTypeCallbackQuery = "callback_query"
UpdateTypeShippingQuery = "shipping_query"
UpdateTypePreCheckoutQuery = "pre_checkout_query"
UpdateTypePoll = "poll"
UpdateTypePollAnswer = "poll_answer"
UpdateTypeMyChatMember = "my_chat_member"
UpdateTypeChatMember = "chat_member"
UpdateTypeChatJoinRequest = "chat_join_request"
UpdateTypeChatBoost = "chat_boost"
UpdateTypeRemovedChatBoost = "removed_chat_boost"
UpdateTypeMessage = "message"
UpdateTypeEditedMessage = "edited_message"
UpdateTypeChannelPost = "channel_post"
UpdateTypeEditedChannelPost = "edited_channel_post"
UpdateTypeBusinessConnection = "business_connection"
UpdateTypeBusinessMessage = "business_message"
UpdateTypeEditedBusinessMessage = "edited_business_message"
UpdateTypeDeletedBusinessMessages = "deleted_business_messages"
UpdateTypeMessageReaction = "message_reaction"
UpdateTypeMessageReactionCount = "message_reaction_count"
UpdateTypeInlineQuery = "inline_query"
UpdateTypeChosenInlineResult = "chosen_inline_result"
UpdateTypeCallbackQuery = "callback_query"
UpdateTypeShippingQuery = "shipping_query"
UpdateTypePreCheckoutQuery = "pre_checkout_query"
UpdateTypePoll = "poll"
UpdateTypePollAnswer = "poll_answer"
UpdateTypeMyChatMember = "my_chat_member"
UpdateTypeChatMember = "chat_member"
UpdateTypeChatJoinRequest = "chat_join_request"
UpdateTypeChatBoost = "chat_boost"
UpdateTypeRemovedChatBoost = "removed_chat_boost"
)

// GetType is a helper method to easily identify the type of update that is being received.
Expand All @@ -40,6 +44,18 @@ func (u Update) GetType() string {
case u.EditedChannelPost != nil:
return UpdateTypeEditedChannelPost

case u.BusinessConnection != nil:
return UpdateTypeBusinessConnection

case u.BusinessMessage != nil:
return UpdateTypeBusinessMessage

case u.EditedBusinessMessage != nil:
return UpdateTypeEditedBusinessMessage

case u.DeletedBusinessMessages != nil:
return UpdateTypeDeletedBusinessMessages

case u.MessageReaction != nil:
return UpdateTypeMessageReaction

Expand Down
5 changes: 5 additions & 0 deletions gen_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

package gotgbot

// Get Helper method for Bot.GetBusinessConnection.
func (bc BusinessConnection) Get(b *Bot, opts *GetBusinessConnectionOpts) (*BusinessConnection, error) {
return b.GetBusinessConnection(bc.Id, opts)
}

// Answer Helper method for Bot.AnswerCallbackQuery.
func (cq CallbackQuery) Answer(b *Bot, opts *AnswerCallbackQueryOpts) (bool, error) {
return b.AnswerCallbackQuery(cq.Id, opts)
Expand Down
Loading

0 comments on commit ae21465

Please sign in to comment.