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) {
switch action.Type {
case tg.CmdSendTextMessage:
data := *(action.TextMessageData)
api.SendTextMessage(data)
}
}

View File

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

View File

@ -6,6 +6,7 @@ import (
"log"
"net/http"
"net/url"
"strconv"
"../tg"
)
@ -22,25 +23,39 @@ func mkAPI(token string) *Telegram {
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}})
if !checkerr("setWebhook", err) {
if !checkerr("SetWebhook", err) {
defer resp.Body.Close()
var result tg.APIResponse
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
log.Println("Could not read reply: " + err.Error())
log.Println("[SetWebhook] Could not read reply: " + err.Error())
return
}
if result.Ok {
log.Println("Webhook successfully set!")
} 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"))
}
}
}
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 {
return APIEndpoint + "bot" + t.Token + "/" + method
}

View File

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

View File

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

View File

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

View File

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