Add basic reply functionality

This commit is contained in:
Hamcha 2016-02-09 10:33:38 +00:00
parent 9378ec3e38
commit 18fe8eee60
7 changed files with 34 additions and 11 deletions

View file

@ -5,5 +5,9 @@ import (
) )
func executeClientCommand(action tg.ClientCommand) { func executeClientCommand(action tg.ClientCommand) {
switch action.Type {
case tg.CmdSendTextMessage:
data := *(action.TextMessageData)
api.SendTextMessage(data)
}
} }

View file

@ -48,7 +48,7 @@ func main() {
// Register webhook @ Telegram // Register webhook @ Telegram
log.Println("Registering webhook..") log.Println("Registering webhook..")
api.setWebhook(config.BaseURL + config.WebhookURL) api.SetWebhook(config.BaseURL + config.WebhookURL)
// Create server for clients // Create server for clients
log.Println("Starting clients server..") log.Println("Starting clients server..")

View file

@ -6,6 +6,7 @@ import (
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"../tg" "../tg"
) )
@ -22,25 +23,39 @@ func mkAPI(token string) *Telegram {
return tg return tg
} }
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", 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)
if err != nil { if err != nil {
log.Println("Could not read reply: " + err.Error()) log.Println("[SetWebhook] Could not read reply: " + err.Error())
return return
} }
if result.Ok { if result.Ok {
log.Println("Webhook successfully set!") log.Println("Webhook successfully set!")
} else { } else {
log.Printf("Error setting webhook (errcode %d): %s\n", *(result.ErrCode), *(result.Description)) log.Printf("[SetWebhook] Error setting webhook (errcode %d): %s\n", *(result.ErrCode), *(result.Description))
panic(errors.New("Cannot set webhook")) panic(errors.New("Cannot set webhook"))
} }
} }
} }
func (t Telegram) SendTextMessage(data tg.ClientTextMessageData) {
postdata := url.Values{
"chat_id": {strconv.Itoa(data.ChatID)},
"text": {data.Text},
"parse_mode": {"HTML"},
}
if data.ReplyID != nil {
postdata["reply_to_message_id"] = []string{strconv.Itoa(*(data.ReplyID))}
}
_, err := http.PostForm(t.apiURL("sendMessage"), postdata)
checkerr("SendTextMessage", err)
}
func (t Telegram) apiURL(method string) string { func (t Telegram) apiURL(method string) string {
return APIEndpoint + "bot" + t.Token + "/" + method return APIEndpoint + "bot" + t.Token + "/" + method
} }

View file

@ -28,7 +28,7 @@ func metafora(broker *tg.Broker, update tg.APIMessage) {
if *(update.Text) == "/metafora" { if *(update.Text) == "/metafora" {
n := rand.Intn(len(actions)) n := rand.Intn(len(actions))
m := rand.Intn(len(objects)) m := rand.Intn(len(objects))
broker.SendTextMessage(update.Chat, actions[n]+" "+objects[m]) broker.SendTextMessage(update.Chat, actions[n]+" "+objects[m], nil)
return return
} }
} }

View file

@ -17,7 +17,7 @@ const (
) )
type APIChat struct { type APIChat struct {
UserID int `json:"id"` ChatID int `json:"id"`
Type ChatType `json:"type"` Type ChatType `json:"type"`
Title *string `json:"title,omitempty"` Title *string `json:"title,omitempty"`
Username *string `json:"username,omitempty"` Username *string `json:"username,omitempty"`

View file

@ -26,11 +26,13 @@ func (b *Broker) Close() {
b.Socket.Close() b.Socket.Close()
} }
func (b *Broker) SendTextMessage(chat *APIChat, text string) { func (b *Broker) SendTextMessage(chat *APIChat, text string, original *int) {
cmd := ClientCommand{ cmd := ClientCommand{
Type: CmdSendTextMessage, Type: CmdSendTextMessage,
TextMessageData: &ClientTextMessageData{ TextMessageData: &ClientTextMessageData{
Text: text, Text: text,
ChatID: chat.ChatID,
ReplyID: original,
}, },
} }
// Encode command and send to broker // Encode command and send to broker

View file

@ -7,7 +7,9 @@ const (
) )
type ClientTextMessageData struct { type ClientTextMessageData struct {
Text string ChatID int
Text string
ReplyID *int
} }
type ClientCommand struct { type ClientCommand struct {