Skip to content

Commit

Permalink
- add method bot.ID() int64
Browse files Browse the repository at this point in the history
- add method `bot.Token() string`
  • Loading branch information
negasus committed Jan 10, 2025
1 parent 0965b61 commit 6317bb0
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
1 change: 1 addition & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 6317bb0

Please sign in to comment.