1
0

telegram.lisp 10 KB

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