tg/telegram.go

484 lines
14 KiB
Go

package tg
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"log"
"mime/multipart"
"net"
"net/http"
"net/url"
"strconv"
jsoniter "github.com/json-iterator/go"
)
var json = jsoniter.ConfigFastest
// APIEndpoint is Telegram's current Bot API base url endpoint
const APIEndpoint = "https://api.telegram.org/"
var (
// ErrMalformed represents an error that was encountered while processing the request (json encode/decode error etc)
ErrMalformed = errors.New("Error while handling request")
)
// WebhookHandler is a function that handles updates
type WebhookHandler func(APIUpdate)
// Telegram is the API client for the Telegram Bot API
type Telegram struct {
Token string
}
// MakeAPIClient creates a Telegram instance from a Bot API token
func MakeAPIClient(token string) *Telegram {
tg := new(Telegram)
tg.Token = token
return tg
}
// SetWebhook sets the webhook address so that Telegram knows where to send updates
func (t Telegram) SetWebhook(webhook string) {
resp, err := http.PostForm(t.apiURL("setWebhook"), url.Values{"url": {webhook}})
if !checkerr("SetWebhook/http.PostForm", err) {
defer resp.Body.Close()
var result APIResponse
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
log.Println("[SetWebhook] Could not read reply: " + err.Error())
return
}
if result.Ok {
log.Println("Webhook successfully set!")
} else {
log.Printf("[SetWebhook] Error setting webhook (errcode %d): %s\n", *(result.ErrCode), *(result.Description))
panic(errors.New("Cannot set webhook"))
}
}
}
// HandleWebhook is a webhook HTTP handler for standalone bots
func (t Telegram) HandleWebhook(bind string, webhook string, handler WebhookHandler) error {
whmux := http.NewServeMux()
whmux.HandleFunc(webhook, func(rw http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
// Re-encode request to ensure conformity
var update APIUpdate
err := json.NewDecoder(req.Body).Decode(&update)
if err != nil {
log.Println("[webhook] Received incorrect request: " + err.Error())
return
}
handler(update)
})
return http.ListenAndServe(bind, whmux)
}
// SendTextMessage sends an HTML-styled text message to a specified chat
func (t Telegram) SendTextMessage(data ClientTextMessageData) (APIMessage, error) {
postdata := url.Values{
"chat_id": {strconv.FormatInt(data.ChatID, 10)},
"text": {data.Text},
"parse_mode": {"HTML"},
}
if data.ReplyID != nil {
postdata["reply_to_message_id"] = []string{strconv.FormatInt(*(data.ReplyID), 10)}
}
if data.ReplyMarkup != nil {
replyjson, err := json.Marshal(data.ReplyMarkup)
if checkerr("SendTextMessage/json.Marshal", err) {
return APIMessage{}, err
}
postdata["reply_markup"] = []string{string(replyjson)}
}
resp, err := http.PostForm(t.apiURL("sendMessage"), postdata)
if checkerr("SendTextMessage/http.PostForm", err) {
return APIMessage{}, err
}
defer resp.Body.Close()
var out apiMessageResponse
err = json.NewDecoder(resp.Body).Decode(&out)
checkerr("SendTextMessage/json.Decode", err)
return out.Result, err
}
// SendPhotoNet decodes a network SendPhoto request and executes it
func (t Telegram) SendPhotoNet(data ClientPhotoDataNet) (APIMessage, error) {
byt, err := base64.StdEncoding.DecodeString(data.Bytes)
if checkerr("SendPhotoNet/base64.Decode", err) {
return APIMessage{}, err
}
return t.SendPhoto(ClientPhotoData{
ChatID: data.ChatID,
Bytes: byt,
Filename: data.Filename,
Caption: data.Caption,
ReplyID: data.ReplyID,
})
}
// SendPhoto sends a picture to a chat as a photo
func (t Telegram) SendPhoto(data ClientPhotoData) (APIMessage, error) {
// Write file into multipart buffer
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("photo", data.Filename)
if checkerr("SendPhoto/multipart.CreateFormFile", err) {
return APIMessage{}, err
}
part.Write(data.Bytes)
// Write other fields
writer.WriteField("chat_id", strconv.FormatInt(data.ChatID, 10))
if data.ReplyID != nil {
writer.WriteField("reply_to_message_id", strconv.FormatInt(*data.ReplyID, 10))
}
if data.Caption != "" {
writer.WriteField("caption", data.Caption)
}
err = writer.Close()
if checkerr("SendPhoto/writer.Close", err) {
return APIMessage{}, err
}
// 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 APIMessage{}, err
}
req.Header.Add("Content-Type", writer.FormDataContentType())
resp, err := client.Do(req)
if checkerr("SendPhoto/http.Do", err) {
return APIMessage{}, err
}
defer resp.Body.Close()
var out apiMessageResponse
err = json.NewDecoder(resp.Body).Decode(&out)
checkerr("SendPhoto/json.Decode", err)
return out.Result, err
}
// SendAlbum sends an album of photos or videos
func (t Telegram) SendAlbum(data ClientAlbumData) ([]APIMessage, error) {
jsonmedia, err := json.Marshal(data.Media)
if err != nil {
checkerr("SendAlbum/json.Marshal", err)
return nil, err
}
postdata := url.Values{
"chat_id": {strconv.FormatInt(data.ChatID, 10)},
"media": {string(jsonmedia)},
}
if data.Silent {
postdata.Set("disable_notification", "true")
}
if data.ReplyID != nil {
postdata.Set("reply_to_message_id", strconv.FormatInt(*(data.ReplyID), 10))
}
resp, err := http.PostForm(t.apiURL("sendMediaGroup"), postdata)
if checkerr("SendAlbum/http.PostForm", err) {
return nil, err
}
defer resp.Body.Close()
var out apiAlbumResponse
err = json.NewDecoder(resp.Body).Decode(&out)
checkerr("SendAlbum/json.Decode", err)
return out.Result, err
}
// ForwardMessage forwards an existing message to a chat
func (t Telegram) ForwardMessage(data ClientForwardMessageData) (APIMessage, error) {
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)},
}
resp, err := http.PostForm(t.apiURL("forwardMessage"), postdata)
if checkerr("ForwardMessage/http.PostForm", err) {
return APIMessage{}, err
}
defer resp.Body.Close()
var out APIMessage
err = json.NewDecoder(resp.Body).Decode(&out)
checkerr("ForwardMessage/json.Decode", err)
return out, err
}
// SendChatAction sends a 5 second long action (X is writing, sending a photo ecc.)
func (t Telegram) SendChatAction(data 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)
}
// AnswerCallback stops the progress bar after a callback query is initiated
func (t Telegram) AnswerCallback(data ClientCallbackQueryData) {
postdata := url.Values{
"callback_query_id": {data.QueryID},
}
if data.Text != "" {
postdata.Set("text", data.Text)
}
if data.URL != "" {
postdata.Set("url", data.URL)
}
if data.ShowAlert {
postdata.Set("show_alert", "true")
}
if data.CacheTime != 0 {
postdata.Set("cache_time", strconv.FormatInt(data.CacheTime, 10))
}
_, err := http.PostForm(t.apiURL("answerCallbackQuery"), postdata)
checkerr("AnswerCallback/http.PostForm", err)
}
// EditText modifies a text message
func (t Telegram) EditText(data ClientEditTextData) error {
postdata := url.Values{
"text": {data.Text},
"parse_mode": {"HTML"},
}
if data.InlineID != "" {
postdata.Set("inline_message_id", data.InlineID)
} else {
postdata.Set("chat_id", strconv.FormatInt(data.ChatID, 10))
postdata.Set("message_id", strconv.FormatInt(data.MessageID, 10))
}
if data.ReplyMarkup != nil {
replyjson, err := json.Marshal(data.ReplyMarkup)
if checkerr("EditText/json.Marshal", err) {
return ErrMalformed
}
postdata["reply_markup"] = []string{string(replyjson)}
}
_, err := http.PostForm(t.apiURL("editMessageText"), postdata)
checkerr("EditText/http.PostForm", err)
return nil
}
// EditCaption modifies the caption of a photo/document/etc message
func (t Telegram) EditCaption(data ClientEditCaptionData) error {
postdata := url.Values{
"caption": {data.Caption},
"parse_mode": {"HTML"},
}
if data.InlineID != "" {
postdata.Set("inline_message_id", data.InlineID)
} else {
postdata.Set("chat_id", strconv.FormatInt(data.ChatID, 10))
postdata.Set("message_id", strconv.FormatInt(data.MessageID, 10))
}
if data.ReplyMarkup != nil {
replyjson, err := json.Marshal(data.ReplyMarkup)
if checkerr("EditCaption/json.Marshal", err) {
return ErrMalformed
}
postdata["reply_markup"] = []string{string(replyjson)}
}
_, err := http.PostForm(t.apiURL("editMessageCaption"), postdata)
checkerr("EditCaption/http.PostForm", err)
return nil
}
// EditMedia modifies the media content (like photo) of a message
func (t Telegram) EditMedia(data ClientEditMediaData) error {
jsonmedia, err := json.Marshal(data.Media)
if err != nil {
checkerr("EditMedia/json.Marshal", err)
return ErrMalformed
}
postdata := url.Values{
"media": {string(jsonmedia)},
}
if data.InlineID != "" {
postdata.Set("inline_message_id", data.InlineID)
} else {
postdata.Set("chat_id", strconv.FormatInt(data.ChatID, 10))
postdata.Set("message_id", strconv.FormatInt(data.MessageID, 10))
}
if data.ReplyMarkup != nil {
replyjson, err := json.Marshal(data.ReplyMarkup)
if checkerr("EditMedia/json.Marshal", err) {
return ErrMalformed
}
postdata["reply_markup"] = []string{string(replyjson)}
}
_, err = http.PostForm(t.apiURL("editMessageMedia"), postdata)
checkerr("EditMedia/http.PostForm", err)
return nil
}
// AnswerInlineQuery replies to an inline query
func (t Telegram) AnswerInlineQuery(data InlineQueryResponse) error {
jsonresults, err := json.Marshal(data.Results)
if checkerr("AnswerInlineQuery/json.Marshal", err) {
return ErrMalformed
}
postdata := url.Values{
"inline_query_id": {data.QueryID},
"parse_mode": {"HTML"},
"results": {string(jsonresults)},
}
if data.CacheTime != nil {
postdata["cache_time"] = []string{strconv.Itoa(*data.CacheTime)}
}
if data.IsPersonal {
postdata["is_personal"] = []string{"true"}
}
if data.NextOffset != "" {
postdata["next_offset"] = []string{data.NextOffset}
}
if data.PMText != "" {
postdata["switch_pm_text"] = []string{data.PMText}
}
if data.PMParam != "" {
postdata["switch_pm_parameter"] = []string{data.PMParam}
}
result, err := http.PostForm(t.apiURL("answerInlineQuery"), postdata)
if checkerr("AnswerInlineQuery/http.PostForm", err) {
return ErrMalformed
}
var response APIResponse
err = json.NewDecoder(result.Body).Decode(&response)
result.Body.Close()
if checkerr("AnswerInlineQuery/json.Decode", err) {
return ErrMalformed
}
if !response.Ok && response.Description != nil {
return errors.New(*response.Description)
}
return nil
}
// 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 FileRequestData) ([]byte, error) {
postdata := url.Values{
"file_id": {data.FileID},
}
resp, err := http.PostForm(t.apiURL("getFile"), postdata)
if checkerr("GetFile/post", err) {
return nil, fmt.Errorf("Server didn't like my request: %w", err)
}
defer resp.Body.Close()
var filespecs = struct {
Ok bool `json:"ok"`
Result *APIFile `json:"result,omitempty"`
}{}
err = json.NewDecoder(resp.Body).Decode(&filespecs)
if checkerr("GetFile/json.Decode", err) {
return nil, fmt.Errorf("Server sent garbage (or error): %w", err)
}
if filespecs.Result == nil {
return nil, fmt.Errorf("Server didn't send a file info, does the file exist?")
}
result := *filespecs.Result
path := APIEndpoint + "file/bot" + t.Token + "/" + *result.Path
fileresp, err := http.Get(path)
if checkerr("GetFile/get", err) {
return nil, fmt.Errorf("Could not retrieve file from Telegram's servers: %w", err)
}
defer fileresp.Body.Close()
rawdata, err := ioutil.ReadAll(fileresp.Body)
if checkerr("GetFile/ioutil.ReadAll", err) {
return nil, fmt.Errorf("Could not read file data: %w", err)
}
rawlen := len(rawdata)
if rawlen != *result.Size {
// ???
log.Printf("[GetFile] WARN ?? Downloaded file does not match provided filesize: %d != %d\n", rawlen, *result.Size)
}
return rawdata, nil
}
func (t Telegram) GetFileNet(data FileRequestData, client net.Conn, callback int) {
byt, err := t.GetFile(data)
if err != nil {
errstr := err.Error()
errmsg, _ := json.Marshal(BrokerUpdate{
Type: BError,
Error: &errstr,
Callback: &callback,
})
fmt.Fprintln(client, string(errmsg))
}
b64data := base64.StdEncoding.EncodeToString(byt)
clientmsg, err := json.Marshal(BrokerUpdate{
Type: BFile,
Bytes: &b64data,
Callback: &callback,
})
if checkerr("GetFile/json.Marshal", err) {
errstr := "Could not serialize reply JSON"
errmsg, _ := json.Marshal(BrokerUpdate{
Type: BError,
Error: &errstr,
Callback: &callback,
})
fmt.Fprintln(client, string(errmsg))
return
}
fmt.Fprintln(client, string(clientmsg))
}
func (t Telegram) GetMe() (APIUser, error) {
resp, err := http.Get(t.apiURL("getMe"))
if checkerr("GetMe/get", err) {
return APIUser{}, err
}
var result apiUserResponse
err = json.NewDecoder(resp.Body).Decode(&result)
return result.Result, err
}
func (t Telegram) apiURL(method string) string {
return APIEndpoint + "bot" + t.Token + "/" + method
}
func checkerr(method string, err error) bool {
if err != nil {
log.Printf("[%s] Error: %s\n", method, err.Error())
return true
}
return false
}