Skip to content

Conversation

@Interstellar750
Copy link
Contributor

@Interstellar750 Interstellar750 commented Jul 10, 2025

Create a field name username in the Bot and handler structures to match commands like /command@username_bot.

Also created a bot.Username() string method to return the bot's username (without @).

When using bot.New() without bot.WithSkipGetMe() option, the username field 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 as MatchTypeCommandStartOnly, then bot.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?

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-commenter
Copy link

codecov-commenter commented Jul 10, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 45.54%. Comparing base (a88fae3) to head (404d70c).

❗ 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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@parmetra
Copy link

@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.
This feature is really useful - and it will work straight out of the box without any additional middleware.

@Interstellar750
Copy link
Contributor Author

@parmetra Thank you for your attention.

I think this method is still somewhat problematic. IIt relies on the bot.GetMe() method call inside bot.New() function to fill the username field, but if the bot.WithSkipGetMe() option is used when creating the bot instance, this field will be empty and it will never work.

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 bot.WithDefaultHandler() option to specify a handler to manually dispatch update and matching commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants