Skip to content

Commit

Permalink
Check on start (#6)
Browse files Browse the repository at this point in the history
* call getMe on New and returns error

* update changelog
  • Loading branch information
negasus authored Oct 14, 2022
1 parent 0dca12b commit 3b96799
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.3.0 (2022-10-14)

- **[API CHANGE]** method New now call `getMe` after init and returns error as second result on fail

## v0.2.2 (2022-08-23)

- support bot api v6.2
Expand Down
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func main() {
bot.WithDefaultHandler(handler),
}

b := bot.New("YOUR_BOT_TOKEN_FROM_BOTFATHER", opts...)
b, err := bot.New("YOUR_BOT_TOKEN_FROM_BOTFATHER", opts...)
if err != nil {
panic(err)
}

b.Start(ctx)
}
Expand Down Expand Up @@ -60,15 +63,18 @@ go get -u github.com/go-telegram/bot
Initialize and run the bot:

```go
b := bot.New("YOUR_BOT_TOKEN_FROM_BOTFATHER")
b, err := bot.New("YOUR_BOT_TOKEN_FROM_BOTFATHER")

b.Start(context.TODO())
```

On create bot will call the `getMe` method (with 5 sec timeout). And returns error on fail.
If you want to change this timeout, use option `bot.WithCheckInitTimeout`

You can to define default handler for the bot:

```go
b := bot.New("YOUR_BOT_TOKEN_FROM_BOTFATHER", bot.WithDefaultHandler(handler))
b, err := bot.New("YOUR_BOT_TOKEN_FROM_BOTFATHER", bot.WithDefaultHandler(handler))

func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
// this handler will be called for all updates
Expand All @@ -89,7 +95,7 @@ func main() {
bot.WithDefaultHandler(handler),
}

b := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
b, _ := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)

// call methods.SetWebhook if needed

Expand Down Expand Up @@ -143,7 +149,7 @@ bot.SendMessage(ctx, &bot.SendMessageParams{...})
You can use options to customize the bot.

```go
b := bot.New("YOUR_BOT_TOKEN_FROM_BOTFATHER", opts...)
b, err := bot.New("YOUR_BOT_TOKEN_FROM_BOTFATHER", opts...)
```

Full list of options you can find [here](options.go)
Expand All @@ -155,7 +161,7 @@ For your convenience, you can use `Message.Text` and `CallbackQuery.Data` handle
An example:

```go
b := bot.New("YOUR_BOT_TOKEN_FROM_BOTFATHER")
b, err := bot.New("YOUR_BOT_TOKEN_FROM_BOTFATHER")

b.RegisterHandler(bot.HandlerTypeMessageText, "/start", bot.MatchTypeExact, myStartHandler)

Expand Down
25 changes: 18 additions & 7 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
)

const (
defaultPollTimeout = time.Minute
defaultUpdatesChanCap = 64
defaultPollTimeout = time.Minute
defaultUpdatesChanCap = 64
defaultCheckInitTimeout = time.Second * 5
)

type HttpClient interface {
Expand All @@ -40,15 +41,16 @@ type Bot struct {
handlersMx *sync.RWMutex
handlers map[string]handler

client HttpClient
lastUpdateID int64
isDebug bool
client HttpClient
lastUpdateID int64
isDebug bool
checkInitTimeout time.Duration

updates chan *models.Update
}

// New creates new Bot instance
func New(token string, options ...Option) *Bot {
func New(token string, options ...Option) (*Bot, error) {
b := &Bot{
url: "https://api.telegram.org",
token: token,
Expand All @@ -62,6 +64,7 @@ func New(token string, options ...Option) *Bot {
},
defaultHandlerFunc: defaultHandler,
errorsHandler: defaultErrorsHandler,
checkInitTimeout: defaultCheckInitTimeout,

updates: make(chan *models.Update, defaultUpdatesChanCap),
}
Expand All @@ -70,7 +73,15 @@ func New(token string, options ...Option) *Bot {
o(b)
}

return b
ctx, cancel := context.WithTimeout(context.Background(), b.checkInitTimeout)
defer cancel()

_, err := b.GetMe(ctx)
if err != nil {
return nil, fmt.Errorf("error call getMe, %w", err)
}

return b, nil
}

func (b *Bot) StartWebhook(ctx context.Context) {
Expand Down
2 changes: 1 addition & 1 deletion examples/commands/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
bot.WithDefaultHandler(defaultHandler),
}

b := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
b, _ := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)

b.RegisterHandler(bot.HandlerTypeMessageText, "/hello", bot.MatchTypeExact, helloHandler)

Expand Down
7 changes: 3 additions & 4 deletions examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package main

import (
"context"
"os"
"os/signal"

"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
"os"
"os/signal"
)

// Send any text message to the bot after the bot has been started
Expand All @@ -19,7 +18,7 @@ func main() {
bot.WithDefaultHandler(handler),
}

b := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
b, _ := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
b.Start(ctx)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/echo_with_webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {
bot.WithDefaultHandler(handler),
}

b := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
b, _ := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)

b.SetWebhook(ctx, &bot.SetWebhookParams{
URL: "https://example.com/webhook",
Expand Down
2 changes: 1 addition & 1 deletion examples/getme/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func main() {
b := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"))
b, _ := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"))

user, _ := b.GetMe(context.Background())

Expand Down
2 changes: 1 addition & 1 deletion examples/inline_keyboard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {
bot.WithCallbackQueryDataHandler("button", bot.MatchTypePrefix, callbackHandler),
}

b := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
b, _ := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)

b.Start(ctx)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/inline_mode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
bot.WithDefaultHandler(handler),
}

b := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
b, _ := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)

b.Start(ctx)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/middleware/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
bot.WithDefaultHandler(handler),
}

b := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
b, _ := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)

b.Start(ctx)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/send_media_group/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
bot.WithDefaultHandler(handler),
}

b := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
b, _ := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)

b.Start(ctx)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/send_photo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {
bot.WithDefaultHandler(handler),
}

b := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
b, _ := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)

b.Start(ctx)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/send_photo_upload/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
bot.WithDefaultHandler(handler),
}

b := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
b, _ := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)

b.Start(ctx)
}
Expand Down
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import (
// Option is a function that configures a bot.
type Option func(b *Bot)

// WithCheckInitTimeout allows to redefine CheckInitTimeout
func WithCheckInitTimeout(timeout time.Duration) Option {
return func(b *Bot) {
b.checkInitTimeout = timeout
}
}

// WithMiddlewares allows to set middlewares for each incoming request
func WithMiddlewares(middlewares ...Middleware) Option {
return func(b *Bot) {
Expand Down

0 comments on commit 3b96799

Please sign in to comment.