-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
telega-inline.el
590 lines (521 loc) · 23.6 KB
/
telega-inline.el
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
;;; telega-inline.el --- Support for inline stuff -*- lexical-binding:t -*-
;; Copyright (C) 2019 by Zajcev Evgeny.
;; Author: Zajcev Evgeny <[email protected]>
;; Created: Thu Feb 14 04:51:54 2019
;; Keywords:
;; telega is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; telega is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with telega. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Inline bots support
;; @gif @youtube @pic @vid etc
;;; Code:
(require 'telega-core)
(require 'telega-tdlib)
(declare-function telega-browse-url "telega-webpage" (url &optional in-web-browser))
(declare-function telega-chatbuf-input-insert "telega-chat" (imc))
(declare-function telega-chatbuf-attach-inline-bot-query "telega-chat" (&optional no-empty-search))
(declare-function telega-chat--pop-to-buffer "telega-chat" (chat))
(declare-function telega-chat-private-p "telega-chat" (chat))
(defvar telega--inline-bot nil
"BOT value for the inline results help buffer.")
(defvar telega--inline-query nil
"Query string in help buffer.")
(defvar telega--inline-results nil
"Value for `inlineQueryResults' in help buffer.")
(defun telega--on-callbackQueryAnswer (reply)
"Handle callback reply answer."
(let ((text (telega-tl-str reply :text))
(link (telega-tl-str reply :url)))
(if (plist-get reply :show_alert)
;; Popup message from the bot
(with-telega-help-win "*Callback Alert*"
(telega-ins text)
(when link
(telega-ins "\n")
(telega-ins--raw-button (telega-link-props 'url link 'face 'link)
(telega-ins link))))
(message text)
(when link
(telega-browse-url link)))))
(cl-defun telega--getCallbackQueryAnswer (msg payload &key (callback #'ignore))
"Async send callback to bot."
(declare (indent 2))
(telega-server--call
(list :@type "getCallbackQueryAnswer"
:chat_id (plist-get msg :chat_id)
:message_id (plist-get msg :id)
:payload payload)
callback))
(defun telega-inline--callback (kbd-button msg)
"Action to take when KBD-BUTTON is pressed."
(let ((kbd-type (plist-get kbd-button :type)))
(cl-ecase (telega--tl-type kbd-type)
(keyboardButtonTypeWebApp
(telega-browse-url
(telega--getWebAppUrl (telega-msg-sender msg)
:url (telega-tl-str kbd-type :url))
'in-browser))
(keyboardButtonTypeText
;; A simple button, with text that should be sent
;; when the button is pressed
;; NOTE: (see https://t.me/emacs_telega/14354)
;; - Send as reply to the message for group chats
;; - Send as ordinary message for private chats
(let ((chat (telega-msg-chat msg))
(imc (list :@type "inputMessageText"
:text (telega-string-fmt-text
(telega-tl-str kbd-button :text))))
(reply-msg (unless (telega-chat-private-p (telega-msg-chat msg))
msg)))
(telega--sendMessage chat imc reply-msg)))
(keyboardButtonTypeRequestPhoneNumber
(telega-chat-share-my-contact (telega-msg-chat msg)))
(inlineKeyboardButtonTypeUrl
(telega-browse-url (telega-tl-str kbd-type :url)))
(inlineKeyboardButtonTypeLoginUrl
(let* ((url-info (telega--getLoginUrlInfo msg kbd-type))
(url (or (plist-get url-info :url)
(plist-get kbd-type :url))))
(when (or (plist-get url-info :skip_confirmation)
(not (memq telega-inline-login-url-action
'(query-all query-open)))
(y-or-n-p
(telega-i18n "lng_url_auth_open_confirm" :link url)))
;; Check we need to log into domain ?
;; See https://github.com/tdlib/td/issues/1189#issuecomment-691445789
(when (and (eq (telega--tl-type url-info)
'loginUrlInfoRequestConfirmation)
(memq telega-inline-login-url-action
'(query-all query-login-and-write-access
query-login-only))
(y-or-n-p
(concat (telega-i18n "lng_url_auth_login_option"
:domain (propertize
(telega-tl-str url-info :domain)
'face 'bold)
:user (telega-msg-sender-title (telega-user-me)
:with-username-p t))
"? ")))
(let ((write-p (when (and (memq telega-inline-login-url-action
'(query-all
query-login-and-write-access))
(plist-get url-info :request_write_access))
(y-or-n-p
(concat (telega-i18n "lng_url_auth_allow_messages"
:bot (propertize
(telega-msg-sender-title
(telega-user-get
(plist-get
url-info :bot_user_id))
:with-username-p t)
'face 'bold))
"? ")))))
(setq url (plist-get (telega--getLoginUrl msg kbd-type write-p)
:url))))
(telega-browse-url url))))
(inlineKeyboardButtonTypeCallback
(telega--getCallbackQueryAnswer
msg (list :@type "callbackQueryPayloadData"
:data (plist-get kbd-type :data))))
(inlineKeyboardButtonTypeSwitchInline
;; Generate another inline query to the bot
(let* ((via-bot-user-id (plist-get msg :via_bot_user_id))
(bot (if (not (zerop via-bot-user-id))
(telega-user-get via-bot-user-id)
(telega-msg-sender msg)))
(new-query (telega-tl-str kbd-type :query)))
(when (and bot (telega-user-bot-p bot))
(unless (plist-get kbd-type :in_current_chat)
(telega-chat--pop-to-buffer
(telega-completing-read-chat "To chat: ")))
(telega-chatbuf--input-delete)
(telega-chatbuf-input-insert
(concat (telega-msg-sender-username bot 'with-@)
" " (or new-query "")))
(telega-chatbuf-attach-inline-bot-query 'no-search))))
(inlineKeyboardButtonTypeCallbackGame
(telega--getCallbackQueryAnswer
msg (list :@type "callbackQueryPayloadGame"
:game_short_name
(telega--tl-get msg :content :game :short_name))))
(inlineKeyboardButtonTypeBuy
(let ((payment-form (telega--getPaymentForm msg)))
(message "payment-form: %S" payment-form)))
(inlineKeyboardButtonTypeUser
(when-let ((user (telega-user-get (plist-get kbd-type :user_id))))
(telega-chat-with user)))
(inlineKeyboardButtonTypeWebApp
(let ((web-app-info (telega--openWebApp
(telega-msg-chat msg)
(telega-msg-sender msg)
(plist-get kbd-type :url))))
(message "Webapp info: %S" web-app-info)
(telega-browse-url (plist-get web-app-info :url) 'in-browser)
))
(keyboardButtonTypeRequestUsers
(let* ((user-temex
`(and ,(or telega-user-completing-temex
'(return t))
,(if (plist-get kbd-type :restrict_user_is_bot)
(if (plist-get kbd-type :user_is_bot)
'is-bot
'(not is-bot))
'(return t))
,(if (plist-get kbd-type :restrict_user_is_premium)
(if (plist-get kbd-type :user_is_premium)
'is-premium
'(not is-premium))
'(return t))))
(users (telega-completing-read-user-list
(telega-tl-str kbd-button :text)
(telega-user-list user-temex))))
(when users
(telega--shareUsersWithBot msg (plist-get kbd-type :id) users))))
(keyboardButtonTypeRequestChat
(let* ((chat-temex
`(and (or is-known has-chatbuf)
(not (type private))
,(if (plist-get kbd-type :chat_is_channel)
'(type channel)
'(not (type channel)))
,(if (plist-get kbd-type :restrict_chat_is_forum)
(if (plist-get kbd-type :chat_is_forum)
'is-forum
'(not is-forum))
'(return t))
,(if (plist-get kbd-type :restrict_chat_has_username)
(if (plist-get kbd-type :chat_is_forum)
'has-username
'(not has-username))
'(return t))
,(if (plist-get kbd-type :chat_is_created)
'me-is-owner
'(return t))
;; TODO:
;; :user_administrator_rights
;; :bot_administrator_rights
;; :bot_is_member
))
(chat
(telega-completing-read-chat
(concat (telega-tl-str kbd-button :text) ": ")
(telega-filter-chats telega--ordered-chats
chat-temex))))
(when chat
(telega--shareChatWithBot msg (plist-get kbd-type :id) chat))))
;; TODO: other types
)))
(defun telega-inline--help-echo (kbd-button _msg)
"Generate help-echo value for KBD-BUTTON."
(let ((kbd-type (plist-get kbd-button :type)))
(cl-case (telega--tl-type kbd-type)
((inlineKeyboardButtonTypeUrl inlineKeyboardButtonTypeLoginUrl)
(telega-tl-str kbd-type :url))
)))
(defun telega--getInlineQueryResults (bot-user query &optional chat
offset location callback)
"Query BOT-ID for the QUERY."
(declare (indent 5))
(telega-server--call
(nconc (list :@type "getInlineQueryResults"
:bot_user_id (plist-get bot-user :id)
:query query)
(when chat
(list :chat_id (plist-get chat :id)))
(when location
(list :location location))
(when offset
(list :offset offset)))
callback))
(defun telega-ins--inline-delim ()
"Inserter for the delimiter."
(telega-ins--with-props
'(face underline display ((space-width 2) (height 0.5)))
(telega-ins (make-string 30 ?\s) "\n")))
(defun telega-inline-bot--action (qr)
"Action to take when corresponding query result QR button is pressed."
(cl-assert telega--chat)
(cl-assert telega--inline-bot)
(cl-assert telega--inline-results)
(cl-assert (eq major-mode 'help-mode))
(let ((chat telega--chat)
(inline-query telega--inline-query)
(inline-results telega--inline-results)
(bot telega--inline-bot))
;; NOTE: Kill help win before modifying chatbuffer, because it
;; recovers window configuration on kill
(quit-window 'kill-buffer)
(let* ((thumb (cl-case (telega--tl-type qr)
(inlineQueryResultAnimation
(telega--tl-get qr :animation :thumbnail))
(inlineQueryResultArticle
(plist-get qr :thumbnail))
(inlineQueryResultPhoto
(telega-photo--thumb (plist-get qr :photo)))
(inlineQueryResultGame
(telega-photo--thumb (telega--tl-get qr :game :photo)))
(inlineQueryResultVideo
(telega--tl-get qr :video :thumbnail))))
(thumb-file (when thumb (telega-file--renew thumb :file)))
(thumb-img (when (telega-file--downloaded-p thumb-file)
(telega-create-image
(telega--tl-get thumb-file :local :path) nil nil
:scale 1.0
:ascent 'center
:height (telega-ch-height 1)))))
(with-telega-chatbuf chat
(telega-chatbuf--input-delete)
(telega-chatbuf-input-insert
(list :@type "telegaInlineQuery"
:preview thumb-img
:caption (substring (plist-get qr :@type) 17)
:query inline-query
:via-bot bot
:hide-via-bot current-prefix-arg
:query-id (plist-get inline-results :inline_query_id)
:result-id (telega-tl-str qr :id)))))))
(defun telega-ins--inline-audio (qr)
"Inserter for `inlineQueryResultAudio' QR."
(let ((audio (plist-get qr :audio)))
(telega-ins--audio nil audio 'full (telega-symbol 'audio))
(telega-ins "\n")))
(defun telega-ins--inline-voice-note (qr)
"Inserter for `inlineQueryResultVoiceNote' QR."
(let ((voice-note (plist-get qr :voice_note)))
(telega-ins (telega-tl-str qr :title) "\n")
(telega-ins--voice-note nil voice-note)
(telega-ins "\n")))
(defun telega-ins--inline-sticker (qr)
"Inserter for `inlineQueryResultSticker' QR."
(let ((sticker (plist-get qr :sticker)))
(telega-ins--sticker-image sticker)))
(defun telega-ins--inline-animation (qr)
"Inserter for `inlineQueryResultAnimation' QR."
(let ((anim (plist-get qr :animation)))
(telega-ins--animation-image anim)))
(defun telega-ins--inline-photo (qr)
"Inserter for `inlineQueryResultPhoto' QR."
(let ((photo (plist-get qr :photo)))
(telega-ins--image
(telega-photo--image photo telega-inline-photo-size-limits))))
(defun telega-ins--inline-document (qr)
"Inserter for `inlineQueryResultDocument' QR."
(let* ((doc (plist-get qr :document))
(thumb (plist-get doc :thumbnail))
(thumb-img (when thumb
(telega-media--image
(cons thumb 'telega-thumb--create-image-two-lines)
(cons thumb :photo)))))
(telega-ins--document-header doc)
(telega-ins "\n")
;; documents thumbnail preview (if any)
(when thumb-img
(telega-ins--image thumb-img 0))
(telega-ins " " (telega-tl-str qr :title) "\n")
(when thumb-img
(telega-ins--image thumb-img 1))
(telega-ins " " (telega-tl-str qr :description) "\n")))
(defun telega-ins--inline-article (qr)
"Inserter for `inlineQueryResultArticle' QR."
(let* ((thumb (plist-get qr :thumbnail))
(thumb-img (when thumb
(telega-media--image
(cons thumb 'telega-thumb--create-image-two-lines)
(cons thumb :file)))))
(when thumb-img
(telega-ins--image thumb-img 0))
(telega-ins " " (telega-tl-str qr :title) "\n")
(when thumb-img
(telega-ins--image thumb-img 1))
(telega-ins " " (telega-tl-str qr :description) "\n")
))
(defun telega-ins--inline-video (qr)
"Inserter for `inlineQueryResultVideo' QR."
(let* ((video (plist-get qr :video))
(thumb (plist-get video :thumbnail))
(thumb-img (when thumb
(telega-media--image
(cons thumb 'telega-thumb--create-image-two-lines)
(cons thumb :file)))))
(when thumb-img
(telega-ins--image thumb-img 0)
(telega-ins " "))
(telega-ins (telega-tl-str qr :title))
(telega-ins "\n")
(when thumb-img
(telega-ins--image thumb-img 1)
(telega-ins " "))
(telega-ins-fmt "%dx%d %s"
(plist-get video :width)
(plist-get video :height)
(telega-duration-human-readable (plist-get video :duration)))
(telega-ins "\n")))
(defun telega-ins--inline-game (qr)
"Inserter for `inlineQueryResultGame' QR."
(let* ((game (plist-get qr :game))
(photo (plist-get game :photo))
(photo-img (when photo
(telega-photo--image photo (list 4 2 4 2)))))
(when photo-img
(telega-ins--image photo-img 0)
(telega-ins " "))
(telega-ins--with-face 'bold
(telega-ins (telega-tl-str game :title)))
(telega-ins "\n")
(when photo-img
(telega-ins--image photo-img 1)
(telega-ins " "))
(telega-ins (telega-tl-str game :description))
(telega-ins "\n")))
(defun telega-ins--inline-venue (qr)
"Inserter for `inlineQueryResultVenue' OR."
(let* ((venue (plist-get qr :venue))
(thumb (plist-get qr :thumbnail))
(thumb-img (when thumb
(telega-media--image
(cons thumb 'telega-thumb--create-image-three-lines)
(cons thumb :photo)))))
(when thumb-img
(telega-ins--image thumb-img 0)
(telega-ins " "))
(telega-ins--with-face 'bold
(telega-ins (telega-tl-str venue :title)))
(telega-ins "\n")
(when thumb-img
(telega-ins--image thumb-img 1)
(telega-ins " "))
(telega-ins--with-face 'telega-shadow
(telega-ins (telega-tl-str venue :address)))
(telega-ins "\n")
(when thumb-img
(telega-ins--image thumb-img 2)
(telega-ins " "))
(telega-ins--location (plist-get venue :location))
(telega-ins "\n")))
(defun telega-inline-bot--gen-callback (bot query &optional for-chat)
"Generate callback for the BOT's QUERY result handling in FOR-CHAT."
(lambda (reply)
(if-let ((qr-results (append (plist-get reply :results) nil)))
(let ((help-window-select telega-inline-query-window-select))
(with-telega-help-win "*Telegram Inline Results*"
(visual-line-mode 1)
;; NOTE: Non-nil `auto-window-vscroll' make C-n jump to the end
;; of the buffer
(set (make-local-variable 'auto-window-vscroll) nil)
(setq telega--inline-bot bot)
(setq telega--inline-query query)
(setq telega--inline-results reply)
(setq telega--chat for-chat)
(dolist (qr qr-results)
;; NOTE: possible insert the delimiter, so mixing for
;; example Articles and Animations is possible
(when (memq (telega--tl-type qr)
'(inlineQueryResultVideo
inlineQueryResultAudio
inlineQueryResultArticle
inlineQueryResultDocument
inlineQueryResultGame
inlineQueryResultVenue))
(unless (or (= (point) (line-beginning-position))
(= (point) 1))
(telega-ins "\n")
(telega-ins--inline-delim)))
(cl-case (telega--tl-type qr)
(inlineQueryResultDocument
(telega-button--insert 'telega qr
:inserter 'telega-ins--inline-document
:action 'telega-inline-bot--action
:telega-add-sensor-func
#'telega-button-highlight--sensor-func)
(telega-ins--inline-delim))
(inlineQueryResultVideo
(telega-button--insert 'telega qr
:inserter 'telega-ins--inline-video
:action 'telega-inline-bot--action
:telega-add-sensor-func
#'telega-button-highlight--sensor-func)
(telega-ins--inline-delim))
(inlineQueryResultAudio
(telega-button--insert 'telega qr
:inserter 'telega-ins--inline-audio
:action 'telega-inline-bot--action
:telega-add-sensor-func
#'telega-button-highlight--sensor-func)
(telega-ins--inline-delim))
(inlineQueryResultVoiceNote
(telega-button--insert 'telega qr
:inserter 'telega-ins--inline-voice-note
:action 'telega-inline-bot--action
:telega-add-sensor-func
#'telega-button-highlight--sensor-func)
(telega-ins--inline-delim))
(inlineQueryResultArticle
(telega-button--insert 'telega qr
:inserter 'telega-ins--inline-article
:action 'telega-inline-bot--action
:telega-add-sensor-func
'telega-button-highlight--sensor-func)
(telega-ins--inline-delim))
(inlineQueryResultAnimation
(telega-button--insert 'telega qr
:inserter 'telega-ins--inline-animation
:action 'telega-inline-bot--action
:telega-add-sensor-func
(telega-animation--gen-sensor-func (plist-get qr :animation))
'help-echo (when-let ((title (telega-tl-str qr :title)))
(format "GIF title: %s" title))))
(inlineQueryResultPhoto
(telega-button--insert 'telega qr
:inserter 'telega-ins--inline-photo
:action 'telega-inline-bot--action))
(inlineQueryResultSticker
(telega-button--insert 'telega qr
:inserter 'telega-ins--inline-sticker
:action 'telega-inline-bot--action))
(inlineQueryResultGame
(telega-button--insert 'telega qr
:inserter 'telega-ins--inline-game
:action 'telega-inline-bot--action)
(telega-ins--inline-delim))
(inlineQueryResultVenue
(telega-button--insert 'telega qr
:inserter 'telega-ins--inline-venue
:action 'telega-inline-bot--action)
(telega-ins--inline-delim))
(t
(telega-ins-fmt "* %S\n" qr))))))
;; Not found
(unless (string-empty-p query)
(message "telega: @%s Nothing found for %s"
(telega-msg-sender-username bot)
(propertize query 'face 'bold)))
)))
(defun telega-inline-bot-query (bot query for-chat)
"Query BOT for inline results for the QUERY."
(with-telega-chatbuf for-chat
;; Cancel currently active inline-query loading
(when (telega-server--callback-get telega-chatbuf--inline-query)
(telega-server--callback-put telega-chatbuf--inline-query 'ignore))
(message "telega: @%s Searching for %s..."
(telega-msg-sender-username bot) (propertize query 'face 'bold))
(setq telega-chatbuf--inline-query
(telega--getInlineQueryResults bot query for-chat nil nil
(telega-inline-bot--gen-callback bot query for-chat)))))
(defun telega--recent-inline-bots-fetch ()
"Update recently used bots."
(telega--getRecentInlineBots
(lambda (users)
(setq telega--recent-inline-bots
(mapcar (lambda (user)
(telega-user-title user 'username))
users)))))
(provide 'telega-inline)
;;; telega-inline.el ends here