@@ -76,13 +76,16 @@ func newTgWebhookResponder(w http.ResponseWriter, whc *tgWebhookContext) tgWebho
76
76
77
77
func (r tgWebhookResponder ) SendMessage (ctx context.Context , m botmsg.MessageFromBot , channel botmsg.BotAPISendMessageChannel ) (resp botsfw.OnMessageSentResponse , err error ) {
78
78
logus .Debugf (ctx , "tgWebhookResponder.SendMessage(channel=%v, isEdit=%v)\n m: %+v" , channel , m .IsEdit , m )
79
- channel = botsfw .BotAPISendMessageOverHTTPS
80
79
switch channel {
81
80
case botsfw .BotAPISendMessageOverHTTPS , botsfw .BotAPISendMessageOverResponse :
82
81
// Known channels
83
82
default :
84
83
panic (fmt .Sprintf ("Unknown channel: [%v]. Expected either 'https' or 'response'." , channel ))
85
84
}
85
+ if channel != botsfw .BotAPISendMessageOverHTTPS {
86
+ logus .Debugf (ctx , "Rewriting sending channel from %s to %s" , channel , botsfw .BotAPISendMessageOverHTTPS )
87
+ channel = botsfw .BotAPISendMessageOverHTTPS
88
+ }
86
89
//ctx := tc.Context()
87
90
88
91
var sendable tgbotapi.Sendable
@@ -188,38 +191,25 @@ func (r tgWebhookResponder) SendMessage(ctx context.Context, m botmsg.MessageFro
188
191
inlineMessageID = ""
189
192
}
190
193
if m .EditMessageUID != nil {
191
- switch m .EditMessageUID .(type ) { // TODO: How do we remove duplicates for value & pointer cases?
194
+ switch messageUID := m .EditMessageUID .(type ) { // TODO: How do we remove duplicates for value & pointer cases?
192
195
case callbackCurrent :
193
196
// do nothing
194
- case InlineMessageUID :
195
- inlineMessageID = m .EditMessageUID .(InlineMessageUID ).InlineMessageID
196
- chatID = 0
197
- messageID = 0
198
- case * InlineMessageUID :
199
- inlineMessageID = m .EditMessageUID .(* InlineMessageUID ).InlineMessageID
197
+ case InlineMessageUID , * InlineMessageUID :
198
+ inlineMessageID = messageUID .UID ()
200
199
chatID = 0
201
200
messageID = 0
202
- case ChatMessageUID :
203
- chatMessageUID := m .EditMessageUID .(ChatMessageUID )
204
- inlineMessageID = ""
205
- if chatMessageUID .ChatID != 0 {
206
- chatID = chatMessageUID .ChatID
207
- }
208
- if chatMessageUID .MessageID != 0 {
209
- messageID = chatMessageUID .MessageID
210
- }
211
- case * ChatMessageUID :
212
- chatMessageUID := m .EditMessageUID .(* ChatMessageUID )
213
- inlineMessageID = ""
214
- if chatMessageUID .ChatID != 0 {
215
- chatID = chatMessageUID .ChatID
216
- }
217
- if chatMessageUID .MessageID != 0 {
218
- messageID = chatMessageUID .MessageID
219
- }
220
201
default :
221
202
err = fmt .Errorf ("unknown EditMessageUID type %T(%v)" , m .EditMessageUID , m .EditMessageUID )
222
203
return
204
+ case ChatMessageUID , * ChatMessageUID :
205
+ inlineMessageID = ""
206
+ if uid , ok := messageUID .(interface {
207
+ GetChatID () int64
208
+ GetMessageID () int
209
+ }); ok {
210
+ chatID = uid .GetChatID ()
211
+ messageID = uid .GetMessageID ()
212
+ }
223
213
}
224
214
}
225
215
logus .Debugf (ctx , "Edit message => inlineMessageID: %v, chatID: %d, messageID: %d" , inlineMessageID , chatID , messageID )
@@ -228,33 +218,46 @@ func (r tgWebhookResponder) SendMessage(ctx context.Context, m botmsg.MessageFro
228
218
return
229
219
}
230
220
if m .Text == "" && m .Keyboard != nil {
231
- switch keyboard := m .Keyboard .(type ) {
221
+ keyboard := getTelegramKeyboard (m .Keyboard )
222
+ switch kb := keyboard .(type ) {
232
223
case * tgbotapi.InlineKeyboardMarkup :
233
- sendable = tgbotapi .NewEditMessageReplyMarkup (chatID , messageID , inlineMessageID , keyboard )
234
- case * botkb.MessageKeyboard :
235
- switch keyboard .KeyboardType () {
236
- case botkb .KeyboardTypeInline :
237
- kb := getTelegramKeyboard (m .Keyboard )
238
- inlineKbMarkup := kb .(* tgbotapi.InlineKeyboardMarkup )
239
- sendable = tgbotapi .NewEditMessageReplyMarkup (chatID , messageID , inlineMessageID , tgbotapi .NewInlineKeyboardMarkup (inlineKbMarkup .InlineKeyboard ... ))
240
- case botkb .KeyboardTypeBottom :
241
- kb := getTelegramKeyboard (m .Keyboard )
242
- msg := tgbotapi .NewMessage (chatID , m .Text )
243
- msg .ReplyMarkup = kb .(* tgbotapi.InlineKeyboardMarkup )
244
- sendable = msg
224
+ sendable = tgbotapi .NewEditMessageReplyMarkup (chatID , messageID , inlineMessageID , kb )
225
+ case * tgbotapi.ReplyKeyboardMarkup , * tgbotapi.ReplyKeyboardHide :
226
+ msg := tgbotapi .NewMessage (chatID , "" )
227
+ msg .ReplyMarkup = kb
228
+ sendable = msg
229
+ default :
230
+ err = fmt .Errorf ("unknown keyboard type %T(%v)" , keyboard .KeyboardType (), keyboard )
231
+ return
232
+ }
233
+ } else if m .Text != "" {
234
+ kb := getTelegramKeyboard (m .Keyboard )
235
+
236
+ createEditMessage := func () * tgbotapi.EditMessageTextConfig {
237
+ editMessageTextConfig := tgbotapi .NewEditMessageText (chatID , messageID , inlineMessageID , m .Text )
238
+ editMessageTextConfig .ParseMode = parseMode ()
239
+ editMessageTextConfig .DisableWebPagePreview = m .DisableWebPagePreview
240
+ sendable = editMessageTextConfig
241
+ return editMessageTextConfig
242
+ }
243
+
244
+ if kb == nil {
245
+ createEditMessage ()
246
+ } else {
247
+ switch kb := kb .(type ) {
248
+ case * tgbotapi.InlineKeyboardMarkup :
249
+ editMessageTextConfig := createEditMessage ()
250
+ editMessageTextConfig .ReplyMarkup = kb
251
+ sendable = editMessageTextConfig
252
+ case * tgbotapi.ReplyKeyboardMarkup , * tgbotapi.ReplyKeyboardHide :
253
+ messageConfig := tgbotapi .NewMessage (chatID , m .Text )
254
+ messageConfig .ReplyMarkup = kb
255
+ sendable = messageConfig
245
256
default :
246
- err = fmt .Errorf ("unknown keyboard type %T(%v)" , keyboard .KeyboardType (), keyboard )
257
+ err = fmt .Errorf ("unknown keyboard type %T(%v)" , kb .KeyboardType (), kb )
247
258
return
248
259
}
249
260
}
250
- } else if m .Text != "" {
251
- editMessageTextConfig := tgbotapi .NewEditMessageText (chatID , messageID , inlineMessageID , m .Text )
252
- editMessageTextConfig .ParseMode = parseMode ()
253
- editMessageTextConfig .DisableWebPagePreview = m .DisableWebPagePreview
254
- if m .Keyboard != nil {
255
- editMessageTextConfig .ReplyMarkup = getTelegramKeyboard (m .Keyboard ).(* tgbotapi.InlineKeyboardMarkup )
256
- }
257
- sendable = editMessageTextConfig
258
261
} else {
259
262
err = fmt .Errorf ("can't edit telegram message as got unknown output: %v" , m )
260
263
panic (err )
@@ -357,36 +360,69 @@ func getTelegramKeyboard(keyboard botkb.Keyboard) tgbotapi.Keyboard {
357
360
}
358
361
switch kb := keyboard .(type ) {
359
362
case * botkb.MessageKeyboard :
360
- tgButtons := make ([][]tgbotapi.InlineKeyboardButton , len (kb .Buttons ))
361
- for i , buttons := range kb .Buttons {
362
- tgButtons [i ] = make ([]tgbotapi.InlineKeyboardButton , len (buttons ))
363
- for j , button := range buttons {
364
- switch btn := button .(type ) {
365
- case botkb.DataButton :
366
- tgButtons [i ][j ] = tgbotapi .NewInlineKeyboardButtonData (btn .Text , btn .Data )
367
- case * botkb.DataButton :
368
- tgButtons [i ][j ] = tgbotapi .NewInlineKeyboardButtonData (btn .Text , btn .Data )
369
- case botkb.UrlButton :
370
- tgButtons [i ][j ] = tgbotapi .NewInlineKeyboardButtonURL (btn .Text , btn .URL )
371
- case * botkb.UrlButton :
372
- tgButtons [i ][j ] = tgbotapi .NewInlineKeyboardButtonURL (btn .Text , btn .URL )
373
- case botkb.SwitchInlineQueryButton :
374
- tgButtons [i ][j ] = tgbotapi .NewInlineKeyboardButtonSwitchInlineQuery (btn .Text , btn .Query )
375
- case * botkb.SwitchInlineQueryButton :
376
- tgButtons [i ][j ] = tgbotapi .NewInlineKeyboardButtonSwitchInlineQuery (btn .Text , btn .Query )
377
- case botkb.SwitchInlineQueryCurrentChatButton :
378
- tgButtons [i ][j ] = tgbotapi .NewInlineKeyboardButtonSwitchInlineQueryCurrentChat (btn .Text , btn .Query )
379
- case * botkb.SwitchInlineQueryCurrentChatButton :
380
- tgButtons [i ][j ] = tgbotapi .NewInlineKeyboardButtonSwitchInlineQueryCurrentChat (btn .Text , btn .Query )
381
- default :
382
- panic (fmt .Sprintf ("Unknown button type at [%d][%d]: %T" , i , j , btn ))
383
- }
384
- }
363
+ switch keyboard .KeyboardType () {
364
+ case botkb .KeyboardTypeInline :
365
+ return getInlineKeyboard (kb )
366
+ case botkb .KeyboardTypeBottom :
367
+ return getReplyKeyboard (kb )
368
+ case botkb .KeyboardTypeHide :
369
+ return getHideKeyboard (kb )
370
+ default :
371
+ panic (fmt .Sprintf ("keyboard.KeyboardType() returns unsupported type %v" , kb .KeyboardType ()))
385
372
}
386
- return tgbotapi .NewInlineKeyboardMarkup (tgButtons ... )
387
373
default :
388
- panic (fmt .Sprintf ("m.Keyboard has unsupported type %v" , kb .KeyboardType ()))
374
+ panic (fmt .Sprintf ("keyboard is of unsupported type %v" , keyboard ))
375
+ }
376
+ }
377
+
378
+ func getHideKeyboard (_ * botkb.MessageKeyboard ) * tgbotapi.ReplyKeyboardHide {
379
+ return & tgbotapi.ReplyKeyboardHide {HideKeyboard : true }
380
+ }
381
+
382
+ func getReplyKeyboard (kb * botkb.MessageKeyboard ) * tgbotapi.ReplyKeyboardMarkup {
383
+ tgButtons := make ([][]tgbotapi.KeyboardButton , len (kb .Buttons ))
384
+ for i , buttons := range kb .Buttons {
385
+ tgButtons [i ] = make ([]tgbotapi.KeyboardButton , len (buttons ))
386
+ for j , button := range buttons {
387
+ switch btn := button .(type ) {
388
+ case botkb.TextButton :
389
+ tgButtons [i ][j ] = tgbotapi.KeyboardButton {Text : btn .Text }
390
+ default :
391
+ tgButtons [i ][j ] = tgbotapi.KeyboardButton {Text : btn .GetText ()}
392
+ }
393
+ }
394
+ }
395
+ return tgbotapi .NewReplyKeyboard (tgButtons ... )
396
+ }
397
+
398
+ func getInlineKeyboard (kb * botkb.MessageKeyboard ) * tgbotapi.InlineKeyboardMarkup {
399
+ tgButtons := make ([][]tgbotapi.InlineKeyboardButton , len (kb .Buttons ))
400
+ for i , buttons := range kb .Buttons {
401
+ tgButtons [i ] = make ([]tgbotapi.InlineKeyboardButton , len (buttons ))
402
+ for j , button := range buttons {
403
+ switch btn := button .(type ) {
404
+ case botkb.DataButton :
405
+ tgButtons [i ][j ] = tgbotapi .NewInlineKeyboardButtonData (btn .Text , btn .Data )
406
+ case * botkb.DataButton :
407
+ tgButtons [i ][j ] = tgbotapi .NewInlineKeyboardButtonData (btn .Text , btn .Data )
408
+ case botkb.UrlButton :
409
+ tgButtons [i ][j ] = tgbotapi .NewInlineKeyboardButtonURL (btn .Text , btn .URL )
410
+ case * botkb.UrlButton :
411
+ tgButtons [i ][j ] = tgbotapi .NewInlineKeyboardButtonURL (btn .Text , btn .URL )
412
+ case botkb.SwitchInlineQueryButton :
413
+ tgButtons [i ][j ] = tgbotapi .NewInlineKeyboardButtonSwitchInlineQuery (btn .Text , btn .Query )
414
+ case * botkb.SwitchInlineQueryButton :
415
+ tgButtons [i ][j ] = tgbotapi .NewInlineKeyboardButtonSwitchInlineQuery (btn .Text , btn .Query )
416
+ case botkb.SwitchInlineQueryCurrentChatButton :
417
+ tgButtons [i ][j ] = tgbotapi .NewInlineKeyboardButtonSwitchInlineQueryCurrentChat (btn .Text , btn .Query )
418
+ case * botkb.SwitchInlineQueryCurrentChatButton :
419
+ tgButtons [i ][j ] = tgbotapi .NewInlineKeyboardButtonSwitchInlineQueryCurrentChat (btn .Text , btn .Query )
420
+ default :
421
+ panic (fmt .Sprintf ("Unknown button type at [%d][%d]: %T" , i , j , btn ))
422
+ }
423
+ }
389
424
}
425
+ return tgbotapi .NewInlineKeyboardMarkup (tgButtons ... )
390
426
}
391
427
392
428
func GetTelegramBotAPIClient (ctx context.Context , botContext botsfw.BotContext ) * tgbotapi.BotAPI {
0 commit comments