1
0

telegram.lisp 11 KB

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