Added sendPhoto

This commit is contained in:
Hamcha 2016-02-20 22:28:30 +01:00
parent 0210dfe43e
commit a49072ab16
3 changed files with 57 additions and 5 deletions

View file

@ -1,12 +1,14 @@
package main package main
import ( import (
"bytes"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"mime/multipart"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -33,7 +35,7 @@ func mkAPI(token string) *Telegram {
// SetWebhook sets the webhook address so that Telegram knows where to send updates // SetWebhook sets the webhook address so that Telegram knows where to send updates
func (t Telegram) SetWebhook(webhook string) { func (t Telegram) SetWebhook(webhook string) {
resp, err := http.PostForm(t.apiURL("setWebhook"), url.Values{"url": {webhook}}) resp, err := http.PostForm(t.apiURL("setWebhook"), url.Values{"url": {webhook}})
if !checkerr("SetWebhook", err) { if !checkerr("SetWebhook/http.PostForm", err) {
defer resp.Body.Close() defer resp.Body.Close()
var result tg.APIResponse var result tg.APIResponse
err = json.NewDecoder(resp.Body).Decode(&result) err = json.NewDecoder(resp.Body).Decode(&result)
@ -62,7 +64,48 @@ func (t Telegram) SendTextMessage(data tg.ClientTextMessageData) {
} }
_, err := http.PostForm(t.apiURL("sendMessage"), postdata) _, err := http.PostForm(t.apiURL("sendMessage"), postdata)
checkerr("SendTextMessage", err) 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)
part, err := writer.CreateFormFile("photo", "meme.jpg")
if checkerr("SendPhoto/multipart.CreateFormFile", err) {
return
}
part.Write(photobytes[0:decoded])
// Write other fields
writer.WriteField("chat_id", strconv.Itoa(data.ChatID))
if data.ReplyID != nil {
writer.WriteField("reply_to_message_id", strconv.Itoa(*data.ReplyID))
}
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
}
_, err = client.Do(req)
checkerr("SendPhoto/http.Do", err)
} }
// GetFile sends a "getFile" API call to Telegram's servers and fetches the file // GetFile sends a "getFile" API call to Telegram's servers and fetches the file

View file

@ -31,7 +31,7 @@ func initmeme() {
font, err := freetype.ParseFont(bytes) font, err := freetype.ParseFont(bytes)
assert(err) assert(err)
memeFontData := draw2d.FontData{"impact", draw2d.FontFamilySans, 0} memeFontData = draw2d.FontData{"impact", draw2d.FontFamilySans, 0}
draw2d.RegisterFont(memeFontData, font) draw2d.RegisterFont(memeFontData, font)
} }

View file

@ -23,14 +23,20 @@ func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) error {
defer broker.Close() defer broker.Close()
in := bufio.NewReader(broker.Socket) in := bufio.NewReader(broker.Socket)
buf := make([]byte, 0)
for { for {
bytes, _, err := in.ReadLine() bytes, isPrefix, err := in.ReadLine()
if err != nil { if err != nil {
break break
} }
buf = append(buf, bytes...)
if isPrefix {
continue
}
var update BrokerUpdate var update BrokerUpdate
err = json.Unmarshal(bytes, &update) err = json.Unmarshal(buf, &update)
if err != nil { if err != nil {
log.Printf("[tg - CreateBrokerClient] ERROR reading JSON: %s\r\n", err.Error()) log.Printf("[tg - CreateBrokerClient] ERROR reading JSON: %s\r\n", err.Error())
log.Printf("%s\n", string(bytes)) log.Printf("%s\n", string(bytes))
@ -44,6 +50,9 @@ func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) error {
// It's a response to a request: retrieve callback and call it // It's a response to a request: retrieve callback and call it
go broker.SpliceCallback(*(update.Callback))(broker, update) go broker.SpliceCallback(*(update.Callback))(broker, update)
} }
// Empty buffer
buf = []byte{}
} }
return io.EOF return io.EOF
} }