bot.go (view raw)
1// Package tgbotapi has bindings for interacting with the Telegram Bot API.
2package tgbotapi
3
4import (
5 "net/http"
6)
7
8// BotAPI has methods for interacting with all of Telegram's Bot API endpoints.
9type BotAPI struct {
10 Token string `json:"token"`
11 Debug bool `json:"debug"`
12 Self User `json:"-"`
13 Updates chan Update `json:"-"`
14 Client *http.Client `json:"-"`
15}
16
17// NewBotAPI creates a new BotAPI instance.
18// Requires a token, provided by @BotFather on Telegram
19func NewBotAPI(token string) (*BotAPI, error) {
20 return NewBotAPIWithClient(token, &http.Client{})
21}
22
23// NewBotAPIWithClient creates a new BotAPI instance passing an http.Client.
24// Requires a token, provided by @BotFather on Telegram
25func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) {
26 bot := &BotAPI{
27 Token: token,
28 Client: client,
29 }
30
31 self, err := bot.GetMe()
32 if err != nil {
33 return &BotAPI{}, err
34 }
35
36 bot.Self = self
37
38 return bot, nil
39}