Skip to content

Commit

Permalink
feat: bussines account router support
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-linch committed Apr 29, 2024
1 parent 431c8ae commit 3114ee0
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tgb/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func (handler MessageHandler) Handle(ctx context.Context, update *Update) error
update.EditedMessage,
update.ChannelPost,
update.EditedChannelPost,
update.BusinessMessage,
update.EditedBusinessMessage,
); msg != nil {
return handler(ctx, &MessageUpdate{
Message: msg,
Expand Down Expand Up @@ -199,3 +201,25 @@ func (handler RemovedChatBoostHandler) Handle(ctx context.Context, update *Updat
Client: update.Client,
})
}

// BusinessConnection it's typed handler for [BusinessConnectionUpdate].
type BusinessConnectionHandler func(context.Context, *BusinessConnectionUpdate) error

func (handler BusinessConnectionHandler) Handle(ctx context.Context, update *Update) error {
return handler(ctx, &BusinessConnectionUpdate{
BusinessConnection: update.BusinessConnection,
Update: update,
Client: update.Client,
})
}

// DeletedBusinessMessageHandler it's typed handler for [DeletedBusinessMessage]
type DeletedBusinessMessageHandler func(context.Context, *DeletedBusinessMessagesUpdate) error

func (handler DeletedBusinessMessageHandler) Handle(ctx context.Context, update *Update) error {
return handler(ctx, &DeletedBusinessMessagesUpdate{
BusinessMessagesDeleted: update.DeletedBusinessMessages,
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 @@ -177,6 +177,26 @@ func (bot *Router) RemovedChatBoost(handler RemovedChatBoostHandler, filters ...
return bot.register(tg.UpdateTypeRemovedChatBoost, handler, filters...)
}

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

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

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

// DeletedBusinessMessages register handlers for Update with not empty DeletedBusinessMessages field.
func (bot *Router) DeletedBusinessMessages(handler DeletedBusinessMessageHandler, filters ...Filter) *Router {
return bot.register(tg.UpdateTypeDeletedBusinessMessages, 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
68 changes: 68 additions & 0 deletions tgb/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,74 @@ func TestRouter(t *testing.T) {
assert.NoError(t, err)
assert.True(t, isRemovedChatBoostHandlerCalled, "removed chat boosted handler should be called")
}

{
isBusinessConnectionHandlerCalled := false

router.BusinessConnection(func(ctx context.Context, msg *BusinessConnectionUpdate) error {
assert.NotNil(t, msg.BusinessConnection)
isBusinessConnectionHandlerCalled = true
return nil
})

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

assert.NoError(t, err)
assert.True(t, isBusinessConnectionHandlerCalled, "business connection handler should be called")
}

{
isBusinessMessageHandlerCalled := false

router.BusinessMessage(func(ctx context.Context, msg *MessageUpdate) error {
assert.NotNil(t, msg.Message)
isBusinessMessageHandlerCalled = true
return nil
})

err := router.Handle(ctx, &Update{Update: &tg.Update{
BusinessMessage: &tg.Message{},
}})

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

{
isEditedBusinessMessageHandlerCalled := false

router.EditedBusinessMessage(func(ctx context.Context, msg *MessageUpdate) error {
assert.NotNil(t, msg.Message)
isEditedBusinessMessageHandlerCalled = true
return nil
})

err := router.Handle(ctx, &Update{Update: &tg.Update{
EditedBusinessMessage: &tg.Message{},
}})

assert.NoError(t, err)
assert.True(t, isEditedBusinessMessageHandlerCalled, "edited business message handler should be called")
}

{
isDeletedBusinessMessagesHandlerCalled := false

router.DeletedBusinessMessages(func(ctx context.Context, msg *DeletedBusinessMessagesUpdate) error {
assert.NotNil(t, msg.BusinessMessagesDeleted)
isDeletedBusinessMessagesHandlerCalled = true
return nil
})

err := router.Handle(ctx, &Update{Update: &tg.Update{
DeletedBusinessMessages: &tg.BusinessMessagesDeleted{},
}})

assert.NoError(t, err)
assert.True(t, isDeletedBusinessMessagesHandlerCalled, "deleted business messages handler should be called")
}
})

t.Run("FilterNotAllow", func(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions tgb/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,16 @@ type RemovedChatBoostUpdate struct {
Update *Update
Client *tg.Client
}

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

type DeletedBusinessMessagesUpdate struct {
*tg.BusinessMessagesDeleted
Update *Update
Client *tg.Client
}
24 changes: 24 additions & 0 deletions types_gen_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,10 @@ const (
UpdateTypeMessageReactionCount
UpdateTypeChatBoost
UpdateTypeRemovedChatBoost
UpdateTypeBusinessConnection
UpdateTypeBusinessMessage
UpdateTypeEditedBusinessMessage
UpdateTypeDeletedBusinessMessages
)

// MarshalText implements encoding.TextMarshaler.
Expand Down Expand Up @@ -1132,6 +1136,14 @@ func (typ *UpdateType) UnmarshalText(v []byte) error {
*typ = UpdateTypeChatBoost
case "removed_chat_boost":
*typ = UpdateTypeRemovedChatBoost
case "business_connection":
*typ = UpdateTypeBusinessConnection
case "business_message":
*typ = UpdateTypeBusinessMessage
case "edited_business_message":
*typ = UpdateTypeEditedBusinessMessage
case "deleted_business_messages":
*typ = UpdateTypeDeletedBusinessMessages
default:
return fmt.Errorf("unknown update type")
}
Expand Down Expand Up @@ -1161,6 +1173,10 @@ func (typ UpdateType) String() string {
"message_reaction_count",
"chat_boost",
"removed_chat_boost",
"business_connection",
"business_message",
"edited_business_message",
"deleted_business_messages",
}[typ-1]
}

Expand Down Expand Up @@ -1205,6 +1221,14 @@ func (update *Update) Type() UpdateType {
return UpdateTypeChatBoost
case update.RemovedChatBoost != nil:
return UpdateTypeRemovedChatBoost
case update.BusinessConnection != nil:
return UpdateTypeBusinessConnection
case update.BusinessMessage != nil:
return UpdateTypeBusinessMessage
case update.EditedBusinessMessage != nil:
return UpdateTypeEditedBusinessMessage
case update.DeletedBusinessMessages != nil:
return UpdateTypeDeletedBusinessMessages
default:
return UpdateTypeUnknown
}
Expand Down
20 changes: 20 additions & 0 deletions types_gen_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,10 @@ func TestUpdateType_UnmarshalText(t *testing.T) {
{"message_reaction_count", UpdateTypeMessageReactionCount, false},
{"chat_boost", UpdateTypeChatBoost, false},
{"removed_chat_boost", UpdateTypeRemovedChatBoost, false},
{"business_connection", UpdateTypeBusinessConnection, false},
{"business_message", UpdateTypeBusinessMessage, false},
{"edited_business_message", UpdateTypeEditedBusinessMessage, false},
{"deleted_business_messages", UpdateTypeDeletedBusinessMessages, false},
{"test", UpdateTypeUnknown, true},
} {
t.Run(test.Text, func(t *testing.T) {
Expand Down Expand Up @@ -959,6 +963,22 @@ func TestUpdate_Type(t *testing.T) {
Update: &Update{RemovedChatBoost: &ChatBoostRemoved{}},
Want: UpdateTypeRemovedChatBoost,
},
{
Update: &Update{BusinessConnection: &BusinessConnection{}},
Want: UpdateTypeBusinessConnection,
},
{
Update: &Update{BusinessMessage: &Message{}},
Want: UpdateTypeBusinessMessage,
},
{
Update: &Update{EditedBusinessMessage: &Message{}},
Want: UpdateTypeEditedBusinessMessage,
},
{
Update: &Update{DeletedBusinessMessages: &BusinessMessagesDeleted{}},
Want: UpdateTypeDeletedBusinessMessages,
},
} {
assert.Equal(t, test.Want, test.Update.Type())
}
Expand Down

0 comments on commit 3114ee0

Please sign in to comment.