This repository has been archived on 2023-07-05. You can view files and clone it, but cannot push or open issues or pull requests.
clessy/broker/telegram.go

221 lines
5.8 KiB
Go
Raw Normal View History

2016-02-08 16:52:13 +00:00
package main
import (
2016-02-20 21:28:30 +00:00
"bytes"
2016-02-20 20:40:24 +00:00
"encoding/base64"
2016-02-08 17:05:46 +00:00
"encoding/json"
"errors"
2016-02-20 20:40:24 +00:00
"fmt"
"io/ioutil"
2016-02-08 16:52:13 +00:00
"log"
2016-02-20 21:28:30 +00:00
"mime/multipart"
2016-02-20 20:40:24 +00:00
"net"
2016-02-08 16:52:13 +00:00
"net/http"
"net/url"
2016-02-09 10:33:38 +00:00
"strconv"
2016-02-09 14:55:37 +00:00
"github.com/hamcha/clessy/tg"
2016-02-08 16:52:13 +00:00
)
// APIEndpoint is Telegram's current Bot API base url endpoint
2016-02-08 16:52:13 +00:00
const APIEndpoint = "https://api.telegram.org/"
// Telegram is the API client for the Telegram Bot API
2016-02-08 16:52:13 +00:00
type Telegram struct {
Token string
}
// mkAPI creates a Telegram instance from a Bot API token
2016-02-08 16:52:13 +00:00
func mkAPI(token string) *Telegram {
tg := new(Telegram)
tg.Token = token
return tg
}
// SetWebhook sets the webhook address so that Telegram knows where to send updates
2016-02-09 10:33:38 +00:00
func (t Telegram) SetWebhook(webhook string) {
2016-02-08 16:52:13 +00:00
resp, err := http.PostForm(t.apiURL("setWebhook"), url.Values{"url": {webhook}})
2016-02-20 21:28:30 +00:00
if !checkerr("SetWebhook/http.PostForm", err) {
2016-02-08 16:52:13 +00:00
defer resp.Body.Close()
2016-02-08 17:05:46 +00:00
var result tg.APIResponse
err = json.NewDecoder(resp.Body).Decode(&result)
2016-02-08 16:52:13 +00:00
if err != nil {
2016-02-09 10:33:38 +00:00
log.Println("[SetWebhook] Could not read reply: " + err.Error())
2016-02-08 17:05:46 +00:00
return
2016-02-08 16:52:13 +00:00
}
if result.Ok {
log.Println("Webhook successfully set!")
} else {
2016-02-09 10:33:38 +00:00
log.Printf("[SetWebhook] Error setting webhook (errcode %d): %s\n", *(result.ErrCode), *(result.Description))
panic(errors.New("Cannot set webhook"))
}
2016-02-08 16:52:13 +00:00
}
}
// SendTextMessage sends an HTML-styled text message to a specified chat
2016-02-09 10:33:38 +00:00
func (t Telegram) SendTextMessage(data tg.ClientTextMessageData) {
postdata := url.Values{
2016-09-01 12:05:32 +00:00
"chat_id": {strconv.FormatInt(data.ChatID, 10)},
2016-02-09 10:33:38 +00:00
"text": {data.Text},
"parse_mode": {"HTML"},
}
if data.ReplyID != nil {
2016-09-01 12:05:32 +00:00
postdata["reply_to_message_id"] = []string{strconv.FormatInt(*(data.ReplyID), 10)}
2016-02-09 10:33:38 +00:00
}
_, err := http.PostForm(t.apiURL("sendMessage"), postdata)
2016-02-20 21:28:30 +00:00
checkerr("SendTextMessage/http.PostForm", err)
}
func (t Telegram) SendPhoto(data tg.ClientPhotoData) {
// Decode photo from b64
photolen := base64.StdEncoding.DecodedLen(len(data.Bytes))
photobytes := make([]byte, photolen)
decoded, err := base64.StdEncoding.Decode(photobytes, []byte(data.Bytes))
if checkerr("SendPhoto/base64.Decode", err) {
return
}
// Write file into multipart buffer
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
2016-02-20 21:49:50 +00:00
part, err := writer.CreateFormFile("photo", data.Filename)
2016-02-20 21:28:30 +00:00
if checkerr("SendPhoto/multipart.CreateFormFile", err) {
return
}
part.Write(photobytes[0:decoded])
// Write other fields
2016-09-01 12:05:32 +00:00
writer.WriteField("chat_id", strconv.FormatInt(data.ChatID, 10))
2016-02-20 21:28:30 +00:00
if data.ReplyID != nil {
2016-09-01 12:05:32 +00:00
writer.WriteField("reply_to_message_id", strconv.FormatInt(*data.ReplyID, 10))
}
if data.Caption != "" {
writer.WriteField("caption", data.Caption)
2016-02-20 21:28:30 +00:00
}
err = writer.Close()
if checkerr("SendPhoto/writer.Close", err) {
return
}
// Create HTTP client and execute request
client := &http.Client{}
req, err := http.NewRequest("POST", t.apiURL("sendPhoto"), body)
if checkerr("SendPhoto/http.NewRequest", err) {
return
}
2016-02-20 21:49:50 +00:00
req.Header.Add("Content-Type", writer.FormDataContentType())
2016-02-20 22:18:25 +00:00
_, err = client.Do(req)
2016-02-20 21:28:30 +00:00
checkerr("SendPhoto/http.Do", err)
2016-02-09 10:33:38 +00:00
}
func (t Telegram) ForwardMessage(data tg.ClientForwardMessageData) {
postdata := url.Values{
"chat_id": {strconv.FormatInt(data.ChatID, 10)},
"from_chat_id": {strconv.FormatInt(data.FromChatID, 10)},
"message_id": {strconv.FormatInt(data.MessageID, 10)},
}
_, err := http.PostForm(t.apiURL("forwardMessage"), postdata)
checkerr("ForwardMessage/http.PostForm", err)
}
func (t Telegram) SendChatAction(data tg.ClientChatActionData) {
postdata := url.Values{
"chat_id": {strconv.FormatInt(data.ChatID, 10)},
"action": {string(data.Action)},
}
_, err := http.PostForm(t.apiURL("sendChatAction"), postdata)
checkerr("SendChatAction/http.PostForm", err)
}
2016-02-20 20:40:24 +00:00
// GetFile sends a "getFile" API call to Telegram's servers and fetches the file
// specified afterward. The file will be then send back to the client that requested it
// with the specified callback id.
func (t Telegram) GetFile(data tg.FileRequestData, client net.Conn, callback int) {
fail := func(msg string) {
errmsg, _ := json.Marshal(tg.BrokerUpdate{
Type: tg.BError,
Error: &msg,
Callback: &callback,
})
fmt.Fprintln(client, string(errmsg))
}
postdata := url.Values{
"file_id": {data.FileID},
}
resp, err := http.PostForm(t.apiURL("getFile"), postdata)
if checkerr("GetFile/post", err) {
fail("Server didn't like my request")
return
}
defer resp.Body.Close()
2016-02-20 20:57:04 +00:00
var filespecs = struct {
Ok bool `json:"ok"`
Result *tg.APIFile `json:"result,omitempty"`
}{}
2016-02-20 20:40:24 +00:00
err = json.NewDecoder(resp.Body).Decode(&filespecs)
if checkerr("GetFile/json.Decode", err) {
fail("Server sent garbage (or error)")
return
}
2016-02-20 20:57:04 +00:00
if filespecs.Result == nil {
fail("Server didn't send a file info, does the file exist?")
return
}
2016-02-20 20:59:41 +00:00
result := *filespecs.Result
2016-02-20 20:40:24 +00:00
2016-02-20 20:59:41 +00:00
path := APIEndpoint + "file/bot" + t.Token + "/" + *result.Path
2016-02-20 20:40:24 +00:00
fileresp, err := http.Get(path)
if checkerr("GetFile/get", err) {
fail("Could not retrieve file from Telegram's servers")
return
}
defer fileresp.Body.Close()
rawdata, err := ioutil.ReadAll(fileresp.Body)
if checkerr("GetFile/ioutil.ReadAll", err) {
fail("Could not read file data")
return
}
rawlen := len(rawdata)
2016-02-20 20:59:41 +00:00
if rawlen != *result.Size {
2016-02-20 20:40:24 +00:00
// ???
2016-02-20 20:59:41 +00:00
log.Printf("[GetFile] WARN ?? Downloaded file does not match provided filesize: %d != %d\n", rawlen, *result.Size)
2016-02-20 20:40:24 +00:00
}
b64data := base64.StdEncoding.EncodeToString(rawdata)
clientmsg, err := json.Marshal(tg.BrokerUpdate{
Type: tg.BFile,
Bytes: &b64data,
Callback: &callback,
})
if checkerr("GetFile/json.Marshal", err) {
fail("Could not serialize reply JSON")
return
}
fmt.Fprintln(client, string(clientmsg))
}
2016-02-08 16:57:27 +00:00
func (t Telegram) apiURL(method string) string {
2016-02-08 16:52:13 +00:00
return APIEndpoint + "bot" + t.Token + "/" + method
}
2016-02-08 16:57:27 +00:00
func checkerr(method string, err error) bool {
2016-02-08 16:52:13 +00:00
if err != nil {
2016-02-20 20:40:24 +00:00
log.Printf("[%s] Error: %s\n", method, err.Error())
2016-02-08 16:52:13 +00:00
return true
}
return false
}