all repos — telegram-bot-api @ 2483f043976a025de9c97f53dfcaa4894c090b8b

Golang bindings for the Telegram Bot API

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, nil)
19}
20
21// NewBotAPI 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	if client == nil {
25		client = &http.Client{}
26	}
27
28	bot := &BotAPI{
29		Token:  token,
30		Client: client,
31	}
32
33	self, err := bot.GetMe()
34	if err != nil {
35		return &BotAPI{}, err
36	}
37
38	bot.Self = self
39
40	return bot, nil
41}