@@ -9,29 +9,51 @@ import (
9
9
"time"
10
10
)
11
11
12
- var _ botinput.InputMessage = (* tgWebhookInput )(nil )
12
+ var _ botinput.InputMessage = (* tgInput )(nil )
13
13
14
- type tgWebhookInput struct {
14
+ type tgInput struct {
15
15
update * tgbotapi.Update
16
16
logRequest func ()
17
17
}
18
18
19
- func (whi tgWebhookInput ) BotChatID () (string , error ) {
19
+ func (whi tgInput ) BotChatID () (string , error ) {
20
20
tgChat := whi .update .Chat ()
21
21
if tgChat == nil {
22
22
return "" , nil
23
23
}
24
24
return strconv .FormatInt (tgChat .ID , 10 ), nil
25
25
}
26
26
27
- func (whi tgWebhookInput ) InputType () botinput.Type {
27
+ func (whi tgInput ) InputType () botinput.Type {
28
+
29
+ getMessageType := func (m * tgbotapi.Message , defaultType botinput.Type ) botinput.Type {
30
+ switch {
31
+ case m .Location != nil :
32
+ return botinput .TypeLocation
33
+ case m .SuccessfulPayment != nil :
34
+ return botinput .TypeSuccessfulPayment
35
+
36
+ case m .RefundedPayment != nil :
37
+ return botinput .TypeRefundedPayment
38
+ default :
39
+ return botinput .TypeText
40
+ }
41
+ }
42
+
28
43
switch {
29
44
case whi .update .InlineQuery != nil :
30
45
return botinput .TypeInlineQuery
31
46
32
47
case whi .update .CallbackQuery != nil :
33
48
return botinput .TypeCallbackQuery
34
49
50
+ case whi .update .Message != nil :
51
+ return getMessageType (whi .update .Message , botinput .TypeText )
52
+
53
+ case whi .update .EditedMessage != nil :
54
+ // This should be after any whi.update.Message.* checks
55
+ return getMessageType (whi .update .EditedMessage , botinput .TypeEditText )
56
+
35
57
case whi .update .ChosenInlineResult != nil :
36
58
return botinput .TypeChosenInlineResult
37
59
@@ -41,16 +63,6 @@ func (whi tgWebhookInput) InputType() botinput.Type {
41
63
case whi .update .PreCheckoutQuery != nil :
42
64
return botinput .TypePreCheckoutQuery
43
65
44
- case whi .update .Message .SuccessfulPayment != nil :
45
- return botinput .TypeSuccessfulPayment
46
-
47
- case whi .update .Message .RefundedPayment != nil :
48
- return botinput .TypeRefundedPayment
49
-
50
- case whi .update .Message != nil || whi .update .EditedMessage != nil :
51
- // This should be after any whi.update.Message.* checks
52
- return botinput .TypeText
53
-
54
66
default :
55
67
return botinput .TypeUnknown
56
68
}
@@ -61,35 +73,35 @@ type TgWebhookInput interface {
61
73
TgUpdate () * tgbotapi.Update
62
74
}
63
75
64
- func (whi tgWebhookInput ) LogRequest () {
76
+ func (whi tgInput ) LogRequest () {
65
77
if whi .logRequest != nil {
66
78
whi .logRequest ()
67
79
}
68
80
}
69
81
70
- var _ TgWebhookInput = (* tgWebhookInput )(nil )
82
+ var _ TgWebhookInput = (* tgInput )(nil )
71
83
72
84
// tgWebhookUpdateProvider indicates that input can provide original Telegram update struct
73
85
type tgWebhookUpdateProvider interface {
74
86
TgUpdate () * tgbotapi.Update
75
87
}
76
88
77
- func (whi tgWebhookInput ) TgUpdate () * tgbotapi.Update {
89
+ func (whi tgInput ) TgUpdate () * tgbotapi.Update {
78
90
return whi .update
79
91
}
80
92
81
93
var _ botinput.InputMessage = (* tgWebhookTextMessage )(nil )
82
94
var _ botinput.InputMessage = (* tgWebhookContactMessage )(nil )
83
95
var _ botinput.InputMessage = (* TgWebhookInlineQuery )(nil )
84
96
var _ botinput.InputMessage = (* tgWebhookChosenInlineResult )(nil )
85
- var _ botinput.InputMessage = (* TgWebhookCallbackQuery )(nil )
97
+ var _ botinput.InputMessage = (* callbackQueryInput )(nil )
86
98
var _ botinput.InputMessage = (* tgWebhookNewChatMembersMessage )(nil )
87
99
88
- func (whi tgWebhookInput ) GetID () interface {} {
100
+ func (whi tgInput ) GetID () interface {} {
89
101
return whi .update .UpdateID
90
102
}
91
103
92
- func message2input (input tgWebhookInput , tgMessageType TgMessageType , tgMessage * tgbotapi.Message ) botinput.InputMessage {
104
+ func message2input (input tgInput , tgMessageType MessageType , tgMessage * tgbotapi.Message ) botinput.InputMessage {
93
105
switch {
94
106
case tgMessage .Text != "" :
95
107
return newTgWebhookTextMessage (input , tgMessageType , tgMessage )
@@ -109,14 +121,16 @@ func message2input(input tgWebhookInput, tgMessageType TgMessageType, tgMessage
109
121
return newTgWebhookStickerMessage (input , tgMessageType , tgMessage )
110
122
case tgMessage .UsersShared != nil :
111
123
return newTgWebhookUsersSharedMessage (input , tgMessageType , tgMessage )
124
+ case tgMessage .Location != nil :
125
+ return newLocationMessage (input , tgMessage )
112
126
default :
113
127
return nil
114
128
}
115
129
}
116
130
117
131
// NewTelegramWebhookInput maps telegram update struct to bots framework interface
118
132
func NewTelegramWebhookInput (update * tgbotapi.Update , logRequest func ()) (botinput.InputMessage , error ) {
119
- input := tgWebhookInput {update : update , logRequest : logRequest }
133
+ input := tgInput {update : update , logRequest : logRequest }
120
134
121
135
switch inputType := input .InputType (); inputType {
122
136
case botinput .TypeInlineQuery :
@@ -131,13 +145,15 @@ func NewTelegramWebhookInput(update *tgbotapi.Update, logRequest func()) (botinp
131
145
return newTgWebhookSuccessfulPayment (input ), nil
132
146
case botinput .TypeRefundedPayment :
133
147
return newTgWebhookRefundedPayment (input ), nil
148
+ case botinput .TypeLocation :
149
+ return newLocationMessage (input , update .Message ), nil
134
150
case botinput .TypeText :
135
151
switch {
136
152
case update .Message != nil :
137
- return message2input (input , TgMessageTypeRegular , update .Message ), nil
153
+ return message2input (input , MessageTypeRegular , update .Message ), nil
138
154
139
155
case update .EditedMessage != nil :
140
- return message2input (input , TgMessageTypeEdited , update .EditedMessage ), nil
156
+ return message2input (input , MessageTypeEdited , update .EditedMessage ), nil
141
157
142
158
}
143
159
case botinput .TypeNotImplemented :
@@ -164,7 +180,7 @@ func NewTelegramWebhookInput(update *tgbotapi.Update, logRequest func()) (botinp
164
180
return nil , botsfw .ErrNotImplemented
165
181
}
166
182
167
- func (whi tgWebhookInput ) GetSender () botinput.User {
183
+ func (whi tgInput ) GetSender () botinput.User {
168
184
switch {
169
185
case whi .update .Message != nil :
170
186
return tgWebhookUser {tgUser : whi .update .Message .From }
@@ -193,11 +209,11 @@ func (whi tgWebhookInput) GetSender() botinput.User {
193
209
}
194
210
}
195
211
196
- func (whi tgWebhookInput ) GetRecipient () botinput.Recipient {
212
+ func (whi tgInput ) GetRecipient () botinput.Recipient {
197
213
panic ("Not implemented" )
198
214
}
199
215
200
- func (whi tgWebhookInput ) GetTime () time.Time {
216
+ func (whi tgInput ) GetTime () time.Time {
201
217
if whi .update .Message != nil {
202
218
return whi .update .Message .Time ()
203
219
}
@@ -207,7 +223,7 @@ func (whi tgWebhookInput) GetTime() time.Time {
207
223
return time.Time {}
208
224
}
209
225
210
- func (whi tgWebhookInput ) MessageIntID () int {
226
+ func (whi tgInput ) MessageIntID () int {
211
227
switch {
212
228
case whi .update .CallbackQuery != nil :
213
229
return whi .update .CallbackQuery .Message .MessageID
@@ -223,15 +239,15 @@ func (whi tgWebhookInput) MessageIntID() int {
223
239
return 0
224
240
}
225
241
226
- func (whi tgWebhookInput ) MessageStringID () string {
242
+ func (whi tgInput ) MessageStringID () string {
227
243
messageID := whi .MessageIntID ()
228
244
if messageID == 0 {
229
245
return ""
230
246
}
231
247
return strconv .Itoa (messageID )
232
248
}
233
249
234
- func (whi tgWebhookInput ) TelegramChatID () int64 {
250
+ func (whi tgInput ) TelegramChatID () int64 {
235
251
if whi .update .Message != nil {
236
252
return whi .update .Message .Chat .ID
237
253
}
@@ -241,18 +257,18 @@ func (whi tgWebhookInput) TelegramChatID() int64 {
241
257
panic ("Can't get Telegram chat ID from `update.Message` or `update.EditedMessage`." )
242
258
}
243
259
244
- func (whi tgWebhookInput ) Chat () botinput.Chat {
260
+ func (whi tgInput ) Chat () botinput.Chat {
245
261
update := whi .update
246
262
if update .Message != nil {
247
- return TgWebhookChat {
263
+ return TgChat {
248
264
chat : update .Message .Chat ,
249
265
}
250
266
} else if update .EditedMessage != nil {
251
- return TgWebhookChat {
267
+ return TgChat {
252
268
chat : update .EditedMessage .Chat ,
253
269
}
254
270
} else if callbackQuery := update .CallbackQuery ; callbackQuery != nil && callbackQuery .Message != nil {
255
- return TgWebhookChat {
271
+ return TgChat {
256
272
chat : callbackQuery .Message .Chat ,
257
273
}
258
274
}
0 commit comments