telebot/api.go

176 lines
3.6 KiB
Go
Raw Normal View History

package telebot
import (
2015-07-02 18:39:39 +00:00
"bytes"
"encoding/json"
"fmt"
2015-07-02 18:39:39 +00:00
"io"
"io/ioutil"
2015-07-02 18:39:39 +00:00
"mime/multipart"
"net/http"
2015-07-02 18:39:39 +00:00
"os"
"path/filepath"
2015-06-26 16:12:54 +00:00
"strconv"
)
func sendCommand(method, token string, payload interface{}) ([]byte, error) {
url := fmt.Sprintf("https://api.telegram.org/bot%s/%s", token, method)
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(payload); err != nil {
return []byte{}, err
}
resp, err := http.Post(url, "application/json", &b)
if err != nil {
2015-06-26 19:20:08 +00:00
return []byte{}, err
}
2015-11-12 17:59:24 +00:00
resp.Close = true
defer resp.Body.Close()
2015-06-26 19:20:08 +00:00
json, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, err
}
return json, nil
}
func sendFile(method, token, name, path string, params map[string]string) ([]byte, error) {
2015-07-02 18:39:39 +00:00
file, err := os.Open(path)
if err != nil {
return []byte{}, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(name, filepath.Base(path))
if err != nil {
return []byte{}, err
}
if _, err = io.Copy(part, file); err != nil {
return []byte{}, err
}
for field, value := range params {
writer.WriteField(field, value)
2015-07-02 18:39:39 +00:00
}
if err = writer.Close(); err != nil {
return []byte{}, err
}
url := fmt.Sprintf("https://api.telegram.org/bot%s/%s", token, method)
req, err := http.NewRequest("POST", url, body)
if err != nil {
return []byte{}, err
}
req.Header.Add("Content-Type", writer.FormDataContentType())
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return []byte{}, err
}
if resp.StatusCode == http.StatusInternalServerError {
2015-11-16 10:22:37 +00:00
return []byte{}, fmt.Errorf("telegram: internal server error")
}
2015-07-02 18:39:39 +00:00
json, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, err
}
return json, nil
}
func embedSendOptions(params map[string]string, options *SendOptions) {
if options == nil {
return
}
if options.ReplyTo.ID != 0 {
params["reply_to_message_id"] = strconv.Itoa(options.ReplyTo.ID)
}
if options.DisableWebPagePreview {
params["disable_web_page_preview"] = "true"
}
if options.DisableNotification {
params["disable_notification"] = "true"
}
2015-09-14 03:15:16 +00:00
if options.ParseMode != ModeDefault {
params["parse_mode"] = string(options.ParseMode)
2015-09-14 03:15:16 +00:00
}
2015-11-16 10:22:37 +00:00
// Processing force_reply:
{
forceReply := options.ReplyMarkup.ForceReply
customKeyboard := (options.ReplyMarkup.CustomKeyboard != nil)
inlineKeyboard := options.ReplyMarkup.InlineKeyboard != nil
2015-11-16 10:22:37 +00:00
hiddenKeyboard := options.ReplyMarkup.HideCustomKeyboard
if forceReply || customKeyboard || hiddenKeyboard || inlineKeyboard {
2015-11-16 10:22:37 +00:00
replyMarkup, _ := json.Marshal(options.ReplyMarkup)
params["reply_markup"] = string(replyMarkup)
2015-11-16 10:22:37 +00:00
}
}
}
2015-07-03 18:54:40 +00:00
func getMe(token string) (User, error) {
meJSON, err := sendCommand("getMe", token, nil)
if err != nil {
return User{}, err
}
2015-07-06 13:52:50 +00:00
var botInfo struct {
Ok bool
Result User
Description string
}
2015-07-06 13:52:50 +00:00
err = json.Unmarshal(meJSON, &botInfo)
if err != nil {
return User{}, fmt.Errorf("telebot: invalid token")
}
2015-07-06 13:52:50 +00:00
if botInfo.Ok {
return botInfo.Result, nil
}
2015-07-04 10:12:19 +00:00
return User{}, fmt.Errorf("telebot: %s", botInfo.Description)
}
2015-11-16 10:22:37 +00:00
func getUpdates(token string, offset, timeout int) (upd []Update, err error) {
params := map[string]string{
"offset": strconv.Itoa(offset),
"timeout": strconv.Itoa(timeout),
}
2015-07-06 13:52:50 +00:00
updatesJSON, err := sendCommand("getUpdates", token, params)
if err != nil {
return
}
2015-07-06 13:52:50 +00:00
var updatesRecieved struct {
Ok bool
Result []Update
Description string
}
2015-07-06 13:52:50 +00:00
err = json.Unmarshal(updatesJSON, &updatesRecieved)
2015-06-26 16:12:54 +00:00
if err != nil {
return
2015-06-26 16:12:54 +00:00
}
2015-07-06 13:52:50 +00:00
if !updatesRecieved.Ok {
err = fmt.Errorf("telebot: %s", updatesRecieved.Description)
return
}
2015-11-16 10:22:37 +00:00
return updatesRecieved.Result, nil
}