-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg_bot.go
194 lines (170 loc) · 7.22 KB
/
tg_bot.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package tg_bot
import (
"log"
"net/http"
"time"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/conversation"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters/callbackquery"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters/message"
"github.com/l-orlov/slim-fairy/bot/internal/config"
"github.com/l-orlov/slim-fairy/bot/internal/store"
"github.com/l-orlov/slim-fairy/bot/internal/tg-bot/logic-handlers"
"github.com/pkg/errors"
)
/* TODO:
метод для обновления данных
*/
type (
Bot struct {
bot *gotgbot.Bot
updater *ext.Updater
}
)
func New(
aiClient logic_handlers.AIClient,
storage *store.Storage,
) (*Bot, error) {
cfg := config.Get()
b, err := gotgbot.NewBot(cfg.Token, &gotgbot.BotOpts{
Client: http.Client{},
DefaultRequestOpts: &gotgbot.RequestOpts{
Timeout: gotgbot.DefaultTimeout,
APIURL: gotgbot.DefaultAPIURL,
},
})
if err != nil {
return nil, errors.Wrap(err, "gotgbot.NewBot")
}
// Create logic handlers
logicHandlers := logic_handlers.New(aiClient, storage)
// Create updater and dispatcher.
updater := ext.NewUpdater(&ext.UpdaterOpts{
Dispatcher: ext.NewDispatcher(&ext.DispatcherOpts{
// If an error is returned by a handler, log it and continue going.
Error: func(b *gotgbot.Bot, ctx *ext.Context, err error) ext.DispatcherAction {
log.Println("an error occurred while handling update:", err.Error())
return ext.DispatcherActionNoop
},
MaxRoutines: ext.DefaultMaxRoutines,
}),
})
dispatcher := updater.Dispatcher
// command to introduce the bot
dispatcher.AddHandler(handlers.NewCommand("start", logicHandlers.Start))
// command to register user
dispatcher.AddHandler(handlers.NewConversation(
[]ext.Handler{handlers.NewCommand("register", logicHandlers.StartUserRegistration)},
map[string][]ext.Handler{
logic_handlers.RegisterName: {handlers.NewMessage(noCommands, logicHandlers.RegisterUserName)},
logic_handlers.RegisterAge: {handlers.NewMessage(noCommands, logicHandlers.RegisterUserAge)},
logic_handlers.RegisterWeight: {handlers.NewMessage(noCommands, logicHandlers.RegisterUserWeight)},
logic_handlers.RegisterHeight: {handlers.NewMessage(noCommands, logicHandlers.RegisterUserHeight)},
logic_handlers.RegisterGender: {
handlers.NewCallback(callbackquery.Equal(logic_handlers.RegisterGenderCbMale), logicHandlers.RegisterUserGenderMale),
handlers.NewCallback(callbackquery.Equal(logic_handlers.RegisterGenderCbFemale), logicHandlers.RegisterUserGenderFemale),
},
logic_handlers.RegisterPhysicalActivity: {
handlers.NewCallback(callbackquery.Equal(logic_handlers.RegisterPhysicalActivityCbLow), logicHandlers.RegisterUserPhysicalActivityLow),
handlers.NewCallback(callbackquery.Equal(logic_handlers.RegisterPhysicalActivityCbMedium), logicHandlers.RegisterUserPhysicalActivityMedium),
handlers.NewCallback(callbackquery.Equal(logic_handlers.RegisterPhysicalActivityCbHigh), logicHandlers.RegisterUserPhysicalActivityHigh),
},
logic_handlers.RegisterConfirm: {
handlers.NewCallback(callbackquery.Equal(logic_handlers.RegisterConfirmCbYes), logicHandlers.ConfirmUserRegistrationYes),
handlers.NewCallback(callbackquery.Equal(logic_handlers.RegisterConfirmCbNo), logicHandlers.ConfirmUserRegistrationNo),
},
},
&handlers.ConversationOpts{
Exits: []ext.Handler{handlers.NewCommand("cancel", logicHandlers.CancelUserRegistration)},
StateStorage: conversation.NewInMemoryStorage(conversation.KeyStrategySenderAndChat),
},
))
// command to get diet menu from AI
dispatcher.AddHandler(handlers.NewConversation(
[]ext.Handler{handlers.NewCommand("getdietfromai", logicHandlers.StartGettingDietFromAI)},
map[string][]ext.Handler{
logic_handlers.GetDietFromAISelectMeals: {
handlers.NewCallback(callbackquery.Equal(logic_handlers.GetDietFromAICbSelectMeals2), logicHandlers.SelectMeals2),
handlers.NewCallback(callbackquery.Equal(logic_handlers.GetDietFromAICbSelectMeals3), logicHandlers.SelectMeals3),
},
logic_handlers.GetDietFromAISelectSnacks: {
handlers.NewCallback(callbackquery.Equal(logic_handlers.GetDietFromAICbSelectSnacks0), logicHandlers.SelectSnacks0),
handlers.NewCallback(callbackquery.Equal(logic_handlers.GetDietFromAICbSelectSnacks1), logicHandlers.SelectSnacks1),
handlers.NewCallback(callbackquery.Equal(logic_handlers.GetDietFromAICbSelectSnacks2), logicHandlers.SelectSnacks2),
},
logic_handlers.GetDietFromAISelectOrderProducts: {
handlers.NewCallback(callbackquery.Equal(logic_handlers.GetDietFromAISelectOrderProductsYes), logicHandlers.SelectOrderProductsYes),
handlers.NewCallback(callbackquery.Equal(logic_handlers.GetDietFromAISelectOrderProductsNo), logicHandlers.SelectOrderProductsNo),
},
},
&handlers.ConversationOpts{
Exits: []ext.Handler{handlers.NewCommand("cancel", logicHandlers.CancelGettingDiet)},
StateStorage: conversation.NewInMemoryStorage(conversation.KeyStrategySenderAndChat),
},
))
// command to get diet menu from nutritionist
dispatcher.AddHandler(handlers.NewCommand("getdiet", logicHandlers.StartGettingDiet))
return &Bot{
bot: b,
updater: updater,
}, nil
}
func (b *Bot) Run() {
cfg := config.Get()
if len(cfg.WebhookDomain) != 0 && len(cfg.WebhookSecret) != 0 && len(cfg.WebhookAddress) != 0 {
b.startWebhook()
}
b.startPolling()
}
func (b *Bot) startWebhook() {
cfg := config.Get()
// Start the webhook server. We start the server before we set the webhook itself, so that when telegram starts
// sending updates, the server is already ready.
webhookOpts := ext.WebhookOpts{
ListenAddr: cfg.WebhookAddress,
SecretToken: cfg.WebhookSecret, // Setting a webhook secret here allows you to ensure the webhook is set by you (must be set here AND in SetWebhook!).
}
// We use the token as the urlPath for the webhook, as using a secret ensures that strangers aren't crafting fake updates.
err := b.updater.StartWebhook(b.bot, cfg.Token, webhookOpts)
if err != nil {
panic("failed to start webhook: " + err.Error())
}
err = b.updater.SetAllBotWebhooks(cfg.WebhookDomain, &gotgbot.SetWebhookOpts{
MaxConnections: 100,
DropPendingUpdates: true,
SecretToken: webhookOpts.SecretToken,
})
if err != nil {
panic("failed to set webhook: " + err.Error())
}
log.Printf("%s has been started with webhook...\n", b.bot.User.Username)
// Idle, to keep updates coming in, and avoid bot stopping.
b.updater.Idle()
}
func (b *Bot) startPolling() {
// Start receiving updates.
err := b.updater.StartPolling(b.bot, &ext.PollingOpts{
DropPendingUpdates: true,
GetUpdatesOpts: gotgbot.GetUpdatesOpts{
Timeout: 9,
RequestOpts: &gotgbot.RequestOpts{
Timeout: time.Second * 10,
},
},
})
if err != nil {
panic("failed to start polling: " + err.Error())
}
log.Printf("%s has been started with polling...\n", b.bot.User.Username)
// Idle, to keep updates coming in, and avoid bot stopping.
b.updater.Idle()
}
func (b *Bot) Stop() error {
return b.updater.Stop()
}
// Create a matcher which only matches text which is not a command
func noCommands(msg *gotgbot.Message) bool {
return message.Text(msg) && !message.Command(msg)
}