add MatchTypeCommandStartMaybeWithBotUsernameSuffix command match method#196
add MatchTypeCommandStartMaybeWithBotUsernameSuffix command match method#196Interstellar750 wants to merge 5 commits intogo-telegram:mainfrom
MatchTypeCommandStartMaybeWithBotUsernameSuffix command match method#196Conversation
handlers.go:
add field `username` in `handler` struct
add `MatchTypeCommandStartMaybeWithBotUsernameSuffix` to allow match commands with or without the username suffix
WARING: this match method can't match username suffix if bot with the`bot.WithSkipGetMe()` option
bot.go:
add field `username` in `Bot` struct
add method `bot.Username()` `string`
WARING: this method will return empty string if bot with the `bot.WithSkipGetMe()` option
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## main #196 +/- ##
==========================================
+ Coverage 45.31% 45.54% +0.23%
==========================================
Files 29 29
Lines 2315 2325 +10
==========================================
+ Hits 1049 1059 +10
Misses 1223 1223
Partials 43 43 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@Interstellar750 hello! Could you please tell why you closed this PR? I think it makes sense to reopen it and merge it into the main branch. |
|
@parmetra Thank you for your attention. I think this method is still somewhat problematic. IIt relies on the It doesn't guarantee it will work in both states, so I closed this PR. A better approach is to create a package and store messages about the bot within it. When needed, import this package and read the username field for matching. You might also need to use the |
Create a field name
usernamein theBotandhandlerstructures to match commands like/command@username_bot.Also created a
bot.Username()stringmethod to return the bot's username (without@).When using
bot.New()withoutbot.WithSkipGetMe()option, theusernamefield is filling by following code:func New(token string, options ...Option) (*Bot, error) { // other code... if !b.skipGetMe { - _, err := b.GetMe(ctx) + botInfo, err := b.GetMe(ctx) if err != nil { return nil, fmt.Errorf("error call getMe, %w", err) } + b.username = botInfo.Username } return b, nil }If with the
bot.WithSkipGetMe()option, this match method will work the same asMatchTypeCommandStartOnly, thenbot.Username()will return an empty string.This PR may not be good. If you think it is not good, you can close it directly. Some issues mentioned that this is needed, or are there other better ways?