Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bot API v7.0 & v7.1 #137

Merged
merged 11 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,20 @@ func run(ctx context.Context) error {
// handle other messages
Message(func(ctx context.Context, msg *tgb.MessageUpdate) error {
return msg.Copy(msg.Chat).DoVoid(ctx)
})
}).
MessageReaction(func(ctx context.Context, reaction *tgb.MessageReactionUpdate) error {
// sets same reaction to the message
answer := tg.NewSetMessageReactionCall(reaction.Chat, reaction.MessageID).Reaction(reaction.NewReaction)
return reaction.Update.Reply(ctx, answer)
})

return tgb.NewPoller(
router,
client,
tgb.WithPollerAllowedUpdates(
tg.UpdateTypeMessage,
tg.UpdateTypeMessageReaction,
)
).Run(ctx)
}
```
Expand Down
10 changes: 6 additions & 4 deletions examples/calculator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ func main() {
// handle other buttons
var currentText string

if cbq.Message == nil {
if cbq.Message.IsInaccessible() {
return cbq.AnswerText(
tg.HTML.Italic("this keyboard is too old, please /start again"),
true,
).DoVoid(ctx)
}

currentText = cbq.Message.Text
msg := cbq.Message.Message

currentText = msg.Text

if currentText == typingMessage {
currentText = ""
Expand All @@ -50,8 +52,8 @@ func main() {
}

return cbq.Client.EditMessageText(
cbq.Message.Chat.ID,
cbq.Message.ID,
msg.Chat.ID,
msg.ID,
currentText,
).ReplyMarkup(newKeyboard()).DoVoid(ctx)
}),
Expand Down
15 changes: 12 additions & 3 deletions examples/echo-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ func main() {
return msg.Answer(
tg.HTML.Text(
tg.HTML.Bold("👋 Hi, I'm echo bot!"),
"",
tg.HTML.Italic("🚀 Powered by", tg.HTML.Spoiler(tg.HTML.Link("go-tg", "github.com/mr-linch/go-tg"))),
tg.HTML.Line("Send me a message and I will echo it back to you. Also you can send me a reaction and I will react with the same emoji."),
tg.HTML.Italic("🚀 Powered by", tg.HTML.Spoiler("go-tg")),
),
).ParseMode(tg.HTML).DoVoid(ctx)
).ParseMode(tg.HTML).LinkPreviewOptions(tg.LinkPreviewOptions{
URL: "https://github.com/mr-linch/go-tg",
PreferLargeMedia: true,
}).DoVoid(ctx)

}, tgb.Command("start", tgb.WithCommandAlias("help"))).
Message(func(ctx context.Context, msg *tgb.MessageUpdate) error {
// handles gopher image
Expand All @@ -47,6 +51,11 @@ func main() {
Message(func(ctx context.Context, msg *tgb.MessageUpdate) error {
// handle other messages
return msg.Update.Reply(ctx, msg.Copy(msg.Chat))
}).
MessageReaction(func(ctx context.Context, reaction *tgb.MessageReactionUpdate) error {
// sets same reaction to the message
answer := tg.NewSetMessageReactionCall(reaction.Chat, reaction.MessageID).Reaction(reaction.NewReaction)
return reaction.Update.Reply(ctx, answer)
}),
)
}
20 changes: 20 additions & 0 deletions examples/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ func run(ctx context.Context, handler tgb.Handler) error {
handler,
client,
tgb.WithPollerLogger(log.Default()),
tgb.WithPollerAllowedUpdates(
tg.UpdateTypeMessage,
tg.UpdateTypeEditedMessage,
tg.UpdateTypeChannelPost,
tg.UpdateTypeEditedChannelPost,
tg.UpdateTypeMessageReaction,
tg.UpdateTypeMessageReactionCount,
tg.UpdateTypeInlineQuery,
tg.UpdateTypeChosenInlineResult,
tg.UpdateTypeCallbackQuery,
tg.UpdateTypeShippingQuery,
tg.UpdateTypePreCheckoutQuery,
tg.UpdateTypePoll,
tg.UpdateTypePollAnswer,
tg.UpdateTypeMyChatMember,
tg.UpdateTypeChatMember,
tg.UpdateTypeChatJoinRequest,
tg.UpdateTypeChatBoost,
tg.UpdateTypeRemovedChatBoost,
),
).Run(ctx)
}

Expand Down
582 changes: 377 additions & 205 deletions methods_gen.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tgb/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func ChatType(types ...tg.ChatType) Filter {
case msg != nil:
typ = msg.Chat.Type
case update.CallbackQuery != nil && update.CallbackQuery.Message != nil:
typ = update.CallbackQuery.Message.Chat.Type
typ = update.CallbackQuery.Message.Chat().Type
case update.InlineQuery != nil:
typ = update.InlineQuery.ChatType
case update.MyChatMember != nil:
Expand Down
6 changes: 4 additions & 2 deletions tgb/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,10 @@ func TestChatType(t *testing.T) {
ChatType(tg.ChatTypePrivate),
&tg.Update{
CallbackQuery: &tg.CallbackQuery{
Message: &tg.Message{
Chat: tg.Chat{Type: tg.ChatTypePrivate},
Message: &tg.MaybeInaccessibleMessage{
Message: &tg.Message{
Chat: tg.Chat{Type: tg.ChatTypePrivate},
},
},
},
},
Expand Down
44 changes: 44 additions & 0 deletions tgb/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,47 @@ func firstNotNil[T any](fields ...*T) *T {

return nil
}

// MessageReactionHandler it's typed handler for [MessageReactionUpdate].
type MessageReactionHandler func(context.Context, *MessageReactionUpdate) error

func (handler MessageReactionHandler) Handle(ctx context.Context, update *Update) error {
return handler(ctx, &MessageReactionUpdate{
MessageReactionUpdated: update.MessageReaction,
Update: update,
Client: update.Client,
})
}

// MessageReactionCountHandler it's typed handler for [MessageReactionCountUpdate].
type MessageReactionCountHandler func(context.Context, *MessageReactionCountUpdate) error

func (handler MessageReactionCountHandler) Handle(ctx context.Context, update *Update) error {
return handler(ctx, &MessageReactionCountUpdate{
MessageReactionCountUpdated: update.MessageReactionCount,
Update: update,
Client: update.Client,
})
}

// ChatBoostHandler it's typed handler for [ChatBoostUpdate].
type ChatBoostHandler func(context.Context, *ChatBoostUpdate) error

func (handler ChatBoostHandler) Handle(ctx context.Context, update *Update) error {
return handler(ctx, &ChatBoostUpdate{
ChatBoostUpdated: update.ChatBoost,
Update: update,
Client: update.Client,
})
}

// RemovedChatBoostHandler it's typed handler for [RemovedChatBoostUpdate].
type RemovedChatBoostHandler func(context.Context, *RemovedChatBoostUpdate) error

func (handler RemovedChatBoostHandler) Handle(ctx context.Context, update *Update) error {
return handler(ctx, &RemovedChatBoostUpdate{
ChatBoostRemoved: update.RemovedChatBoost,
Update: update,
Client: update.Client,
})
}
20 changes: 20 additions & 0 deletions tgb/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ func (bot *Router) ChatJoinRequest(handler ChatJoinRequestHandler, filters ...Fi
return bot.register(tg.UpdateTypeChatJoinRequest, handler, filters...)
}

// MessageReaction register handlers for Update with not empty MessageReaction field.
func (bot *Router) MessageReaction(handler MessageReactionHandler, filters ...Filter) *Router {
return bot.register(tg.UpdateTypeMessageReaction, handler, filters...)
}

// MessageReactionCount register handlers for Update with not empty MessageReactionCount field.
func (bot *Router) MessageReactionCount(handler MessageReactionCountHandler, filters ...Filter) *Router {
return bot.register(tg.UpdateTypeMessageReactionCount, handler, filters...)
}

// ChatBoost register handlers for Update with not empty ChatBoost field.
func (bot *Router) ChatBoost(handler ChatBoostHandler, filters ...Filter) *Router {
return bot.register(tg.UpdateTypeChatBoost, handler, filters...)
}

// RemovedChatBoost register handlers for Update with not empty RemovedChatBoost field.
func (bot *Router) RemovedChatBoost(handler RemovedChatBoostHandler, filters ...Filter) *Router {
return bot.register(tg.UpdateTypeRemovedChatBoost, handler, filters...)
}

// Error registers a handler for errors.
// If any error occurs in the chain, it will be passed to that handler.
// By default, errors are returned back by handler method.
Expand Down
69 changes: 69 additions & 0 deletions tgb/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,75 @@ func TestRouter(t *testing.T) {
assert.NoError(t, err)
assert.True(t, isChatJoinRequestHandlerCalled, "chat join request handler should be called")
}

{
isMessageReactionHandlerCalled := false

router.MessageReaction(func(ctx context.Context, msg *MessageReactionUpdate) error {
assert.NotNil(t, msg.MessageReactionUpdated)
isMessageReactionHandlerCalled = true
return nil
})

err := router.Handle(ctx, &Update{Update: &tg.Update{
MessageReaction: &tg.MessageReactionUpdated{},
}})

assert.NoError(t, err)
assert.True(t, isMessageReactionHandlerCalled, "message reaction handler should be called")
}

{
isMessageReactionCountHandlerCalled := false

router.MessageReactionCount(func(ctx context.Context, msg *MessageReactionCountUpdate) error {
assert.NotNil(t, msg.MessageReactionCountUpdated)
isMessageReactionCountHandlerCalled = true
return nil
})

err := router.Handle(ctx, &Update{Update: &tg.Update{
MessageReactionCount: &tg.MessageReactionCountUpdated{},
}})

assert.NoError(t, err)
assert.True(t, isMessageReactionCountHandlerCalled, "message reaction count handler should be called")
}

{
isChatBoostedHandlerCalled := false

router.ChatBoost(func(ctx context.Context, msg *ChatBoostUpdate) error {
assert.NotNil(t, msg.ChatBoostUpdated)
isChatBoostedHandlerCalled = true
return nil

})

err := router.Handle(ctx, &Update{Update: &tg.Update{
ChatBoost: &tg.ChatBoostUpdated{},
}})

assert.NoError(t, err)
assert.True(t, isChatBoostedHandlerCalled, "chat boosted handler should be called")
}

{
isRemovedChatBoostHandlerCalled := false

router.RemovedChatBoost(func(ctx context.Context, msg *RemovedChatBoostUpdate) error {
assert.NotNil(t, msg.ChatBoostRemoved)
isRemovedChatBoostHandlerCalled = true
return nil
})

err := router.Handle(ctx, &Update{Update: &tg.Update{
RemovedChatBoost: &tg.ChatBoostRemoved{},
}})

assert.NoError(t, err)
assert.True(t, isRemovedChatBoostHandlerCalled, "removed chat boosted handler should be called")
}
})

t.Run("FilterNotAllow", func(t *testing.T) {
Expand Down
8 changes: 5 additions & 3 deletions tgb/session/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,11 @@ func TestManager_Wrap(t *testing.T) {
ID: 1234,
CallbackQuery: &tg.CallbackQuery{
Data: "not_found",
Message: &tg.Message{
Chat: tg.Chat{
ID: 1,
Message: &tg.MaybeInaccessibleMessage{
Message: &tg.Message{
Chat: tg.Chat{
ID: 1,
},
},
},
},
Expand Down
28 changes: 28 additions & 0 deletions tgb/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,31 @@ func (joinRequest *ChatJoinRequestUpdate) Approve() *tg.ApproveChatJoinRequestCa
func (joinRequest *ChatJoinRequestUpdate) Decline() *tg.DeclineChatJoinRequestCall {
return joinRequest.Client.DeclineChatJoinRequest(joinRequest.Chat, joinRequest.From.ID)
}

// MessageReactionUpdate it's extend wrapper around [tg.MessageReactionUpdated].
type MessageReactionUpdate struct {
*tg.MessageReactionUpdated
Update *Update
Client *tg.Client
}

// MessageReactionCountUpdate it's extend wrapper around [tg.MessageReactionCountUpdated].
type MessageReactionCountUpdate struct {
*tg.MessageReactionCountUpdated
Update *Update
Client *tg.Client
}

// ChatBoostUpdate it's extend wrapper around [tg.ChatBoostUpdated].
type ChatBoostUpdate struct {
*tg.ChatBoostUpdated
Update *Update
Client *tg.Client
}

// RemovedChatBoostUpdate it's extend wrapper around [tg.RemovedChatBoost].
type RemovedChatBoostUpdate struct {
*tg.ChatBoostRemoved
Update *Update
Client *tg.Client
}
Loading
Loading