|
|
|
@ -12,7 +12,6 @@ import ( |
|
|
|
|
"net/http" |
|
|
|
|
"net/url" |
|
|
|
|
"os" |
|
|
|
|
"strconv" |
|
|
|
|
"strings" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
@ -25,8 +24,8 @@ type BotAPI struct { |
|
|
|
|
Debug bool `json:"debug"` |
|
|
|
|
Buffer int `json:"buffer"` |
|
|
|
|
|
|
|
|
|
Self User `json:"-"` |
|
|
|
|
Client *http.Client `json:"-"` |
|
|
|
|
Self User `json:"-"` |
|
|
|
|
Client *http.Client `json:"-"` |
|
|
|
|
shutdownChannel chan interface{} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -43,9 +42,9 @@ func NewBotAPI(token string) (*BotAPI, error) { |
|
|
|
|
// It requires a token, provided by @BotFather on Telegram.
|
|
|
|
|
func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) { |
|
|
|
|
bot := &BotAPI{ |
|
|
|
|
Token: token, |
|
|
|
|
Client: client, |
|
|
|
|
Buffer: 100, |
|
|
|
|
Token: token, |
|
|
|
|
Client: client, |
|
|
|
|
Buffer: 100, |
|
|
|
|
shutdownChannel: make(chan interface{}), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -59,11 +58,31 @@ func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) { |
|
|
|
|
return bot, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func buildParams(in Params) (out url.Values) { |
|
|
|
|
if in == nil { |
|
|
|
|
return url.Values{} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
out = url.Values{} |
|
|
|
|
|
|
|
|
|
for key, value := range in { |
|
|
|
|
out.Set(key, value) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MakeRequest makes a request to a specific endpoint with our token.
|
|
|
|
|
func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) { |
|
|
|
|
func (bot *BotAPI) MakeRequest(endpoint string, params Params) (APIResponse, error) { |
|
|
|
|
if bot.Debug { |
|
|
|
|
log.Printf("Endpoint: %s, params: %v\n", endpoint, params) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint) |
|
|
|
|
|
|
|
|
|
resp, err := bot.Client.PostForm(method, params) |
|
|
|
|
values := buildParams(params) |
|
|
|
|
|
|
|
|
|
resp, err := bot.Client.PostForm(method, values) |
|
|
|
|
if err != nil { |
|
|
|
|
return APIResponse{}, err |
|
|
|
|
} |
|
|
|
@ -76,15 +95,20 @@ func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if bot.Debug { |
|
|
|
|
log.Printf("%s resp: %s", endpoint, bytes) |
|
|
|
|
log.Printf("Endpoint: %s, response: %s\n", endpoint, string(bytes)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if !apiResp.Ok { |
|
|
|
|
parameters := ResponseParameters{} |
|
|
|
|
var parameters ResponseParameters |
|
|
|
|
|
|
|
|
|
if apiResp.Parameters != nil { |
|
|
|
|
parameters = *apiResp.Parameters |
|
|
|
|
} |
|
|
|
|
return apiResp, Error{apiResp.Description, parameters} |
|
|
|
|
|
|
|
|
|
return apiResp, Error{ |
|
|
|
|
Message: apiResp.Description, |
|
|
|
|
ResponseParameters: parameters, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return apiResp, nil |
|
|
|
@ -114,21 +138,6 @@ func (bot *BotAPI) decodeAPIResponse(responseBody io.Reader, resp *APIResponse) |
|
|
|
|
return data, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// makeMessageRequest makes a request to a method that returns a Message.
|
|
|
|
|
func (bot *BotAPI) makeMessageRequest(endpoint string, params url.Values) (Message, error) { |
|
|
|
|
resp, err := bot.MakeRequest(endpoint, params) |
|
|
|
|
if err != nil { |
|
|
|
|
return Message{}, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var message Message |
|
|
|
|
json.Unmarshal(resp.Result, &message) |
|
|
|
|
|
|
|
|
|
bot.debugLog(endpoint, params, message) |
|
|
|
|
|
|
|
|
|
return message, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// UploadFile makes a request to the API with a file.
|
|
|
|
|
//
|
|
|
|
|
// Requires the parameter to hold the file not be in the params.
|
|
|
|
@ -137,7 +146,7 @@ func (bot *BotAPI) makeMessageRequest(endpoint string, params url.Values) (Messa |
|
|
|
|
//
|
|
|
|
|
// Note that if your FileReader has a size set to -1, it will read
|
|
|
|
|
// the file into memory to calculate a size.
|
|
|
|
|
func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldname string, file interface{}) (APIResponse, error) { |
|
|
|
|
func (bot *BotAPI) UploadFile(endpoint string, params Params, fieldname string, file interface{}) (APIResponse, error) { |
|
|
|
|
ms := multipartstreamer.New() |
|
|
|
|
|
|
|
|
|
switch f := file.(type) { |
|
|
|
@ -186,6 +195,10 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna |
|
|
|
|
return APIResponse{}, errors.New(ErrBadFileType) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if bot.Debug { |
|
|
|
|
log.Printf("Endpoint: %s, fieldname: %s, params: %v, file: %T\n", endpoint, fieldname, params, file) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint) |
|
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", method, nil) |
|
|
|
@ -207,7 +220,7 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if bot.Debug { |
|
|
|
|
log.Println(string(bytes)) |
|
|
|
|
log.Printf("Endpoint: %s, response: %s\n", endpoint, string(bytes)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var apiResp APIResponse |
|
|
|
@ -249,11 +262,9 @@ func (bot *BotAPI) GetMe() (User, error) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var user User |
|
|
|
|
json.Unmarshal(resp.Result, &user) |
|
|
|
|
|
|
|
|
|
bot.debugLog("getMe", nil, user) |
|
|
|
|
err = json.Unmarshal(resp.Result, &user) |
|
|
|
|
|
|
|
|
|
return user, nil |
|
|
|
|
return user, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// IsMessageToMe returns true if message directed to this bot.
|
|
|
|
@ -263,90 +274,52 @@ func (bot *BotAPI) IsMessageToMe(message Message) bool { |
|
|
|
|
return strings.Contains(message.Text, "@"+bot.Self.UserName) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Send will send a Chattable item to Telegram.
|
|
|
|
|
//
|
|
|
|
|
// It requires the Chattable to send.
|
|
|
|
|
func (bot *BotAPI) Send(c Chattable) (Message, error) { |
|
|
|
|
switch c.(type) { |
|
|
|
|
case Fileable: |
|
|
|
|
return bot.sendFile(c.(Fileable)) |
|
|
|
|
default: |
|
|
|
|
return bot.sendChattable(c) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// debugLog checks if the bot is currently running in debug mode, and if
|
|
|
|
|
// so will display information about the request and response in the
|
|
|
|
|
// debug log.
|
|
|
|
|
func (bot *BotAPI) debugLog(context string, v url.Values, message interface{}) { |
|
|
|
|
if bot.Debug { |
|
|
|
|
log.Printf("%s req : %+v\n", context, v) |
|
|
|
|
log.Printf("%s resp: %+v\n", context, message) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// sendExisting will send a Message with an existing file to Telegram.
|
|
|
|
|
func (bot *BotAPI) sendExisting(method string, config Fileable) (Message, error) { |
|
|
|
|
v, err := config.values() |
|
|
|
|
|
|
|
|
|
// Request sends a Chattable to Telegram, and returns the APIResponse.
|
|
|
|
|
func (bot *BotAPI) Request(c Chattable) (APIResponse, error) { |
|
|
|
|
params, err := c.params() |
|
|
|
|
if err != nil { |
|
|
|
|
return Message{}, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
message, err := bot.makeMessageRequest(method, v) |
|
|
|
|
if err != nil { |
|
|
|
|
return Message{}, err |
|
|
|
|
return APIResponse{}, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return message, nil |
|
|
|
|
} |
|
|
|
|
switch t := c.(type) { |
|
|
|
|
case Fileable: |
|
|
|
|
if t.useExistingFile() { |
|
|
|
|
return bot.MakeRequest(t.method(), params) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// uploadAndSend will send a Message with a new file to Telegram.
|
|
|
|
|
func (bot *BotAPI) uploadAndSend(method string, config Fileable) (Message, error) { |
|
|
|
|
params, err := config.params() |
|
|
|
|
if err != nil { |
|
|
|
|
return Message{}, err |
|
|
|
|
return bot.UploadFile(t.method(), params, t.name(), t.getFile()) |
|
|
|
|
default: |
|
|
|
|
return bot.MakeRequest(c.method(), params) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
file := config.getFile() |
|
|
|
|
|
|
|
|
|
resp, err := bot.UploadFile(method, params, config.name(), file) |
|
|
|
|
// Send will send a Chattable item to Telegram and provides the
|
|
|
|
|
// returned Message.
|
|
|
|
|
func (bot *BotAPI) Send(c Chattable) (Message, error) { |
|
|
|
|
resp, err := bot.Request(c) |
|
|
|
|
if err != nil { |
|
|
|
|
return Message{}, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var message Message |
|
|
|
|
json.Unmarshal(resp.Result, &message) |
|
|
|
|
|
|
|
|
|
bot.debugLog(method, nil, message) |
|
|
|
|
err = json.Unmarshal(resp.Result, &message) |
|
|
|
|
|
|
|
|
|
return message, nil |
|
|
|
|
return message, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// sendFile determines if the file is using an existing file or uploading
|
|
|
|
|
// a new file, then sends it as needed.
|
|
|
|
|
func (bot *BotAPI) sendFile(config Fileable) (Message, error) { |
|
|
|
|
if config.useExistingFile() { |
|
|
|
|
return bot.sendExisting(config.method(), config) |
|
|
|
|
} |
|
|
|
|
// SendMediaGroup sends a media group and returns the resulting messages.
|
|
|
|
|
func (bot *BotAPI) SendMediaGroup(config MediaGroupConfig) ([]Message, error) { |
|
|
|
|
params, _ := config.params() |
|
|
|
|
|
|
|
|
|
return bot.uploadAndSend(config.method(), config) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// sendChattable sends a Chattable.
|
|
|
|
|
func (bot *BotAPI) sendChattable(config Chattable) (Message, error) { |
|
|
|
|
v, err := config.values() |
|
|
|
|
resp, err := bot.MakeRequest(config.method(), params) |
|
|
|
|
if err != nil { |
|
|
|
|
return Message{}, err |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
message, err := bot.makeMessageRequest(config.method(), v) |
|
|
|
|
var messages []Message |
|
|
|
|
err = json.Unmarshal(resp.Result, &messages) |
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
return Message{}, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return message, nil |
|
|
|
|
return messages, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetUserProfilePhotos gets a user's profile photos.
|
|
|
|
@ -354,46 +327,34 @@ func (bot *BotAPI) sendChattable(config Chattable) (Message, error) { |
|
|
|
|
// It requires UserID.
|
|
|
|
|
// Offset and Limit are optional.
|
|
|
|
|
func (bot *BotAPI) GetUserProfilePhotos(config UserProfilePhotosConfig) (UserProfilePhotos, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID)) |
|
|
|
|
if config.Offset != 0 { |
|
|
|
|
v.Add("offset", strconv.Itoa(config.Offset)) |
|
|
|
|
} |
|
|
|
|
if config.Limit != 0 { |
|
|
|
|
v.Add("limit", strconv.Itoa(config.Limit)) |
|
|
|
|
} |
|
|
|
|
params, _ := config.params() |
|
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("getUserProfilePhotos", v) |
|
|
|
|
resp, err := bot.MakeRequest(config.method(), params) |
|
|
|
|
if err != nil { |
|
|
|
|
return UserProfilePhotos{}, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var profilePhotos UserProfilePhotos |
|
|
|
|
json.Unmarshal(resp.Result, &profilePhotos) |
|
|
|
|
|
|
|
|
|
bot.debugLog("GetUserProfilePhoto", v, profilePhotos) |
|
|
|
|
err = json.Unmarshal(resp.Result, &profilePhotos) |
|
|
|
|
|
|
|
|
|
return profilePhotos, nil |
|
|
|
|
return profilePhotos, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetFile returns a File which can download a file from Telegram.
|
|
|
|
|
//
|
|
|
|
|
// Requires FileID.
|
|
|
|
|
func (bot *BotAPI) GetFile(config FileConfig) (File, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
v.Add("file_id", config.FileID) |
|
|
|
|
params, _ := config.params() |
|
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("getFile", v) |
|
|
|
|
resp, err := bot.MakeRequest(config.method(), params) |
|
|
|
|
if err != nil { |
|
|
|
|
return File{}, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var file File |
|
|
|
|
json.Unmarshal(resp.Result, &file) |
|
|
|
|
err = json.Unmarshal(resp.Result, &file) |
|
|
|
|
|
|
|
|
|
bot.debugLog("GetFile", v, file) |
|
|
|
|
|
|
|
|
|
return file, nil |
|
|
|
|
return file, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetUpdates fetches updates.
|
|
|
|
@ -404,71 +365,23 @@ func (bot *BotAPI) GetFile(config FileConfig) (File, error) { |
|
|
|
|
// Set Timeout to a large number to reduce requests so you can get updates
|
|
|
|
|
// instantly instead of having to wait between requests.
|
|
|
|
|
func (bot *BotAPI) GetUpdates(config UpdateConfig) ([]Update, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
if config.Offset != 0 { |
|
|
|
|
v.Add("offset", strconv.Itoa(config.Offset)) |
|
|
|
|
} |
|
|
|
|
if config.Limit > 0 { |
|
|
|
|
v.Add("limit", strconv.Itoa(config.Limit)) |
|
|
|
|
} |
|
|
|
|
if config.Timeout > 0 { |
|
|
|
|
v.Add("timeout", strconv.Itoa(config.Timeout)) |
|
|
|
|
} |
|
|
|
|
params, _ := config.params() |
|
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("getUpdates", v) |
|
|
|
|
resp, err := bot.MakeRequest(config.method(), params) |
|
|
|
|
if err != nil { |
|
|
|
|
return []Update{}, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var updates []Update |
|
|
|
|
json.Unmarshal(resp.Result, &updates) |
|
|
|
|
|
|
|
|
|
bot.debugLog("getUpdates", v, updates) |
|
|
|
|
|
|
|
|
|
return updates, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// RemoveWebhook unsets the webhook.
|
|
|
|
|
func (bot *BotAPI) RemoveWebhook() (APIResponse, error) { |
|
|
|
|
return bot.MakeRequest("setWebhook", url.Values{}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// SetWebhook sets a webhook.
|
|
|
|
|
//
|
|
|
|
|
// If this is set, GetUpdates will not get any data!
|
|
|
|
|
//
|
|
|
|
|
// If you do not have a legitimate TLS certificate, you need to include
|
|
|
|
|
// your self signed certificate with the config.
|
|
|
|
|
func (bot *BotAPI) SetWebhook(config WebhookConfig) (APIResponse, error) { |
|
|
|
|
|
|
|
|
|
if config.Certificate == nil { |
|
|
|
|
v := url.Values{} |
|
|
|
|
v.Add("url", config.URL.String()) |
|
|
|
|
if config.MaxConnections != 0 { |
|
|
|
|
v.Add("max_connections", strconv.Itoa(config.MaxConnections)) |
|
|
|
|
} |
|
|
|
|
err = json.Unmarshal(resp.Result, &updates) |
|
|
|
|
|
|
|
|
|
return bot.MakeRequest("setWebhook", v) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
params := make(map[string]string) |
|
|
|
|
params["url"] = config.URL.String() |
|
|
|
|
if config.MaxConnections != 0 { |
|
|
|
|
params["max_connections"] = strconv.Itoa(config.MaxConnections) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
resp, err := bot.UploadFile("setWebhook", params, "certificate", config.Certificate) |
|
|
|
|
if err != nil { |
|
|
|
|
return APIResponse{}, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return resp, nil |
|
|
|
|
return updates, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetWebhookInfo allows you to fetch information about a webhook and if
|
|
|
|
|
// one currently is set, along with pending update count and error messages.
|
|
|
|
|
func (bot *BotAPI) GetWebhookInfo() (WebhookInfo, error) { |
|
|
|
|
resp, err := bot.MakeRequest("getWebhookInfo", url.Values{}) |
|
|
|
|
resp, err := bot.MakeRequest("getWebhookInfo", nil) |
|
|
|
|
if err != nil { |
|
|
|
|
return WebhookInfo{}, err |
|
|
|
|
} |
|
|
|
@ -480,7 +393,7 @@ func (bot *BotAPI) GetWebhookInfo() (WebhookInfo, error) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetUpdatesChan starts and returns a channel for getting updates.
|
|
|
|
|
func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) { |
|
|
|
|
func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) UpdatesChannel { |
|
|
|
|
ch := make(chan Update, bot.Buffer) |
|
|
|
|
|
|
|
|
|
go func() { |
|
|
|
@ -490,7 +403,7 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) { |
|
|
|
|
return |
|
|
|
|
default: |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updates, err := bot.GetUpdates(config) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Println(err) |
|
|
|
@ -509,7 +422,7 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) { |
|
|
|
|
} |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
return ch, nil |
|
|
|
|
return ch |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// StopReceivingUpdates stops the go routine which receives updates
|
|
|
|
@ -536,96 +449,11 @@ func (bot *BotAPI) ListenForWebhook(pattern string) UpdatesChannel { |
|
|
|
|
return ch |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// AnswerInlineQuery sends a response to an inline query.
|
|
|
|
|
//
|
|
|
|
|
// Note that you must respond to an inline query within 30 seconds.
|
|
|
|
|
func (bot *BotAPI) AnswerInlineQuery(config InlineConfig) (APIResponse, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
|
|
|
|
|
v.Add("inline_query_id", config.InlineQueryID) |
|
|
|
|
v.Add("cache_time", strconv.Itoa(config.CacheTime)) |
|
|
|
|
v.Add("is_personal", strconv.FormatBool(config.IsPersonal)) |
|
|
|
|
v.Add("next_offset", config.NextOffset) |
|
|
|
|
data, err := json.Marshal(config.Results) |
|
|
|
|
if err != nil { |
|
|
|
|
return APIResponse{}, err |
|
|
|
|
} |
|
|
|
|
v.Add("results", string(data)) |
|
|
|
|
v.Add("switch_pm_text", config.SwitchPMText) |
|
|
|
|
v.Add("switch_pm_parameter", config.SwitchPMParameter) |
|
|
|
|
|
|
|
|
|
bot.debugLog("answerInlineQuery", v, nil) |
|
|
|
|
|
|
|
|
|
return bot.MakeRequest("answerInlineQuery", v) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// AnswerCallbackQuery sends a response to an inline query callback.
|
|
|
|
|
func (bot *BotAPI) AnswerCallbackQuery(config CallbackConfig) (APIResponse, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
|
|
|
|
|
v.Add("callback_query_id", config.CallbackQueryID) |
|
|
|
|
if config.Text != "" { |
|
|
|
|
v.Add("text", config.Text) |
|
|
|
|
} |
|
|
|
|
v.Add("show_alert", strconv.FormatBool(config.ShowAlert)) |
|
|
|
|
if config.URL != "" { |
|
|
|
|
v.Add("url", config.URL) |
|
|
|
|
} |
|
|
|
|
v.Add("cache_time", strconv.Itoa(config.CacheTime)) |
|
|
|
|
|
|
|
|
|
bot.debugLog("answerCallbackQuery", v, nil) |
|
|
|
|
|
|
|
|
|
return bot.MakeRequest("answerCallbackQuery", v) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// KickChatMember kicks a user from a chat. Note that this only will work
|
|
|
|
|
// in supergroups, and requires the bot to be an admin. Also note they
|
|
|
|
|
// will be unable to rejoin until they are unbanned.
|
|
|
|
|
func (bot *BotAPI) KickChatMember(config KickChatMemberConfig) (APIResponse, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
|
|
|
|
|
if config.SuperGroupUsername == "" { |
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) |
|
|
|
|
} else { |
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername) |
|
|
|
|
} |
|
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID)) |
|
|
|
|
|
|
|
|
|
if config.UntilDate != 0 { |
|
|
|
|
v.Add("until_date", strconv.FormatInt(config.UntilDate, 10)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bot.debugLog("kickChatMember", v, nil) |
|
|
|
|
|
|
|
|
|
return bot.MakeRequest("kickChatMember", v) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// LeaveChat makes the bot leave the chat.
|
|
|
|
|
func (bot *BotAPI) LeaveChat(config ChatConfig) (APIResponse, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
|
|
|
|
|
if config.SuperGroupUsername == "" { |
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) |
|
|
|
|
} else { |
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bot.debugLog("leaveChat", v, nil) |
|
|
|
|
|
|
|
|
|
return bot.MakeRequest("leaveChat", v) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetChat gets information about a chat.
|
|
|
|
|
func (bot *BotAPI) GetChat(config ChatConfig) (Chat, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
|
|
|
|
|
if config.SuperGroupUsername == "" { |
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) |
|
|
|
|
} else { |
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername) |
|
|
|
|
} |
|
|
|
|
func (bot *BotAPI) GetChat(config ChatInfoConfig) (Chat, error) { |
|
|
|
|
params, _ := config.params() |
|
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("getChat", v) |
|
|
|
|
resp, err := bot.MakeRequest(config.method(), params) |
|
|
|
|
if err != nil { |
|
|
|
|
return Chat{}, err |
|
|
|
|
} |
|
|
|
@ -633,8 +461,6 @@ func (bot *BotAPI) GetChat(config ChatConfig) (Chat, error) { |
|
|
|
|
var chat Chat |
|
|
|
|
err = json.Unmarshal(resp.Result, &chat) |
|
|
|
|
|
|
|
|
|
bot.debugLog("getChat", v, chat) |
|
|
|
|
|
|
|
|
|
return chat, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -642,16 +468,10 @@ func (bot *BotAPI) GetChat(config ChatConfig) (Chat, error) { |
|
|
|
|
//
|
|
|
|
|
// If none have been appointed, only the creator will be returned.
|
|
|
|
|
// Bots are not shown, even if they are an administrator.
|
|
|
|
|
func (bot *BotAPI) GetChatAdministrators(config ChatConfig) ([]ChatMember, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
|
|
|
|
|
if config.SuperGroupUsername == "" { |
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) |
|
|
|
|
} else { |
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername) |
|
|
|
|
} |
|
|
|
|
func (bot *BotAPI) GetChatAdministrators(config ChatAdministratorsConfig) ([]ChatMember, error) { |
|
|
|
|
params, _ := config.params() |
|
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("getChatAdministrators", v) |
|
|
|
|
resp, err := bot.MakeRequest(config.method(), params) |
|
|
|
|
if err != nil { |
|
|
|
|
return []ChatMember{}, err |
|
|
|
|
} |
|
|
|
@ -659,22 +479,14 @@ func (bot *BotAPI) GetChatAdministrators(config ChatConfig) ([]ChatMember, error |
|
|
|
|
var members []ChatMember |
|
|
|
|
err = json.Unmarshal(resp.Result, &members) |
|
|
|
|
|
|
|
|
|
bot.debugLog("getChatAdministrators", v, members) |
|
|
|
|
|
|
|
|
|
return members, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetChatMembersCount gets the number of users in a chat.
|
|
|
|
|
func (bot *BotAPI) GetChatMembersCount(config ChatConfig) (int, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
func (bot *BotAPI) GetChatMembersCount(config ChatMemberCountConfig) (int, error) { |
|
|
|
|
params, _ := config.params() |
|
|
|
|
|
|
|
|
|
if config.SuperGroupUsername == "" { |
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) |
|
|
|
|
} else { |
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("getChatMembersCount", v) |
|
|
|
|
resp, err := bot.MakeRequest(config.method(), params) |
|
|
|
|
if err != nil { |
|
|
|
|
return -1, err |
|
|
|
|
} |
|
|
|
@ -682,23 +494,14 @@ func (bot *BotAPI) GetChatMembersCount(config ChatConfig) (int, error) { |
|
|
|
|
var count int |
|
|
|
|
err = json.Unmarshal(resp.Result, &count) |
|
|
|
|
|
|
|
|
|
bot.debugLog("getChatMembersCount", v, count) |
|
|
|
|
|
|
|
|
|
return count, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetChatMember gets a specific chat member.
|
|
|
|
|
func (bot *BotAPI) GetChatMember(config ChatConfigWithUser) (ChatMember, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
func (bot *BotAPI) GetChatMember(config GetChatMemberConfig) (ChatMember, error) { |
|
|
|
|
params, _ := config.params() |
|
|
|
|
|
|
|
|
|
if config.SuperGroupUsername == "" { |
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) |
|
|
|
|
} else { |
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername) |
|
|
|
|
} |
|
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID)) |
|
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("getChatMember", v) |
|
|
|
|
resp, err := bot.MakeRequest(config.method(), params) |
|
|
|
|
if err != nil { |
|
|
|
|
return ChatMember{}, err |
|
|
|
|
} |
|
|
|
@ -706,115 +509,14 @@ func (bot *BotAPI) GetChatMember(config ChatConfigWithUser) (ChatMember, error) |
|
|
|
|
var member ChatMember |
|
|
|
|
err = json.Unmarshal(resp.Result, &member) |
|
|
|
|
|
|
|
|
|
bot.debugLog("getChatMember", v, member) |
|
|
|
|
|
|
|
|
|
return member, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// UnbanChatMember unbans a user from a chat. Note that this only will work
|
|
|
|
|
// in supergroups and channels, and requires the bot to be an admin.
|
|
|
|
|
func (bot *BotAPI) UnbanChatMember(config ChatMemberConfig) (APIResponse, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
|
|
|
|
|
if config.SuperGroupUsername != "" { |
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername) |
|
|
|
|
} else if config.ChannelUsername != "" { |
|
|
|
|
v.Add("chat_id", config.ChannelUsername) |
|
|
|
|
} else { |
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) |
|
|
|
|
} |
|
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID)) |
|
|
|
|
|
|
|
|
|
bot.debugLog("unbanChatMember", v, nil) |
|
|
|
|
|
|
|
|
|
return bot.MakeRequest("unbanChatMember", v) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// RestrictChatMember to restrict a user in a supergroup. The bot must be an
|
|
|
|
|
//administrator in the supergroup for this to work and must have the
|
|
|
|
|
//appropriate admin rights. Pass True for all boolean parameters to lift
|
|
|
|
|
//restrictions from a user. Returns True on success.
|
|
|
|
|
func (bot *BotAPI) RestrictChatMember(config RestrictChatMemberConfig) (APIResponse, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
|
|
|
|
|
if config.SuperGroupUsername != "" { |
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername) |
|
|
|
|
} else if config.ChannelUsername != "" { |
|
|
|
|
v.Add("chat_id", config.ChannelUsername) |
|
|
|
|
} else { |
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) |
|
|
|
|
} |
|
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID)) |
|
|
|
|
|
|
|
|
|
if config.CanSendMessages != nil { |
|
|
|
|
v.Add("can_send_messages", strconv.FormatBool(*config.CanSendMessages)) |
|
|
|
|
} |
|
|
|
|
if config.CanSendMediaMessages != nil { |
|
|
|
|
v.Add("can_send_media_messages", strconv.FormatBool(*config.CanSendMediaMessages)) |
|
|
|
|
} |
|
|
|
|
if config.CanSendOtherMessages != nil { |
|
|
|
|
v.Add("can_send_other_messages", strconv.FormatBool(*config.CanSendOtherMessages)) |
|
|
|
|
} |
|
|
|
|
if config.CanAddWebPagePreviews != nil { |
|
|
|
|
v.Add("can_add_web_page_previews", strconv.FormatBool(*config.CanAddWebPagePreviews)) |
|
|
|
|
} |
|
|
|
|
if config.UntilDate != 0 { |
|
|
|
|
v.Add("until_date", strconv.FormatInt(config.UntilDate, 10)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bot.debugLog("restrictChatMember", v, nil) |
|
|
|
|
|
|
|
|
|
return bot.MakeRequest("restrictChatMember", v) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// PromoteChatMember add admin rights to user
|
|
|
|
|
func (bot *BotAPI) PromoteChatMember(config PromoteChatMemberConfig) (APIResponse, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
|
|
|
|
|
if config.SuperGroupUsername != "" { |
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername) |
|
|
|
|
} else if config.ChannelUsername != "" { |
|
|
|
|
v.Add("chat_id", config.ChannelUsername) |
|
|
|
|
} else { |
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) |
|
|
|
|
} |
|
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID)) |
|
|
|
|
|
|
|
|
|
if config.CanChangeInfo != nil { |
|
|
|
|
v.Add("can_change_info", strconv.FormatBool(*config.CanChangeInfo)) |
|
|
|
|
} |
|
|
|
|
if config.CanPostMessages != nil { |
|
|
|
|
v.Add("can_post_messages", strconv.FormatBool(*config.CanPostMessages)) |
|
|
|
|
} |
|
|
|
|
if config.CanEditMessages != nil { |
|
|
|
|
v.Add("can_edit_messages", strconv.FormatBool(*config.CanEditMessages)) |
|
|
|
|
} |
|
|
|
|
if config.CanDeleteMessages != nil { |
|
|
|
|
v.Add("can_delete_messages", strconv.FormatBool(*config.CanDeleteMessages)) |
|
|
|
|
} |
|
|
|
|
if config.CanInviteUsers != nil { |
|
|
|
|
v.Add("can_invite_users", strconv.FormatBool(*config.CanInviteUsers)) |
|
|
|
|
} |
|
|
|
|
if config.CanRestrictMembers != nil { |
|
|
|
|
v.Add("can_restrict_members", strconv.FormatBool(*config.CanRestrictMembers)) |
|
|
|
|
} |
|
|
|
|
if config.CanPinMessages != nil { |
|
|
|
|
v.Add("can_pin_messages", strconv.FormatBool(*config.CanPinMessages)) |
|
|
|
|
} |
|
|
|
|
if config.CanPromoteMembers != nil { |
|
|
|
|
v.Add("can_promote_members", strconv.FormatBool(*config.CanPromoteMembers)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bot.debugLog("promoteChatMember", v, nil) |
|
|
|
|
|
|
|
|
|
return bot.MakeRequest("promoteChatMember", v) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetGameHighScores allows you to get the high scores for a game.
|
|
|
|
|
func (bot *BotAPI) GetGameHighScores(config GetGameHighScoresConfig) ([]GameHighScore, error) { |
|
|
|
|
v, _ := config.values() |
|
|
|
|
params, _ := config.params() |
|
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest(config.method(), v) |
|
|
|
|
resp, err := bot.MakeRequest(config.method(), params) |
|
|
|
|
if err != nil { |
|
|
|
|
return []GameHighScore{}, err |
|
|
|
|
} |
|
|
|
@ -825,65 +527,11 @@ func (bot *BotAPI) GetGameHighScores(config GetGameHighScoresConfig) ([]GameHigh |
|
|
|
|
return highScores, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// AnswerShippingQuery allows you to reply to Update with shipping_query parameter.
|
|
|
|
|
func (bot *BotAPI) AnswerShippingQuery(config ShippingConfig) (APIResponse, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
|
|
|
|
|
v.Add("shipping_query_id", config.ShippingQueryID) |
|
|
|
|
v.Add("ok", strconv.FormatBool(config.OK)) |
|
|
|
|
if config.OK == true { |
|
|
|
|
data, err := json.Marshal(config.ShippingOptions) |
|
|
|
|
if err != nil { |
|
|
|
|
return APIResponse{}, err |
|
|
|
|
} |
|
|
|
|
v.Add("shipping_options", string(data)) |
|
|
|
|
} else { |
|
|
|
|
v.Add("error_message", config.ErrorMessage) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bot.debugLog("answerShippingQuery", v, nil) |
|
|
|
|
|
|
|
|
|
return bot.MakeRequest("answerShippingQuery", v) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// AnswerPreCheckoutQuery allows you to reply to Update with pre_checkout_query.
|
|
|
|
|
func (bot *BotAPI) AnswerPreCheckoutQuery(config PreCheckoutConfig) (APIResponse, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
|
|
|
|
|
v.Add("pre_checkout_query_id", config.PreCheckoutQueryID) |
|
|
|
|
v.Add("ok", strconv.FormatBool(config.OK)) |
|
|
|
|
if config.OK != true { |
|
|
|
|
v.Add("error", config.ErrorMessage) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bot.debugLog("answerPreCheckoutQuery", v, nil) |
|
|
|
|
|
|
|
|
|
return bot.MakeRequest("answerPreCheckoutQuery", v) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// DeleteMessage deletes a message in a chat
|
|
|
|
|
func (bot *BotAPI) DeleteMessage(config DeleteMessageConfig) (APIResponse, error) { |
|
|
|
|
v, err := config.values() |
|
|
|
|
if err != nil { |
|
|
|
|
return APIResponse{}, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil) |
|
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetInviteLink get InviteLink for a chat
|
|
|
|
|
func (bot *BotAPI) GetInviteLink(config ChatConfig) (string, error) { |
|
|
|
|
v := url.Values{} |
|
|
|
|
|
|
|
|
|
if config.SuperGroupUsername == "" { |
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) |
|
|
|
|
} else { |
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername) |
|
|
|
|
} |
|
|
|
|
func (bot *BotAPI) GetInviteLink(config ChatInviteLinkConfig) (string, error) { |
|
|
|
|
params, _ := config.params() |
|
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("exportChatInviteLink", v) |
|
|
|
|
resp, err := bot.MakeRequest(config.method(), params) |
|
|
|
|
if err != nil { |
|
|
|
|
return "", err |
|
|
|
|
} |
|
|
|
@ -894,74 +542,35 @@ func (bot *BotAPI) GetInviteLink(config ChatConfig) (string, error) { |
|
|
|
|
return inviteLink, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// PinChatMessage pin message in supergroup
|
|
|
|
|
func (bot *BotAPI) PinChatMessage(config PinChatMessageConfig) (APIResponse, error) { |
|
|
|
|
v, err := config.values() |
|
|
|
|
if err != nil { |
|
|
|
|
return APIResponse{}, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil) |
|
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// UnpinChatMessage unpin message in supergroup
|
|
|
|
|
func (bot *BotAPI) UnpinChatMessage(config UnpinChatMessageConfig) (APIResponse, error) { |
|
|
|
|
v, err := config.values() |
|
|
|
|
if err != nil { |
|
|
|
|
return APIResponse{}, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil) |
|
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v) |
|
|
|
|
} |
|
|
|
|
// GetStickerSet returns a StickerSet.
|
|
|
|
|
func (bot *BotAPI) GetStickerSet(config GetStickerSetConfig) (StickerSet, error) { |
|
|
|
|
params, _ := config.params() |
|
|
|
|
|
|
|
|
|
// SetChatTitle change title of chat.
|
|
|
|
|
func (bot *BotAPI) SetChatTitle(config SetChatTitleConfig) (APIResponse, error) { |
|
|
|
|
v, err := config.values() |
|
|
|
|
resp, err := bot.MakeRequest(config.method(), params) |
|
|
|
|
if err != nil { |
|
|
|
|
return APIResponse{}, err |
|
|
|
|
return StickerSet{}, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil) |
|
|
|
|
var stickers StickerSet |
|
|
|
|
err = json.Unmarshal(resp.Result, &stickers) |
|
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v) |
|
|
|
|
return stickers, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// SetChatDescription change description of chat.
|
|
|
|
|
func (bot *BotAPI) SetChatDescription(config SetChatDescriptionConfig) (APIResponse, error) { |
|
|
|
|
v, err := config.values() |
|
|
|
|
if err != nil { |
|
|
|
|
return APIResponse{}, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil) |
|
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// SetChatPhoto change photo of chat.
|
|
|
|
|
func (bot *BotAPI) SetChatPhoto(config SetChatPhotoConfig) (APIResponse, error) { |
|
|
|
|
// StopPoll stops a poll and returns the result.
|
|
|
|
|
func (bot *BotAPI) StopPoll(config StopPollConfig) (Poll, error) { |
|
|
|
|
params, err := config.params() |
|
|
|
|
if err != nil { |
|
|
|
|
return APIResponse{}, err |
|
|
|
|
return Poll{}, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
file := config.getFile() |
|
|
|
|
|
|
|
|
|
return bot.UploadFile(config.method(), params, config.name(), file) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// DeleteChatPhoto delete photo of chat.
|
|
|
|
|
func (bot *BotAPI) DeleteChatPhoto(config DeleteChatPhotoConfig) (APIResponse, error) { |
|
|
|
|
v, err := config.values() |
|
|
|
|
resp, err := bot.MakeRequest(config.method(), params) |
|
|
|
|
if err != nil { |
|
|
|
|
return APIResponse{}, err |
|
|
|
|
return Poll{}, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil) |
|
|
|
|
var poll Poll |
|
|
|
|
err = json.Unmarshal(resp.Result, &poll) |
|
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v) |
|
|
|
|
return poll, err |
|
|
|
|
} |
|
|
|
|