-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsum_session_test.go
58 lines (48 loc) · 1.42 KB
/
sum_session_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package margelet_test
import (
"fmt"
"strconv"
"../margelet"
"gopkg.in/telegram-bot-api.v4"
)
// SumSession - simple example session, that can sum numbers
type SumSession struct {
}
// HandleResponse - Handlers user response
func (s SumSession) HandleSession(session margelet.Session) error {
switch len(session.Responses()) {
case 0:
session.QuickReply("Hello, please, write one number per message, after some iterations write 'end'.", nil)
default:
if session.Message().Text == "end" {
var sum int
for _, m := range session.Responses() {
n, _ := strconv.Atoi(m.Text)
sum += n
}
session.QuickReply(fmt.Sprintf("Your sum: %d", sum), nil)
session.Finish()
return nil
}
_, err := strconv.Atoi(session.Message().Text)
if err != nil {
session.QuickReply("Sorry, not a number", nil)
return err
}
}
return nil
}
// CancelResponse - Chance to clean up everything
func (s SumSession) CancelSession(session margelet.Session) {
//Clean up all variables only used in the session
}
func (session SumSession) response(bot margelet.MargeletAPI, message *tgbotapi.Message, msg tgbotapi.MessageConfig) {
msg.ChatID = message.Chat.ID
msg.ReplyToMessageID = message.MessageID
msg.ReplyMarkup = tgbotapi.ForceReply{ForceReply: true, Selective: true}
bot.Send(msg)
}
// HelpMessage return help string for SumSession
func (session SumSession) HelpMessage() string {
return "Sum your numbers and print result"
}