-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtelego.go
147 lines (130 loc) · 4.23 KB
/
telego.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
package telego
import (
"log"
"github.com/davilag/telego/api"
"github.com/davilag/telego/kind"
"github.com/davilag/telego/metrics"
)
// Telego is the main struct on which we can define all the flows and handlers.
type Telego struct {
defaultHandler FlowStep // Default handler which is going to be executed for those messages that don't have any flow assigned
kindFlows map[kind.Kind]Flow // Flows that are going to be executed based on the kind of the message
commandFlows map[string]Flow // Flows that are going to be executed based on the command that the message has
updates chan api.Update // Channel on which we have to send the updates to be processed
Client TelegramClient // Client allows send messages to Telegram chats
}
var (
metricMessageSent = "telego_message_sent"
metricMessageReceived = "telego_message_received"
metricSession = "telego_sessions"
telegramHost = "https://api.telegram.org/bot"
produceMetrics = false
)
// Initialise inits the telegram instance with the telegram bot access token.
// See https://core.telegram.org/bots/api#authorizing-your-bot
func Initialise(accessToken string) *Telego {
log.Println("Local telego")
client := TelegramClient{
AccessToken: accessToken,
}
telego := Telego{
kindFlows: map[kind.Kind]Flow{},
commandFlows: map[string]Flow{},
Client: client,
}
updates, _ := newSessionManager(&telego)
telego.updates = updates
return &telego
}
// SetDefaultMessageHandler Sets the default message handler for the telegram bot. It defines
// what we are going to do with messages that by default the bot doesn't understand
// (eg. send a description of the commands)
func (t *Telego) SetDefaultMessageHandler(f FlowStep) {
t.defaultHandler = f
}
// AddKindHandler adds the step that it is going to be executed when we receive a message
// of a certain kind
func (t *Telego) AddKindHandler(k kind.Kind, fs FlowStep) {
t.AddKindHandlerSession(k, fs, 0)
}
// AddKindHandlerSession adds the step that it is going to be executed when we receive
// a message of a certain kind, keeping the information that the handler saves for the
// time defined in ttl
func (t *Telego) AddKindHandlerSession(k kind.Kind, fs FlowStep, ttl int32) {
f := Flow{
ActualStep: fs,
}
t.kindFlows[k] = f
}
// AddCommandHandler adds the step that it is going to be executed when we receive a certain command
func (t *Telego) AddCommandHandler(c string, fs FlowStep) {
t.AddCommandHandlerSession(c, fs, 0)
}
// AddCommandHandlerSession adds the step that it is going to be executed when we receive
// a message certain command, keeping the information that the handler saves for the
// time defined in ttl
func (t *Telego) AddCommandHandlerSession(c string, fs FlowStep, ttl int32) {
f := Flow{
ActualStep: fs,
TimeToLive: ttl,
}
t.commandFlows[c] = f
}
// Listen main loop which is going to be listening for updates.
func (t *Telego) Listen() {
var offset int
fetch := true
for fetch {
us := t.Client.getUpdates(offset)
for _, u := range us {
addMessageReceivedMetric()
t.updates <- u
offset = u.UpdateID + 1
}
}
}
// SetupMetrics sets up the metrics for a telegram bot. It exposes metrics when
// the bot sends a message, when the bot receives a message and the sessions that
// the bot is keeping with chat information.
func (t *Telego) SetupMetrics() {
metrics.AddCounter(metricMessageSent, "Telego sending a message")
metrics.AddCounter(metricMessageReceived, "Telego receiving a message")
metrics.AddGauge(metricSession, "Sessions that telego is keeping waiting for messages")
go metrics.ExposeMetrics()
}
// SetTelegramHost sets the telegram host where telegram is running
func (t *Telego) SetTelegramHost(tm string) {
telegramHost = tm
}
func addMessageSentMetric() {
if produceMetrics {
m, ok := metrics.GetCounter(metricMessageSent)
if ok {
m.Inc()
}
}
}
func addMessageReceivedMetric() {
if produceMetrics {
m, ok := metrics.GetCounter(metricMessageReceived)
if ok {
m.Inc()
}
}
}
func addSessionMetric() {
if produceMetrics {
m, ok := metrics.GetGauge(metricSession)
if ok {
m.Inc()
}
}
}
func finishSessionMetric() {
if produceMetrics {
m, ok := metrics.GetGauge(metricSession)
if ok {
m.Dec()
}
}
}