Skip to content

Commit b7442b8

Browse files
Merge pull request #12 from snehalyelmati/dev
Quick replies
2 parents 66b269d + 57c5c90 commit b7442b8

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

main.go

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66
"log"
77
"net/http"
88
"os"
9+
"regexp"
910
"strconv"
1011
"strings"
1112

1213
fiber "github.com/gofiber/fiber/v2"
13-
"github.com/snehalyelmati/telegram-bot-golang/models"
14+
. "github.com/snehalyelmati/telegram-bot-golang/models"
1415
DialogFlow "github.com/snehalyelmati/telegram-bot-golang/services"
1516
)
1617

@@ -42,12 +43,14 @@ func main() {
4243

4344
app.Post(URI, func(c *fiber.Ctx) error {
4445
l.Println("Post request")
45-
reqBody := new(models.MessageReq)
46+
reqBody := new(TelgramMessageReq)
4647
json.Unmarshal(c.Body(), reqBody)
4748
l.Printf("%v", reqBody)
4849

50+
// initialize stuff
4951
inputMessage := reqBody.Message.Text
5052
if inputMessage == "/start" {
53+
// save the user data
5154
inputMessage = "hi"
5255
}
5356

@@ -58,6 +61,12 @@ func main() {
5861
// })
5962

6063
// get response from dialogflow
64+
65+
// sample format for text and quick replies:
66+
// statement1
67+
// statement2
68+
// <qrType1, qrName1, qrText1><qrType2, qrName2, qrText2>...
69+
6170
sessionID := strconv.Itoa(reqBody.Message.Chat.ID)
6271
response, queryResult, err := DialogFlow.DetectIntentText(PROJECT_ID, sessionID, inputMessage, LANGUAGE)
6372
if err != nil {
@@ -77,6 +86,43 @@ func main() {
7786
l.Println(err)
7887
}
7988

89+
response := new(TelegramMessageRes)
90+
response.ChatID = strconv.Itoa(reqBody.Message.Chat.ID)
91+
response.ReplyMarkup.Keyboard = make([][]TelegramMessageResReplyMarkupKeyboard, 0)
92+
93+
// get all the Quick Replies from the text
94+
// prepare a map with label, postBack and qrType
95+
regEx := `<(?P<qrType>[\sa-zA-Z0-9]*),(?P<postBack>[\sa-zA-Z0-9]*),(?P<label>[\sa-zA-Z0-9]*)>`
96+
quickReplies, statement := getParams(regEx, statement)
97+
response.Text = statement
98+
l.Println("Quick replies:", quickReplies)
99+
100+
// initialize keyboard obj
101+
response.ReplyMarkup.Keyboard = append(response.ReplyMarkup.Keyboard, []TelegramMessageResReplyMarkupKeyboard{})
102+
103+
// append available quick replies
104+
for _, qr := range quickReplies {
105+
if qr["qrType"] == "OPT" {
106+
// append to response.ReplyMarkup.Keyboard
107+
response.ReplyMarkup.Keyboard = append(response.ReplyMarkup.Keyboard, []TelegramMessageResReplyMarkupKeyboard{{Text: qr["label"]}})
108+
} else if qr["qrType"] == "SUGT" {
109+
// TODO: add buttons for suggestions
110+
}
111+
}
112+
l.Println("Response ReplyMarkup Keyboard:", response.ReplyMarkup.Keyboard)
113+
114+
if len(response.ReplyMarkup.Keyboard) > 0 {
115+
response.ReplyMarkup.OneTimeKeyboard = true
116+
response.ReplyMarkup.ResizeKeyboard = true
117+
}
118+
119+
// prepare a json object from the response object
120+
data, err = json.Marshal(response)
121+
if err != nil {
122+
l.Println(err)
123+
}
124+
125+
// call the Telegram API
80126
_, err = http.Post(TELEGRAM_API+"/sendMessage", "application/json", bytes.NewBuffer(data))
81127
if err != nil {
82128
l.Println(err)
@@ -89,3 +135,30 @@ func main() {
89135
app.Listen(":3000")
90136
l.Println("Server running on port 3000")
91137
}
138+
139+
/**
140+
* Parses string with the given regular expression and returns the
141+
* group values defined in the expression.
142+
*
143+
*/
144+
func getParams(regEx, str string) ([]map[string]string, string) {
145+
146+
var compRegEx = regexp.MustCompile(regEx)
147+
matchArr := compRegEx.FindAllStringSubmatch(str, -1)
148+
if len(matchArr) > 0 {
149+
str = compRegEx.ReplaceAllString(str, "")
150+
}
151+
152+
res := make([]map[string]string, len(matchArr)) // map array
153+
154+
for _, match := range matchArr {
155+
paramsMap := make(map[string]string)
156+
for i, name := range compRegEx.SubexpNames() {
157+
if i > 0 && i <= len(match) {
158+
paramsMap[name] = match[i]
159+
}
160+
}
161+
res = append(res, paramsMap)
162+
}
163+
return res, str
164+
}

models/MessageReq.go renamed to models/TelegramMessageReq.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package models
22

3-
type MessageReq struct {
3+
type TelgramMessageReq struct {
44
UpdateID int `json:"update_id"`
55
Message struct {
66
MessageID int `json:"message_id"`

models/TelegramMessageRes.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package models
2+
3+
type TelegramMessageRes struct {
4+
ChatID string `json:"chat_id"`
5+
Text string `json:"text"`
6+
ReplyMarkup TelegramMessageResReplyMarkup `json:"reply_markup"`
7+
}
8+
9+
type TelegramMessageResReplyMarkup struct {
10+
OneTimeKeyboard bool `json:"one_time_keyboard"`
11+
ResizeKeyboard bool `json:"resize_keyboard"`
12+
Keyboard [][]TelegramMessageResReplyMarkupKeyboard `json:"keyboard"`
13+
}
14+
15+
type TelegramMessageResReplyMarkupKeyboard struct {
16+
Text string `json:"text"`
17+
}
18+

0 commit comments

Comments
 (0)