2015-06-26 08:53:20 +02:00
|
|
|
// Package tgbotapi has bindings for interacting with the Telegram Bot API.
|
2015-06-26 06:26:24 +02:00
|
|
|
package tgbotapi
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-07-27 00:16:45 +02:00
|
|
|
import "net/http"
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// BotAPI has methods for interacting with all of Telegram's Bot API endpoints.
|
|
|
|
type BotAPI struct {
|
2015-07-28 11:18:49 +02:00
|
|
|
Token string `json:"token"`
|
|
|
|
Debug bool `json:"debug"`
|
|
|
|
Self User `json:"-"`
|
|
|
|
Updates chan Update `json:"-"`
|
|
|
|
Client *http.Client `json:"-"`
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// NewBotAPI creates a new BotAPI instance.
|
2015-06-26 08:19:29 +02:00
|
|
|
// Requires a token, provided by @BotFather on Telegram
|
2015-07-28 13:00:31 +02:00
|
|
|
func NewBotAPI(token string) (*BotAPI, error) {
|
|
|
|
return NewBotAPIwithClient(token, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBotAPI creates a new BotAPI instance passing an http.Client.
|
|
|
|
// Requires a token, provided by @BotFather on Telegram
|
|
|
|
func NewBotAPIwithClient(token string, client *http.Client) (*BotAPI, error) {
|
2015-07-27 00:16:45 +02:00
|
|
|
if client == nil {
|
|
|
|
client = &http.Client{}
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
bot := &BotAPI{
|
2015-07-27 00:16:45 +02:00
|
|
|
Token: token,
|
2015-07-28 11:18:49 +02:00
|
|
|
Client: client,
|
2015-06-25 23:15:28 +02:00
|
|
|
}
|
2015-06-26 06:44:14 +02:00
|
|
|
|
|
|
|
self, err := bot.GetMe()
|
|
|
|
if err != nil {
|
2015-06-26 08:53:20 +02:00
|
|
|
return &BotAPI{}, err
|
2015-06-26 06:44:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bot.Self = self
|
|
|
|
|
2015-06-26 06:45:56 +02:00
|
|
|
return bot, nil
|
2015-06-25 23:15:28 +02:00
|
|
|
}
|