telebot/api.go

165 lines
3.3 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-06-26 16:12:54 +00:00
"net/url"
2015-07-02 18:39:39 +00:00
"os"
"path/filepath"
2015-06-26 16:12:54 +00:00
"strconv"
)
2015-07-03 18:54:40 +00:00
func sendCommand(method string, token string, params url.Values) ([]byte, error) {
2015-06-26 19:20:08 +00:00
url := fmt.Sprintf("https://api.telegram.org/bot%s/%s?%s",
token, method, params.Encode())
2015-06-26 19:20:08 +00:00
resp, err := http.Get(url)
if err != nil {
2015-06-26 19:20:08 +00:00
return []byte{}, err
}
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
}
2015-07-03 18:54:40 +00:00
func sendFile(method, token, name, path string, params url.Values) ([]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, values := range params {
if len(values) > 0 {
writer.WriteField(field, values[0])
}
}
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 {
return []byte{}, fmt.Errorf("Telegram API returned 500 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 *url.Values, options *SendOptions) {
if params == nil || options == nil {
return
}
if options.ReplyTo.ID != 0 {
params.Set("reply_to_message_id", strconv.Itoa(options.ReplyTo.ID))
}
if options.DisableWebPagePreview {
params.Set("disable_web_page_preview", "true")
}
2015-09-14 03:15:16 +00:00
if options.ParseMode != ModeDefault {
params.Set("parse_mode", string(options.ParseMode))
}
2015-07-31 07:56:02 +00:00
// process reply_markup
if options.ReplyMarkup.ForceReply || options.ReplyMarkup.CustomKeyboard != nil || options.ReplyMarkup.HideCustomKeyboard {
replyMarkup, _ := json.Marshal(options.ReplyMarkup)
params.Set("reply_markup", string(replyMarkup))
}
}
2015-07-03 18:54:40 +00:00
func getMe(token string) (User, error) {
2015-07-06 13:52:50 +00:00
meJSON, err := sendCommand("getMe", token, url.Values{})
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 {
2015-07-06 16:58:54 +00:00
return User{}, AuthError{"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
2015-07-06 13:52:50 +00:00
return User{}, AuthError{botInfo.Description}
}
func getUpdates(token string, offset int, timeout int) (updates []Update, err error) {
2015-06-26 19:20:08 +00:00
params := url.Values{}
2015-07-02 18:39:39 +00:00
params.Set("offset", strconv.Itoa(offset))
params.Set("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 = FetchError{updatesRecieved.Description}
return
}
updates = updatesRecieved.Result
return
}