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