-
Notifications
You must be signed in to change notification settings - Fork 25
/
interfaces.go
326 lines (280 loc) · 9.63 KB
/
interfaces.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package botsfw
import (
"context"
"net/http"
"time"
)
// BotPlatform describes current bot platform
type BotPlatform interface {
// ID returns bot platform ID like 'telegram', 'fbmessenger', 'viber', etc.
ID() string
// Version returns a version of a bot platform adapter. It is used for debugging purposes.
Version() string
}
// BotHost describes current bot app host environment
type BotHost interface {
// Context returns a context.Context for a request. We need this as some platforms (as Google App Engine Standard)
// require usage of a context with a specific wrapper
Context(r *http.Request) context.Context
// GetHTTPClient returns HTTP client for current host
// We need this as some platforms (as Google App Engine Standard) require setting http client in a specific way.
GetHTTPClient(c context.Context) *http.Client
}
// BotContext describes a bot on a specific platform
type BotContext struct {
BotHost BotHost // describes current bot app host environment
BotSettings BotSettings // keeps parameters of a bot that are static and are not changed in runtime
}
// NewBotContext creates current bot host & settings
func NewBotContext(host BotHost, settings BotSettings) *BotContext {
if settings.Code == "" {
panic("ReferredTo settings.Code is empty string")
}
return &BotContext{
BotHost: host,
BotSettings: settings,
}
}
// WebhookEntry represents a single message from a messenger user
type WebhookEntry interface {
GetID() interface{}
GetTime() time.Time
}
// WebhookInputType is enum of input type
type WebhookInputType int
const (
// WebhookInputUnknown is unknown input type
WebhookInputUnknown WebhookInputType = iota
// WebhookInputNotImplemented is not implemented input type
WebhookInputNotImplemented
// WebhookInputText is text input type
WebhookInputText // Facebook, Telegram, Viber
// WebhookInputVoice is voice input type
WebhookInputVoice
// WebhookInputPhoto is photo input type
WebhookInputPhoto
// WebhookInputAudio is audio input type
WebhookInputAudio
// WebhookInputContact is contact input type
WebhookInputContact // Facebook, Telegram, Viber
// WebhookInputPostback is unknown input type
WebhookInputPostback
// WebhookInputDelivery is postback input type
WebhookInputDelivery
// WebhookInputAttachment is delivery report input type
WebhookInputAttachment
// WebhookInputInlineQuery is attachment input type
WebhookInputInlineQuery // Telegram
// WebhookInputCallbackQuery is inline input type
WebhookInputCallbackQuery
// WebhookInputReferral is callback input type
WebhookInputReferral // FBM
// WebhookInputChosenInlineResult is chosen inline result input type
WebhookInputChosenInlineResult // Telegram
// WebhookInputSubscribed is subscribed input type
WebhookInputSubscribed // Viber
// WebhookInputUnsubscribed is unsubscribed input type
WebhookInputUnsubscribed // Viber
// WebhookInputConversationStarted is converstation started input type
WebhookInputConversationStarted // Viber
// WebhookInputNewChatMembers is new botChat memebers input type
WebhookInputNewChatMembers // Telegram groups
// WebhookInputLeftChatMembers is left botChat members input type
WebhookInputLeftChatMembers
// WebhookInputSticker is sticker input type
WebhookInputSticker // Telegram
)
// WebhookInputTypeNames names for input type
var WebhookInputTypeNames = map[WebhookInputType]string{
//WebhookInputContact: "Contact",
WebhookInputUnknown: "unknown",
WebhookInputNotImplemented: "not implemented",
WebhookInputReferral: "Referral",
WebhookInputText: "Text",
WebhookInputContact: "Contact",
WebhookInputPostback: "Postback",
WebhookInputDelivery: "Delivery",
WebhookInputAttachment: "Attachment",
WebhookInputInlineQuery: "InlineQuery",
WebhookInputCallbackQuery: "CallbackQuery",
WebhookInputChosenInlineResult: "ChosenInlineResult",
WebhookInputSubscribed: "Subscribed", // Viber
WebhookInputUnsubscribed: "Unsubscribed", // Viber
WebhookInputConversationStarted: "ConversationStarted", // Telegram
WebhookInputNewChatMembers: "NewChatMembers", // Telegram
}
// WebhookInput represent a single message
// '/entry/messaging' for Facebook Messenger
type WebhookInput interface {
GetSender() WebhookSender
GetRecipient() WebhookRecipient
GetTime() time.Time
InputType() WebhookInputType
BotChatID() (string, error)
Chat() WebhookChat
LogRequest()
}
// WebhookActor represents sender
type WebhookActor interface {
Platform() string // TODO: Consider removing this?
GetID() interface{}
IsBotUser() bool
GetFirstName() string
GetLastName() string
GetUserName() string
GetLanguage() string
}
// WebhookSender represents sender with avatar
type WebhookSender interface {
GetAvatar() string // Extension to support avatar (Viber)
WebhookActor
}
// WebhookUser represents sender with country
type WebhookUser interface {
WebhookSender
// GetCountry is an extension to support language & country (Viber)
GetCountry() string
}
// WebhookRecipient represents receiver
type WebhookRecipient interface {
WebhookActor
}
// WebhookMessage represents single message
type WebhookMessage interface {
IntID() int64
StringID() string
Chat() WebhookChat
//Sequence() int // 'seq' for Facebook, '???' for Telegram
}
// WebhookTextMessage represents single text message
type WebhookTextMessage interface {
WebhookMessage
Text() string
IsEdited() bool
}
// WebhookStickerMessage represents single sticker message
type WebhookStickerMessage interface {
WebhookMessage
// TODO: Define sticker message interface
}
// WebhookVoiceMessage represents single voice message
type WebhookVoiceMessage interface {
WebhookMessage
// TODO: Define voice message interface
}
// WebhookPhotoMessage represents single photo message
type WebhookPhotoMessage interface {
WebhookMessage
// TODO: Define photo message interface
}
// WebhookAudioMessage represents single audio message
type WebhookAudioMessage interface {
WebhookMessage
// TODO: Define audio message interface
}
// WebhookReferralMessage represents single referral message
// https://developers.facebook.com/docs/messenger-platform/webhook-reference/referral
type WebhookReferralMessage interface {
Type() string
Source() string
RefData() string
}
// WebhookContactMessage represents single contact message
type WebhookContactMessage interface {
PhoneNumber() string
FirstName() string
LastName() string
UserID() interface{}
}
// WebhookNewChatMembersMessage represents single message about a new member of a botChat
type WebhookNewChatMembersMessage interface {
BotChatID() (string, error)
NewChatMembers() []WebhookActor
}
// WebhookLeftChatMembersMessage represents single message about a member leaving a botChat
type WebhookLeftChatMembersMessage interface {
BotChatID() (string, error)
LeftChatMembers() []WebhookActor
}
// WebhookChat represents botChat of a messenger
type WebhookChat interface {
GetID() string
GetType() string
IsGroupChat() bool
}
// WebhookPostback represents single postback message
type WebhookPostback interface {
PostbackMessage() interface{}
Payload() string
}
// WebhookSubscribed represents a subscription message
type WebhookSubscribed interface {
SubscribedMessage() interface{}
}
// WebhookUnsubscribed represents a message when user unsubscribe
type WebhookUnsubscribed interface {
UnsubscribedMessage() interface{}
}
// WebhookConversationStarted represents a single message about new conversation
type WebhookConversationStarted interface {
ConversationStartedMessage() interface{}
}
// WebhookInlineQuery represents a single inline message
type WebhookInlineQuery interface {
GetID() interface{}
GetInlineQueryID() string
GetFrom() WebhookSender
GetQuery() string
GetOffset() string
//GetLocation() - TODO: Not implemented yet
}
// WebhookDelivery represents a single delivery report message
type WebhookDelivery interface {
Payload() string
}
// WebhookChosenInlineResult represents a single report message on chosen inline result
type WebhookChosenInlineResult interface {
GetResultID() string
GetInlineMessageID() string // Telegram only?
GetFrom() WebhookSender
GetQuery() string
//GetLocation() - TODO: Not implemented yet
}
// WebhookCallbackQuery represents a single callback query message
type WebhookCallbackQuery interface {
GetID() string
GetInlineMessageID() string // Telegram only?
GetFrom() WebhookSender
GetMessage() WebhookMessage
GetData() string
Chat() WebhookChat
}
// WebhookAttachment represents attachment to a message
type WebhookAttachment interface {
Type() string // Enum(image, video, audio) for Facebook
PayloadUrl() string // 'payload.url' for Facebook
}
// MessengerResponse represents response from a messenger
type MessengerResponse interface {
}
// OnMessageSentResponse represents response on message sent event
type OnMessageSentResponse struct {
StatusCode int
TelegramMessage MessengerResponse // TODO: change to some interface
}
// WebhookResponder is an API provider to send messages through a messenger
type WebhookResponder interface {
SendMessage(c context.Context, m MessageFromBot, channel BotAPISendMessageChannel) (OnMessageSentResponse, error)
}
// InputMessage represents single input message
type InputMessage interface {
Text() string
}
// BotAPISendMessageChannel specifies messenger channel
type BotAPISendMessageChannel string
const (
// BotAPISendMessageOverHTTPS indicates message should be sent over HTTPS
BotAPISendMessageOverHTTPS = BotAPISendMessageChannel("https")
// BotAPISendMessageOverResponse indicates message should be sent in HTTP response
BotAPISendMessageOverResponse = BotAPISendMessageChannel("response")
)