@@ -6,11 +6,12 @@ import (
6
6
"log"
7
7
"net/http"
8
8
"os"
9
+ "regexp"
9
10
"strconv"
10
11
"strings"
11
12
12
13
fiber "github.com/gofiber/fiber/v2"
13
- "github.com/snehalyelmati/telegram-bot-golang/models"
14
+ . "github.com/snehalyelmati/telegram-bot-golang/models"
14
15
DialogFlow "github.com/snehalyelmati/telegram-bot-golang/services"
15
16
)
16
17
@@ -42,12 +43,14 @@ func main() {
42
43
43
44
app .Post (URI , func (c * fiber.Ctx ) error {
44
45
l .Println ("Post request" )
45
- reqBody := new (models. MessageReq )
46
+ reqBody := new (TelgramMessageReq )
46
47
json .Unmarshal (c .Body (), reqBody )
47
48
l .Printf ("%v" , reqBody )
48
49
50
+ // initialize stuff
49
51
inputMessage := reqBody .Message .Text
50
52
if inputMessage == "/start" {
53
+ // save the user data
51
54
inputMessage = "hi"
52
55
}
53
56
@@ -58,6 +61,12 @@ func main() {
58
61
// })
59
62
60
63
// 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
+
61
70
sessionID := strconv .Itoa (reqBody .Message .Chat .ID )
62
71
response , queryResult , err := DialogFlow .DetectIntentText (PROJECT_ID , sessionID , inputMessage , LANGUAGE )
63
72
if err != nil {
@@ -77,6 +86,43 @@ func main() {
77
86
l .Println (err )
78
87
}
79
88
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
80
126
_ , err = http .Post (TELEGRAM_API + "/sendMessage" , "application/json" , bytes .NewBuffer (data ))
81
127
if err != nil {
82
128
l .Println (err )
@@ -89,3 +135,30 @@ func main() {
89
135
app .Listen (":3000" )
90
136
l .Println ("Server running on port 3000" )
91
137
}
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
+ }
0 commit comments