1
0

telegram.lisp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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-answer-inline-query
  26. :telegram-file-contents
  27. :telegram-inline-keyboard-markup
  28. :telegram-reply-keyboard-markup
  29. :telegram-reply-keyboard-hide
  30. :telegram-force-reply
  31. :send-response
  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-answer-inline-query (query-id results &key cache-time is-personal next-offset switch-pm-text switch-pm-parameter)
  187. (%telegram-api-call
  188. "answerInlineQuery"
  189. (list (cons "inline_query_id" query-id)
  190. (cons "results" (plist-json results))
  191. (cons "cache_time" cache-time)
  192. (cons "is_personal" is-personal)
  193. (cons "next_offset" next-offset)
  194. (cons "switch_pm_text" switch-pm-text)
  195. (cons "switch_pm_parameter" switch-pm-parameter))))
  196. (defun telegram-file-contents (file-id)
  197. (let* ((file (telegram-get-file file-id))
  198. (file-path (aget "file_path" file))
  199. (file-url (format nil +telegram-file-format+ *telegram-token* file-path)))
  200. (http-request file-url :force-binary t)))
  201. (defun telegram-inline-keyboard-markup (inline-keyboard)
  202. (when inline-keyboard
  203. (plist-json
  204. (list :inline-keyboard inline-keyboard))))
  205. (defun telegram-reply-keyboard-markup (keyboard &key resize-keyboard one-time-keyboard selective)
  206. (when keyboard
  207. (plist-json
  208. (list :keyboard keyboard
  209. :resize-keyboard resize-keyboard
  210. :one-time-keyboard one-time-keyboard
  211. :selective selective))))
  212. (defun telegram-reply-keyboard-hide (&optional selective)
  213. (plist-json (list :hide-keyboard t :selective selective)))
  214. (defun telegram-force-reply (&optional selective)
  215. (plist-json (list :force-reply t :selective selective)))
  216. ;; Simplified interface
  217. ;;
  218. (defun send-response (chat-id response &optional reply-id)
  219. (if (consp response)
  220. (if (keywordp (car response))
  221. (case (car response)
  222. (:text (telegram-send-message chat-id (cdr response) :reply-to reply-id))
  223. (:voice (telegram-send-voice chat-id (cdr response) :reply-to reply-id))
  224. (:sticker (telegram-send-sticker chat-id (cdr response) :reply-to reply-id)))
  225. (mapc #'(lambda (r) (send-response chat-id r reply-id)) response))
  226. (telegram-send-message chat-id response :reply-to reply-id)))
  227. (defun bot-send-message (chat-id text &key parse-mode disable-web-preview disable-notification reply-to reply-markup)
  228. (handler-case (telegram-send-message chat-id text :parse-mode parse-mode
  229. :disable-web-preview disable-web-preview
  230. :disable-notification disable-notification
  231. :reply-to reply-to
  232. :reply-markup reply-markup)
  233. (error (e)
  234. (log:error e))))