/
events.go
25 lines (19 loc) · 891 Bytes
/
events.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
package telegram
import (
"reflect"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
func messageCondition(message tgbotapi.Message, messageText string) bool {
return reflect.TypeOf(message.Text).Kind() == reflect.String && message.Text == messageText
}
// OnMessage registers callback on message
func (bot *Bot) OnMessage(messageText string, callback OnMessageCallback) {
bot.OnCondition(func(message tgbotapi.Message) bool { return messageCondition(message, messageText) }, callback)
}
// OnCondition registers callback on any message that satisfies condition
func (bot *Bot) OnCondition(condition Condition, callback OnMessageCallback) {
if bot.conditions[&condition] == nil {
bot.conditions[&condition] = make([]func(message tgbotapi.Message, botAPI *tgbotapi.BotAPI, bot *Bot), 0)
}
bot.conditions[&condition] = append(bot.conditions[&condition], callback)
}