diff --git a/CHANGELOG.md b/CHANGELOG.md index e5f591e..470b583 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 76951bd..8c05d3a 100644 --- a/README.md +++ b/README.md @@ -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) } @@ -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 @@ -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 @@ -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) @@ -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) diff --git a/bot.go b/bot.go index cbbd802..8abe4db 100644 --- a/bot.go +++ b/bot.go @@ -12,8 +12,9 @@ import ( ) const ( - defaultPollTimeout = time.Minute - defaultUpdatesChanCap = 64 + defaultPollTimeout = time.Minute + defaultUpdatesChanCap = 64 + defaultCheckInitTimeout = time.Second * 5 ) type HttpClient interface { @@ -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, @@ -62,6 +64,7 @@ func New(token string, options ...Option) *Bot { }, defaultHandlerFunc: defaultHandler, errorsHandler: defaultErrorsHandler, + checkInitTimeout: defaultCheckInitTimeout, updates: make(chan *models.Update, defaultUpdatesChanCap), } @@ -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) { diff --git a/examples/commands/main.go b/examples/commands/main.go index 345c1bc..b2c2448 100644 --- a/examples/commands/main.go +++ b/examples/commands/main.go @@ -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) diff --git a/examples/echo/main.go b/examples/echo/main.go index 78cd0ca..68525a2 100644 --- a/examples/echo/main.go +++ b/examples/echo/main.go @@ -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 @@ -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) } diff --git a/examples/echo_with_webhook/main.go b/examples/echo_with_webhook/main.go index 8f04797..9871dfa 100644 --- a/examples/echo_with_webhook/main.go +++ b/examples/echo_with_webhook/main.go @@ -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", diff --git a/examples/getme/main.go b/examples/getme/main.go index 49ea103..0787af8 100644 --- a/examples/getme/main.go +++ b/examples/getme/main.go @@ -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()) diff --git a/examples/inline_keyboard/main.go b/examples/inline_keyboard/main.go index d2270c8..08ba42f 100644 --- a/examples/inline_keyboard/main.go +++ b/examples/inline_keyboard/main.go @@ -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) } diff --git a/examples/inline_mode/main.go b/examples/inline_mode/main.go index 2d4132e..963028e 100644 --- a/examples/inline_mode/main.go +++ b/examples/inline_mode/main.go @@ -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) } diff --git a/examples/middleware/main.go b/examples/middleware/main.go index 7c71191..be136c4 100644 --- a/examples/middleware/main.go +++ b/examples/middleware/main.go @@ -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) } diff --git a/examples/send_media_group/main.go b/examples/send_media_group/main.go index f30e6e7..8b37eb3 100644 --- a/examples/send_media_group/main.go +++ b/examples/send_media_group/main.go @@ -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) } diff --git a/examples/send_photo/main.go b/examples/send_photo/main.go index f04f499..45ae627 100644 --- a/examples/send_photo/main.go +++ b/examples/send_photo/main.go @@ -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) } diff --git a/examples/send_photo_upload/main.go b/examples/send_photo_upload/main.go index 6284e8a..314648d 100644 --- a/examples/send_photo_upload/main.go +++ b/examples/send_photo_upload/main.go @@ -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) } diff --git a/options.go b/options.go index f863d28..ca2fa32 100644 --- a/options.go +++ b/options.go @@ -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) {