changeset 18:02dba5e5265e

Remodel handler type and add button test
author Lewin Bormann <lbo@spheniscida.de>
date Fri, 09 Dec 2016 22:14:12 +0100
parents b7fca64d56d0
children a9d6fa80e7dc
files api_schema.go handlers.go
diffstat 2 files changed, 71 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/api_schema.go	Fri Dec 09 22:00:48 2016 +0100
+++ b/api_schema.go	Fri Dec 09 22:14:12 2016 +0100
@@ -62,10 +62,11 @@
 }
 
 type sendMessage struct {
-	Chat_ID             uint64 `json:"chat_id"`
-	Text                string `json:"text"`
-	Parse_Mode          string `json:"parse_mode"` // either Markdown or HTML
-	Reply_To_message_Id uint64 `json:"reply_to_message_id"`
+	Chat_ID             uint64               `json:"chat_id"`
+	Text                string               `json:"text"`
+	Parse_Mode          string               `json:"parse_mode"` // either Markdown or HTML
+	Reply_To_message_Id uint64               `json:"reply_to_message_id"`
+	Reply_Markup        inlineKeyboardMarkup `json:"reply_markup"`
 
 	// Used for replying from a webhook; usually set to "sendMessage"
 	Method string `json:"method"`
@@ -80,3 +81,16 @@
 	URL             string `json:"url"`
 	Allowed_Updates string `json:"allowed_updates"`
 }
+
+// for todo lists
+type inlineKeyboardMarkup struct {
+	Inline_Keyboard [][]inlineKeyboardButton `json:"inline_keyboard"`
+}
+
+type inlineKeyboardButton struct {
+	Text string `json:"text"`
+	// optional: URL to open on press
+	URL string `json:"url"`
+	// Response to send on press
+	Callback_Data string `json:"callback_data"`
+}
--- a/handlers.go	Fri Dec 09 22:00:48 2016 +0100
+++ b/handlers.go	Fri Dec 09 22:14:12 2016 +0100
@@ -8,63 +8,80 @@
 )
 
 const (
-	echoCmd    = "echo"
-	fortuneCmd = "fortune"
-	helpCmd    = "help"
-	quoteCmd   = "quote"
-	remindCmd  = "remind"
-	statusCmd  = "status"
-	todoCmd    = "todo"
+	echoCmd     = "echo"
+	fortuneCmd  = "fortune"
+	helpCmd     = "help"
+	quoteCmd    = "quote"
+	remindCmd   = "remind"
+	statusCmd   = "status"
+	todoCmd     = "todo"
+	todoTestCmd = "todo-"
 )
 
 type handler struct {
-	fn   (func(message) (string, error))
+	fn   (func(message) (replyContent, error))
 	desc string
 }
 
 var (
 	handlers = map[string]handler{
-		echoCmd:    {echoHandler, "Anfrage zurücksenden"},
-		fortuneCmd: {missingHandler, "Glückskeks"},
-		quoteCmd:   {missingHandler, "Zitat speichern/abfragen"},
-		remindCmd:  {missingHandler, "Wecker"},
-		statusCmd:  {statusHandler, "Status anfragen"},
-		todoCmd:    {missingHandler, "Aufgabenliste"},
+		echoCmd:     {echoHandler, "Anfrage zurücksenden"},
+		fortuneCmd:  {missingHandler, "Glückskeks"},
+		quoteCmd:    {missingHandler, "Zitat speichern/abfragen"},
+		remindCmd:   {missingHandler, "Wecker"},
+		statusCmd:   {statusHandler, "Status anfragen"},
+		todoCmd:     {missingHandler, "Aufgabenliste"},
+		todoTestCmd: {todoButtonTest, "TODO test (internal)"},
 	}
 )
 
-func echoHandler(msg message) (string, error) {
-	return msg.Text, nil
+type replyContent struct {
+	text    string
+	buttons inlineKeyboardMarkup
 }
 
-func missingHandler(msg message) (string, error) {
-	return "_tut mir leid, das kann ich noch nicht :(_", nil
+func echoHandler(msg message) (replyContent, error) {
+	return replyContent{text: msg.Text}, nil
 }
 
-func unknownCommandHandler(msg message) (string, error) {
-	return "_tut mir leid, das verstehe ich nicht (*/help* für Hilfe)_", nil
+func missingHandler(msg message) (replyContent, error) {
+	return replyContent{text: "_tut mir leid, das kann ich noch nicht :(_"}, nil
 }
 
-func badFormatHandler(msg message) (string, error) {
-	return "_bitte benutze einen Befehl, der mit '/' beginnt!_", nil
+func unknownCommandHandler(msg message) (replyContent, error) {
+	return replyContent{text: "_tut mir leid, das verstehe ich nicht (*/help* für Hilfe)_"}, nil
 }
 
-func helpHandler(message) (string, error) {
+func badFormatHandler(msg message) (replyContent, error) {
+	return replyContent{text: "_bitte benutze einen Befehl, der mit '/' beginnt!_"}, nil
+}
+
+func helpHandler(message) (replyContent, error) {
 	b := bytes.NewBuffer(nil)
 
 	for n, h := range handlers {
 		b.WriteString(fmt.Sprintf("*/%-15s*    %s\n", n, h.desc))
 	}
 
-	return b.String(), nil
+	return replyContent{text: b.String()}, nil
+}
+
+func statusHandler(message) (replyContent, error) {
+	return replyContent{text: srvStatus.String()}, nil
 }
 
-func statusHandler(message) (string, error) {
-	return srvStatus.String(), nil
+func todoButtonTest(msg message) (replyContent, error) {
+	buttonRows := [][]inlineKeyboardButton{}
+
+	for _, b := range []string{"1", "2", "3"} {
+		buttonRows = append(buttonRows, []inlineKeyboardButton{inlineKeyboardButton{Callback_Data: b, Text: b}})
+	}
+
+	return replyContent{text: "Please select", buttons: inlineKeyboardMarkup{buttonRows}}, nil
 }
 
 func dispatch(msg message) (sendMessage, error) {
-	text := ""
+	var rp replyContent
 	var err error
 
 	words := strings.SplitAfter(msg.Text, " ")
@@ -79,22 +96,24 @@
 		cmd = strings.Trim(cmd, " ")
 
 		if cmd == helpCmd { // special case
-			text, err = helpHandler(msg)
+			rp, err = helpHandler(msg)
 		} else if h, ok := handlers[cmd]; ok {
-			text, err = h.fn(msg)
+			rp, err = h.fn(msg)
 		} else {
 			log.Println("Unknown command", cmd)
-			text, err = unknownCommandHandler(msg)
+			rp, err = unknownCommandHandler(msg)
 		}
 	} else {
 		log.Println("Bad format:", msg.Text)
-		text, err = badFormatHandler(msg)
+		rp, err = badFormatHandler(msg)
 	}
 
 	reply := sendMessage{
-		Chat_ID:    msg.Chat.ID,
-		Parse_Mode: "Markdown",
-		Text:       text}
+		Chat_ID:      msg.Chat.ID,
+		Parse_Mode:   "Markdown",
+		Text:         rp.text,
+		Reply_Markup: rp.buttons,
+	}
 
 	return reply, err
 }