Added sendPhoto
This commit is contained in:
parent
0210dfe43e
commit
a49072ab16
3 changed files with 57 additions and 5 deletions
|
@ -1,12 +1,14 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
@ -33,7 +35,7 @@ func mkAPI(token string) *Telegram {
|
|||
// 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", err) {
|
||||
if !checkerr("SetWebhook/http.PostForm", err) {
|
||||
defer resp.Body.Close()
|
||||
var result tg.APIResponse
|
||||
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)
|
||||
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
|
||||
|
|
|
@ -31,7 +31,7 @@ func initmeme() {
|
|||
font, err := freetype.ParseFont(bytes)
|
||||
assert(err)
|
||||
|
||||
memeFontData := draw2d.FontData{"impact", draw2d.FontFamilySans, 0}
|
||||
memeFontData = draw2d.FontData{"impact", draw2d.FontFamilySans, 0}
|
||||
draw2d.RegisterFont(memeFontData, font)
|
||||
}
|
||||
|
||||
|
|
13
tg/client.go
13
tg/client.go
|
@ -23,14 +23,20 @@ func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) error {
|
|||
defer broker.Close()
|
||||
|
||||
in := bufio.NewReader(broker.Socket)
|
||||
buf := make([]byte, 0)
|
||||
for {
|
||||
bytes, _, err := in.ReadLine()
|
||||
bytes, isPrefix, err := in.ReadLine()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
buf = append(buf, bytes...)
|
||||
|
||||
if isPrefix {
|
||||
continue
|
||||
}
|
||||
|
||||
var update BrokerUpdate
|
||||
err = json.Unmarshal(bytes, &update)
|
||||
err = json.Unmarshal(buf, &update)
|
||||
if err != nil {
|
||||
log.Printf("[tg - CreateBrokerClient] ERROR reading JSON: %s\r\n", err.Error())
|
||||
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
|
||||
go broker.SpliceCallback(*(update.Callback))(broker, update)
|
||||
}
|
||||
|
||||
// Empty buffer
|
||||
buf = []byte{}
|
||||
}
|
||||
return io.EOF
|
||||
}
|
||||
|
|
Reference in a new issue