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

Proposal: Special ChatID type to describe Recipient only by its integer ID #263

Closed
demget opened this issue Apr 16, 2020 · 1 comment
Closed
Labels
Milestone

Comments

@demget
Copy link
Collaborator

demget commented Apr 16, 2020

Something like ChatID helper type which is actually int64 that implements Recipient interface and returns itself. It would make work with specific chat IDs much easier.

Before:

type Config struct {
    AdminGroupID int64
}

b.Send(&tb.Chat{ID: conf.AdminGroupID}, "message")

After:

type Config struct {
    AdminGroup tb.ChatID
}

b.Send(conf.AdminGroup, "message")
@demget demget changed the title Proposal: Special ChatID to describe Recipient by its ID Proposal: Special ChatID type to describe Recipient by its ID Apr 16, 2020
@demget demget changed the title Proposal: Special ChatID type to describe Recipient by its ID Proposal: Special ChatID type to describe Recipient only by its integer ID Apr 16, 2020
@demget demget added the feature label Apr 16, 2020
@demget demget removed the proposal label Apr 27, 2020
@demget demget added this to the v2.2 milestone May 4, 2020
@demget
Copy link
Collaborator Author

demget commented May 5, 2020

Just implemented it in 183d648.

The possible problem is that you can't represent a string username as a recipient. But on the other hand, ChatID type is about ID, not Name.

// ChatID represents a chat or an user integer ID, which can be used
// as recipient in bot methods. It is very useful in cases where
// you have special group IDs, for example in your config, and don't
// want to wrap it into *tb.Chat every time you send messages.
//
// Example:
//
//		group := tb.ChatID(-100756389456)
//		b.Send(group, "Hello!")
//
//		type Config struct {
//			AdminGroup tb.ChatID `json:"admin_group"`
//		}
//		b.Send(conf.AdminGroup, "Hello!")
//
type ChatID int64

// Recipient returns chat ID (see Recipient interface).
func (i ChatID) Recipient() string {
	return strconv.FormatInt(int64(i), 10)
}

@demget demget closed this as completed May 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant