changeset 23:ee729eaf611c

Make callback/message handling more consistent
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 10 Dec 2016 10:03:15 +0100
parents 9e7757101e75
children 0a0ea2b34787
files handler_todo.go handlers.go webhook.go
diffstat 3 files changed, 44 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/handler_todo.go	Sat Dec 10 09:37:47 2016 +0100
+++ b/handler_todo.go	Sat Dec 10 10:03:15 2016 +0100
@@ -1,16 +1,21 @@
 package main
 
+import "log"
+
 func todoButtonTest(msg message) (replyContent, error) {
 	buttonRows := [][]inlineKeyboardButton{}
 
 	for _, b := range []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"} {
-		buttonRows = append(buttonRows, []inlineKeyboardButton{inlineKeyboardButton{Callback_Data: b, Text: b}})
+		buttonRows = append(buttonRows, []inlineKeyboardButton{inlineKeyboardButton{Callback_Data: "markdone:" + b, Text: b}})
 	}
 
 	return replyContent{text: "Please select", buttons: inlineKeyboardMarkup{buttonRows}}, nil
 }
 
-func todoDoneHandler(token string) error {
+func todoDoneHandler(token string, cbq callbackQuery) (replyContent, error) {
+	log.Println("Received callback for", token)
 
-	return nil
+	reply := replyContent{text: "Verarbeitet!"}
+
+	return reply, nil
 }
--- a/handlers.go	Sat Dec 10 09:37:47 2016 +0100
+++ b/handlers.go	Sat Dec 10 10:03:15 2016 +0100
@@ -27,7 +27,7 @@
 }
 
 // A handler taking the callback data. `data` only contains the token, not the callback type.
-type callbackHandler (func(data string) error)
+type callbackHandler (func(data string, cbq callbackQuery) (replyContent, error))
 
 var (
 	handlers = map[string]handler{
@@ -80,48 +80,65 @@
 	return replyContent{text: srvStatus.String()}, nil
 }
 
+// Dispatch an incoming update to the right handler. dispatch() only selects between messages and callbacks; dispatchMessage
+// and dispatchCallback select the actual handlers.
 func dispatch(upd update) (sendMessage, error) {
+	var rp replyContent
+	var err error
+	var chatID uint64
+
 	if upd.Message.Message_ID > 0 {
 		srvStatus.commands++
-
-		msg, err := dispatchMessage(upd.Message)
-
-		if err != nil {
-			srvStatus.errors++
-		}
-
-		return msg, err
+		chatID = upd.Message.Chat.ID
+		rp, err = dispatchMessage(upd.Message)
 	} else if upd.Callback_Query.ID != "" {
 		srvStatus.callbacks++
-		dispatchCallback(upd.Callback_Query)
-
-		return sendMessage{}, nil
+		chatID = upd.Callback_Query.Message.Chat.ID
+		rp, err = dispatchCallback(upd.Callback_Query)
 	} else {
-
 		reply := sendMessage{
 			Chat_ID:    upd.Message.Chat.ID,
 			Parse_Mode: "Markdown",
 			Text:       "",
 		}
+		srvStatus.errors++
 
 		return reply, nil
 	}
+
+	if err != nil {
+		srvStatus.errors++
+	}
+
+	reply := sendMessage{
+		Chat_ID:      chatID,
+		Parse_Mode:   "Markdown",
+		Text:         rp.text,
+		Reply_Markup: inlineKeyboardMarkup{Inline_Keyboard: [][]inlineKeyboardButton{}},
+	}
+
+	if rp.buttons.Inline_Keyboard != nil {
+		reply.Reply_Markup = rp.buttons
+	}
+
+	return reply, err
 }
 
 // Dispatches an incoming callbackQuery. The data field has the format callback_type:token.
-func dispatchCallback(cbq callbackQuery) error {
+func dispatchCallback(cbq callbackQuery) (replyContent, error) {
 	parts := strings.Split(cbq.Data, ":") // callback:token
 
 	if handler, ok := callbackHandlers[parts[0]]; ok {
-		handler(parts[1])
-		return nil
+		rp, err := handler(parts[1], cbq)
+
+		return rp, err
 	} else {
 		log.Println("Didn't find callback handler for type", parts[0])
-		return errors.New("handler not found")
+		return replyContent{}, errors.New("handler not found")
 	}
 }
 
-func dispatchMessage(msg message) (sendMessage, error) {
+func dispatchMessage(msg message) (replyContent, error) {
 	var rp replyContent
 	var err error
 
@@ -149,17 +166,5 @@
 		rp, err = badFormatHandler(msg)
 	}
 
-	reply := sendMessage{
-		Chat_ID:    msg.Chat.ID,
-		Parse_Mode: "Markdown",
-		Text:       rp.text,
-	}
-
-	if rp.buttons.Inline_Keyboard != nil {
-		reply.Reply_Markup = rp.buttons
-	} else {
-		reply.Reply_Markup = inlineKeyboardMarkup{Inline_Keyboard: [][]inlineKeyboardButton{}}
-	}
-
-	return reply, err
+	return rp, err
 }
--- a/webhook.go	Sat Dec 10 09:37:47 2016 +0100
+++ b/webhook.go	Sat Dec 10 10:03:15 2016 +0100
@@ -127,7 +127,6 @@
 	}
 
 	responseMsg.Method = sendMessageMethod
-
 	responseBody, err := json.Marshal(responseMsg)
 
 	if err != nil {