forked from go-telegram/bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversationHandler.go
55 lines (43 loc) · 1.13 KB
/
conversationHandler.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
package bot
import "github.com/gokhanaltun/go-telegram-bot/models"
type ConversationStage map[int]HandlerFunc
type ConversationEnd struct {
Command string
Function HandlerFunc
}
type ConversationHandler struct {
active bool
activeStageId int
stages ConversationStage
end *ConversationEnd
}
func NewConversationHandler(stages_ ConversationStage, end_ *ConversationEnd) *ConversationHandler {
return &ConversationHandler{
stages: stages_,
end: end_,
}
}
func (b *Bot) AddConversationHandler(conversationHandler_ *ConversationHandler) {
b.conversationHandler = conversationHandler_
}
func (b *Bot) SetActiveConversationStage(stageId_ int) {
if !b.conversationHandler.active {
b.conversationHandler.active = true
}
b.conversationHandler.activeStageId = stageId_
}
func (c *ConversationHandler) getStageFunction(upd *models.Update) HandlerFunc {
if c.active {
if upd.Message.Text == c.end.Command {
c.active = false
return c.end.Function
}
if hf, ok := c.stages[c.activeStageId]; ok {
return hf
}
}
return nil
}
func (b *Bot) EndConversation() {
b.conversationHandler.active = false
}