1
0

telegram.lisp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. (in-package #:chatikbot)
  2. (defvar *telegram-token* nil "Telegram bot token")
  3. (defparameter +telegram-api-format+ "https://api.telegram.org/bot~A/~A")
  4. (defparameter +telegram-file-format+ "https://api.telegram.org/file/bot~A/~A")
  5. (defparameter +telegram-max-callback-data-length+ 64)
  6. (defvar *telegram-timeout* 30 "Default Telegram timeout")
  7. (defun %telegram-api-call (method &optional args)
  8. (let* ((params (loop for (k . v) in args when v
  9. collect (cons
  10. (princ-to-string k)
  11. (if (pathnamep v) v
  12. (princ-to-string v)))))
  13. (timeout (+ 5 (or (aget "timeout" args)
  14. *telegram-timeout*)))
  15. (response
  16. (json-request (format nil +telegram-api-format+
  17. *telegram-token* method)
  18. :method :post
  19. :parameters params
  20. :timeout timeout)))
  21. (unless (aget "ok" response)
  22. (error (aget "description" response)))
  23. (aget "result" response)))
  24. (defun telegram-get-updates (&key offset limit timeout)
  25. (%telegram-api-call
  26. "getUpdates"
  27. (list (cons "offset" offset)
  28. (cons "limit" limit)
  29. (cons "timeout" timeout))))
  30. (defun telegram-get-me ()
  31. (%telegram-api-call "getMe"))
  32. (defun telegram-set-webhook (&optional url certificate)
  33. (%telegram-api-call "setWebhook" (list (cons "url" url) (cons "certificate" certificate))))
  34. (defun telegram-send-message (chat-id text &key parse-mode disable-web-preview disable-notification reply-to reply-markup)
  35. (%telegram-api-call
  36. "sendMessage"
  37. (list (cons "chat_id" chat-id)
  38. (cons "text" text)
  39. (cons "parse_mode" parse-mode)
  40. (cons "disable_web_page_preview" disable-web-preview)
  41. (cons "disable_notification" disable-notification)
  42. (cons "reply_to_message_id" reply-to)
  43. (cons "reply_markup" reply-markup))))
  44. (defun telegram-forward-message (chat-id from-chat-id message-id)
  45. (%telegram-api-call
  46. "forwardMessage"
  47. `(("chat_id" . ,chat-id)
  48. ("from_chat_id" . ,from-chat-id)
  49. ("message_id" . ,message-id))))
  50. (defun telegram-send-photo (chat-id photo &key caption reply-to reply-markup)
  51. (%telegram-api-call
  52. "sendPhoto"
  53. (list (cons "chat_id" chat-id)
  54. (cons "photo" photo)
  55. (cons "caption" caption)
  56. (cons "reply_to_message_id" reply-to)
  57. (cons "reply_markup" reply-markup))))
  58. (defun telegram-send-audio (chat-id audio &key duration performer title reply-to reply-markup)
  59. (%telegram-api-call
  60. "sendAudio"
  61. (list (cons "chat_id" chat-id)
  62. (cons "audio" audio)
  63. (cons "duration" duration)
  64. (cons "performer" performer)
  65. (cons "title" title)
  66. (cons "reply_to_message_id" reply-to)
  67. (cons "reply_markup" reply-markup))))
  68. (defun telegram-send-document (chat-id document &key reply-to reply-markup)
  69. (%telegram-api-call
  70. "sendDocument"
  71. (list (cons "chat_id" chat-id)
  72. (cons "document" document)
  73. (cons "reply_to_message_id" reply-to)
  74. (cons "reply_markup" reply-markup))))
  75. (defun telegram-send-sticker (chat-id sticker &key reply-to reply-markup)
  76. (%telegram-api-call
  77. "sendSticker"
  78. (list (cons "chat_id" chat-id)
  79. (cons "sticker" sticker)
  80. (cons "reply_to_message_id" reply-to)
  81. (cons "reply_markup" reply-markup))))
  82. (defun telegram-send-video (chat-id video &key duration caption reply-to reply-markup)
  83. (%telegram-api-call
  84. "sendVideo"
  85. (list (cons "chat_id" chat-id)
  86. (cons "video" video)
  87. (cons "duration" duration)
  88. (cons "caption" caption)
  89. (cons "reply_to_message_id" reply-to)
  90. (cons "reply_markup" reply-markup))))
  91. (defun telegram-send-voice (chat-id voice &key duration reply-to reply-markup)
  92. (%telegram-api-call
  93. "sendVoice"
  94. (list (cons "chat_id" chat-id)
  95. (cons "voice" voice)
  96. (cons "duration" duration)
  97. (cons "reply_to_message_id" reply-to)
  98. (cons "reply_markup" reply-markup))))
  99. (defun telegram-send-location (chat-id latitude longitude &key reply-to reply-markup)
  100. (%telegram-api-call
  101. "sendLocation"
  102. (list (cons "chat_id" chat-id)
  103. (cons "latitude" latitude)
  104. (cons "longitude" longitude)
  105. (cons "reply_to_message_id" reply-to)
  106. (cons "reply_markup" reply-markup))))
  107. (defun telegram-send-chat-action (chat-id action)
  108. (%telegram-api-call
  109. "sendChatAction"
  110. (list (cons "chat_id" chat-id)
  111. (cons "action" action))))
  112. (defun telegram-get-user-profile-photos (user-id &key offset limit)
  113. (%telegram-api-call
  114. "getUserProfilePhotos"
  115. (list (cons "user_id" user-id)
  116. (cons "offset" offset)
  117. (cons "limit" limit))))
  118. (defun telegram-get-file (file-id)
  119. (%telegram-api-call "getFile" (list (cons "file_id" file-id))))
  120. (defun telegram-answer-callback-query (query-id &key text show-alert)
  121. (%telegram-api-call
  122. "answerCallbackQuery"
  123. (list (cons "callback_query_id" query-id)
  124. (cons "text" text)
  125. (cons "show_alert" show-alert))))
  126. (defun telegram-edit-message-text (text &key chat-id message-id inline-message-id parse-mode disable-web-preview reply-markup)
  127. (%telegram-api-call
  128. "editMessageText"
  129. (list (cons "chat_id" chat-id)
  130. (cons "message_id" message-id)
  131. (cons "inline_message_id" inline-message-id)
  132. (cons "text" text)
  133. (cons "parse_mode" parse-mode)
  134. (cons "disable_web_page_preview" disable-web-preview)
  135. (cons "reply_markup" reply-markup))))
  136. (defun telegram-edit-message-caption (caption &key chat-id message-id inline-message-id reply-markup)
  137. (%telegram-api-call
  138. "editMessageCaption"
  139. (list (cons "chat_id" chat-id)
  140. (cons "message_id" message-id)
  141. (cons "inline_message_id" inline-message-id)
  142. (cons "caption" caption)
  143. (cons "reply_markup" reply-markup))))
  144. (defun telegram-edit-message-reply-markup (reply-markup &key chat-id message-id inline-message-id)
  145. (%telegram-api-call
  146. "editMessageReplyMarkup"
  147. (list (cons "chat_id" chat-id)
  148. (cons "message_id" message-id)
  149. (cons "inline_message_id" inline-message-id)
  150. (cons "reply_markup" reply-markup))))
  151. (defun telegram-answer-inline-query (query-id results &key cache-time is-personal next-offset switch-pm-text switch-pm-parameter)
  152. (%telegram-api-call
  153. "answerInlineQuery"
  154. (list (cons "inline_query_id" query-id)
  155. (cons "results" (plist-json results))
  156. (cons "cache_time" cache-time)
  157. (cons "is_personal" is-personal)
  158. (cons "next_offset" next-offset)
  159. (cons "switch_pm_text" switch-pm-text)
  160. (cons "switch_pm_parameter" switch-pm-parameter))))
  161. (defun telegram-file-contents (file-id)
  162. (let* ((file (telegram-get-file file-id))
  163. (file-path (aget "file_path" file))
  164. (file-url (format nil +telegram-file-format+ *telegram-token* file-path)))
  165. (http-request file-url :force-binary t)))
  166. (defun telegram-inline-keyboard-markup (inline-keyboard)
  167. (when inline-keyboard
  168. (plist-json
  169. (list :inline-keyboard inline-keyboard))))
  170. (defun telegram-reply-keyboard-markup (keyboard &key resize-keyboard one-time-keyboard selective)
  171. (when keyboard
  172. (plist-json
  173. (list :keyboard keyboard
  174. :resize-keyboard resize-keyboard
  175. :one-time-keyboard one-time-keyboard
  176. :selective selective))))
  177. (defun telegram-reply-keyboard-hide (&optional selective)
  178. (plist-json (list :hide-keyboard t :selective selective)))
  179. (defun telegram-force-reply (&optional selective)
  180. (plist-json (list :force-reply t :selective selective)))
  181. ;; Simplified interface
  182. ;;
  183. (defun send-response (chat-id response &optional reply-id)
  184. (if (consp response)
  185. (if (keywordp (car response))
  186. (case (car response)
  187. (:text (telegram-send-message chat-id (cdr response) :reply-to reply-id))
  188. (:voice (telegram-send-voice chat-id (cdr response) :reply-to reply-id))
  189. (:sticker (telegram-send-sticker chat-id (cdr response) :reply-to reply-id)))
  190. (mapc #'(lambda (r) (send-response chat-id r reply-id)) response))
  191. (telegram-send-message chat-id response :reply-to reply-id)))
  192. (defun bot-send-message (chat-id text &key parse-mode disable-web-preview disable-notification reply-to reply-markup)
  193. (handler-case (telegram-send-message chat-id text :parse-mode parse-mode
  194. :disable-web-preview disable-web-preview
  195. :disable-notification disable-notification
  196. :reply-to reply-to
  197. :reply-markup reply-markup)
  198. (error (e)
  199. (log:error e))))