From 6317bb0bb5774c4f615fd4fb083faba8227e9269 Mon Sep 17 00:00:00 2001 From: negasus Date: Fri, 10 Jan 2025 12:00:32 +0300 Subject: [PATCH] - add method `bot.ID() int64` - add method `bot.Token() string` --- CHANGELOG.md | 5 +++++ README.md | 6 ++++++ bot.go | 18 ++++++++++++++++++ bot_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ common.go | 1 + 5 files changed, 78 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b8a8a3..eb749d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## v1.13.1 (2025-01-10) + +- add method `bot.ID() int64` +- add method `bot.Token() string` + ## v1.13.0 (2025-01-10) - support API 8.2 (#144) diff --git a/README.md b/README.md index 53516f7..b1689c5 100644 --- a/README.md +++ b/README.md @@ -408,6 +408,12 @@ if errors.Is(err, mybot.ErrorConflict) { } ``` +## Other + +- `bot.ID() int64` - returns bot ID. Bot ID is a unique identifier for the bot, obtained from the token as first part before `:`. Example: `110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw` - bot ID is `110201543`. If the bot token is invalid, the bot ID will be 0. +- `bot.Token() string` - returns bot token +- `bot.SetToken()` - set new bot token + ## UI Components In the repo https://github.com/go-telegram/ui you can find a some UI elements for your bot. diff --git a/bot.go b/bot.go index 7078ae2..5f6fa8a 100644 --- a/bot.go +++ b/bot.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "net/http" + "strconv" "strings" "sync" "time" @@ -100,11 +101,26 @@ func New(token string, options ...Option) (*Bot, error) { return b, nil } +// ID returns the bot ID +func (b *Bot) ID() int64 { + id, err := strconv.ParseInt(strings.Split(b.token, ":")[0], 10, 64) + if err != nil { + return 0 + } + + return id +} + // SetToken sets the bot token func (b *Bot) SetToken(token string) { b.token = token } +// Token returns the bot token +func (b *Bot) Token() string { + return b.token +} + // StartWebhook starts the Bot with webhook mode func (b *Bot) StartWebhook(ctx context.Context) { wg := sync.WaitGroup{} @@ -148,11 +164,13 @@ func (b *Bot) error(format string, args ...interface{}) { b.errorsHandler(fmt.Errorf(format, args...)) } +// True and False returns the pointer to bool func True() *bool { b := true return &b } +// False and True returns the pointer to bool func False() *bool { b := false return &b diff --git a/bot_test.go b/bot_test.go index 0142292..3831eca 100644 --- a/bot_test.go +++ b/bot_test.go @@ -348,3 +348,51 @@ func TestBot_Start(t *testing.T) { t.Errorf("not called default handler") } } + +func TestBot_ID(t *testing.T) { + type fields struct { + token string + } + tests := []struct { + name string + fields fields + want int64 + }{ + {name: "empty token", fields: fields{token: ""}, want: 0}, + {name: "no colon", fields: fields{token: "xxx"}, want: 0}, + {name: "bad value", fields: fields{token: "123xxx:xxx"}, want: 0}, + {name: "bad value", fields: fields{token: ":xxx"}, want: 0}, + {name: "ok", fields: fields{token: "123456:xxx"}, want: 123456}, + {name: "two colon", fields: fields{token: "123456:5678:xxx"}, want: 123456}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := &Bot{ + token: tt.fields.token, + } + if got := b.ID(); got != tt.want { + t.Errorf("ID() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestBot_Token(t *testing.T) { + b := &Bot{token: "123456:xxx"} + + token := b.Token() + + if token != "123456:xxx" { + t.Errorf("Token() = %s, want %s", token, "123456:xxx") + } +} + +func TestBot_SetToken(t *testing.T) { + b := &Bot{} + + b.SetToken("123456:xxx") + + if b.token != "123456:xxx" { + t.Errorf("SetToken() = %s, want %s", b.token, "123456:xxx") + } +} diff --git a/common.go b/common.go index e550f6f..9433bf8 100644 --- a/common.go +++ b/common.go @@ -87,6 +87,7 @@ func RandomString(n int) string { return string(b) } +// WebAppUser represents user model from webapp request type WebAppUser struct { ID int `json:"id"` IsBot bool `json:"is_bot"`