telegram.lisp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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-video-note
  18. :telegram-send-media-group
  19. :telegram-send-location
  20. :telegram-edit-message-live-location
  21. :telegram-stop-message-live-location
  22. :telegram-send-contact
  23. :telegram-send-chat-action
  24. :telegram-send-get-user-profile-photos
  25. :telegram-send-get-file
  26. :telegram-answer-callback-query
  27. :telegram-edit-message-text
  28. :telegram-edit-message-caption
  29. :telegram-edit-message-reply-markup
  30. :telegram-delete-message
  31. :telegram-answer-inline-query
  32. :telegram-file-contents
  33. :telegram-inline-keyboard-markup
  34. :telegram-reply-keyboard-markup
  35. :telegram-reply-keyboard-hide
  36. :telegram-force-reply
  37. :bot-send-message))
  38. (in-package :chatikbot.telegram)
  39. (defvar *telegram-token* nil "Telegram bot token")
  40. (defparameter +telegram-api-format+ "https://api.telegram.org/bot~A/~A")
  41. (defparameter +telegram-file-format+ "https://api.telegram.org/file/bot~A/~A")
  42. (defparameter +telegram-max-callback-data-length+ 64)
  43. (defvar *telegram-timeout* 30 "Default Telegram timeout")
  44. (defun %telegram-api-call (method &optional args)
  45. (handler-case
  46. (let* ((params (loop for (k . v) in args when v
  47. collect (cons
  48. (princ-to-string k)
  49. (if (pathnamep v) v
  50. (princ-to-string v)))))
  51. (timeout (+ 5 (or (aget "timeout" args)
  52. *telegram-timeout*)))
  53. (response
  54. (json-request (format nil +telegram-api-format+
  55. *telegram-token* method)
  56. :method :post
  57. :content params
  58. :timeout timeout)))
  59. (unless (aget "ok" response)
  60. (error (aget "description" response)))
  61. (aget "result" response))
  62. (dex:http-request-forbidden (e)
  63. (log:info "Forbidden" e))))
  64. (defun telegram-get-updates (&key offset limit timeout)
  65. (%telegram-api-call
  66. "getUpdates"
  67. (list (cons "offset" offset)
  68. (cons "limit" limit)
  69. (cons "timeout" timeout))))
  70. (defun telegram-get-me ()
  71. (%telegram-api-call "getMe"))
  72. (defun telegram-set-webhook (&optional url certificate)
  73. (%telegram-api-call "setWebhook" (list (cons "url" url) (cons "certificate" certificate))))
  74. (defun telegram-send-message (chat-id text &key parse-mode disable-web-preview disable-notification reply-to reply-markup)
  75. (%telegram-api-call
  76. "sendMessage"
  77. (list (cons "chat_id" chat-id)
  78. (cons "text" text)
  79. (cons "parse_mode" parse-mode)
  80. (cons "disable_web_page_preview" disable-web-preview)
  81. (cons "disable_notification" disable-notification)
  82. (cons "reply_to_message_id" reply-to)
  83. (cons "reply_markup" reply-markup))))
  84. (defun telegram-forward-message (chat-id from-chat-id message-id)
  85. (%telegram-api-call
  86. "forwardMessage"
  87. `(("chat_id" . ,chat-id)
  88. ("from_chat_id" . ,from-chat-id)
  89. ("message_id" . ,message-id))))
  90. (defun telegram-send-photo (chat-id photo &key caption disable-notification reply-to reply-markup)
  91. (%telegram-api-call
  92. "sendPhoto"
  93. (list (cons "chat_id" chat-id)
  94. (cons "photo" photo)
  95. (cons "caption" caption)
  96. (cons "disable_notification" disable-notification)
  97. (cons "reply_to_message_id" reply-to)
  98. (cons "reply_markup" reply-markup))))
  99. (defun telegram-send-audio (chat-id audio &key caption duration performer title disable-notification reply-to reply-markup)
  100. (%telegram-api-call
  101. "sendAudio"
  102. (list (cons "chat_id" chat-id)
  103. (cons "audio" audio)
  104. (cons "caption" caption)
  105. (cons "duration" duration)
  106. (cons "performer" performer)
  107. (cons "title" title)
  108. (cons "disable_notification" disable-notification)
  109. (cons "reply_to_message_id" reply-to)
  110. (cons "reply_markup" reply-markup))))
  111. (defun telegram-send-document (chat-id document &key caption disable-notification reply-to reply-markup)
  112. (%telegram-api-call
  113. "sendDocument"
  114. (list (cons "chat_id" chat-id)
  115. (cons "document" document)
  116. (cons "caption" caption)
  117. (cons "disable_notification" disable-notification)
  118. (cons "reply_to_message_id" reply-to)
  119. (cons "reply_markup" reply-markup))))
  120. (defun telegram-send-sticker (chat-id sticker &key disable-notification reply-to reply-markup)
  121. (%telegram-api-call
  122. "sendSticker"
  123. (list (cons "chat_id" chat-id)
  124. (cons "sticker" sticker)
  125. (cons "disable_notification" disable-notification)
  126. (cons "reply_to_message_id" reply-to)
  127. (cons "reply_markup" reply-markup))))
  128. (defun telegram-send-video (chat-id video &key duration width height caption disable-notification reply-to reply-markup)
  129. (%telegram-api-call
  130. "sendVideo"
  131. (list (cons "chat_id" chat-id)
  132. (cons "video" video)
  133. (cons "duration" duration)
  134. (cons "width" width)
  135. (cons "height" height)
  136. (cons "caption" caption)
  137. (cons "disable_notification" disable-notification)
  138. (cons "reply_to_message_id" reply-to)
  139. (cons "reply_markup" reply-markup))))
  140. (defun telegram-send-voice (chat-id voice &key caption duration disable-notification reply-to reply-markup)
  141. (%telegram-api-call
  142. "sendVoice"
  143. (list (cons "chat_id" chat-id)
  144. (cons "voice" voice)
  145. (cons "caption" caption)
  146. (cons "duration" duration)
  147. (cons "disable_notification" disable-notification)
  148. (cons "reply_to_message_id" reply-to)
  149. (cons "reply_markup" reply-markup))))
  150. (defun telegram-send-video-note (chat-id video-note &key duration length disable-notification reply-to reply-markup)
  151. (%telegram-api-call
  152. "sendVideoNote"
  153. (list (cons "chat_id" chat-id)
  154. (cons "video_note" video-note)
  155. (cons "duration" duration)
  156. (cons "length" length)
  157. (cons "disable_notification" disable-notification)
  158. (cons "reply_to_message_id" reply-to)
  159. (cons "reply_markup" reply-markup))))
  160. (defun telegram-send-media-group (chat-id media &key disable-notification reply-to)
  161. (%telegram-api-call
  162. "sendMediaGroup"
  163. (list (cons "chat_id" chat-id)
  164. (cons "media" media)
  165. (cons "disable_notification" disable-notification)
  166. (cons "reply_to_message_id" reply-to))))
  167. (defun telegram-send-location (chat-id latitude longitude &key live-period disable-notification reply-to reply-markup)
  168. (%telegram-api-call
  169. "sendLocation"
  170. (list (cons "chat_id" chat-id)
  171. (cons "latitude" latitude)
  172. (cons "longitude" longitude)
  173. (cons "live_period" live-period)
  174. (cons "disable_notification" disable-notification)
  175. (cons "reply_to_message_id" reply-to)
  176. (cons "reply_markup" reply-markup))))
  177. (defun telegram-edit-message-live-location (latitude longitude &key chat-id message-id inline-message-id reply-markup)
  178. (%telegram-api-call
  179. "editMessageLiveLocation"
  180. (list (cons "chat_id" chat-id)
  181. (cons "message_id" message-id)
  182. (cons "inline_message_id" inline-message-id)
  183. (cons "latitude" latitude)
  184. (cons "longitude" longitude)
  185. (cons "reply_markup" reply-markup))))
  186. (defun telegram-stop-message-live-location (&key chat-id message-id inline-message-id reply-markup)
  187. (%telegram-api-call
  188. "stopMessageLiveLocation"
  189. (list (cons "chat_id" chat-id)
  190. (cons "message_id" message-id)
  191. (cons "inline_message_id" inline-message-id)
  192. (cons "reply_markup" reply-markup))))
  193. (defun telegram-send-venue (chat-id latitude longitude title address &key foursquare-id disable-notification reply-to reply-markup)
  194. (%telegram-api-call
  195. "sendVenue"
  196. (list (cons "chat_id" chat-id)
  197. (cons "latitude" latitude)
  198. (cons "longitude" longitude)
  199. (cons "title" title)
  200. (cons "address" address)
  201. (cons "foursquare_id" foursquare-id)
  202. (cons "disable_notification" disable-notification)
  203. (cons "reply_to_message_id" reply-to)
  204. (cons "reply_markup" reply-markup))))
  205. (defun telegram-send-contact (chat-id phone-number first-name &key last-name disable-notification reply-to reply-markup)
  206. (%telegram-api-call
  207. "sendContact"
  208. (list (cons "chat_id" chat-id)
  209. (cons "phone_number" phone-number)
  210. (cons "first_name" first-name)
  211. (cons "last_name" last-name)
  212. (cons "disable_notification" disable-notification)
  213. (cons "reply_to_message_id" reply-to)
  214. (cons "reply_markup" reply-markup))))
  215. (defun telegram-send-chat-action (chat-id action)
  216. (%telegram-api-call
  217. "sendChatAction"
  218. (list (cons "chat_id" chat-id)
  219. (cons "action" action))))
  220. (defun telegram-get-user-profile-photos (user-id &key offset limit)
  221. (%telegram-api-call
  222. "getUserProfilePhotos"
  223. (list (cons "user_id" user-id)
  224. (cons "offset" offset)
  225. (cons "limit" limit))))
  226. (defun telegram-get-file (file-id)
  227. (%telegram-api-call "getFile" (list (cons "file_id" file-id))))
  228. (defun telegram-answer-callback-query (query-id &key text show-alert)
  229. (%telegram-api-call
  230. "answerCallbackQuery"
  231. (list (cons "callback_query_id" query-id)
  232. (cons "text" text)
  233. (cons "show_alert" show-alert))))
  234. (defun telegram-edit-message-text (text &key chat-id message-id inline-message-id parse-mode disable-web-preview reply-markup)
  235. (%telegram-api-call
  236. "editMessageText"
  237. (list (cons "chat_id" chat-id)
  238. (cons "message_id" message-id)
  239. (cons "inline_message_id" inline-message-id)
  240. (cons "text" text)
  241. (cons "parse_mode" parse-mode)
  242. (cons "disable_web_page_preview" disable-web-preview)
  243. (cons "reply_markup" reply-markup))))
  244. (defun telegram-edit-message-caption (caption &key chat-id message-id inline-message-id reply-markup)
  245. (%telegram-api-call
  246. "editMessageCaption"
  247. (list (cons "chat_id" chat-id)
  248. (cons "message_id" message-id)
  249. (cons "inline_message_id" inline-message-id)
  250. (cons "caption" caption)
  251. (cons "reply_markup" reply-markup))))
  252. (defun telegram-edit-message-reply-markup (reply-markup &key chat-id message-id inline-message-id)
  253. (%telegram-api-call
  254. "editMessageReplyMarkup"
  255. (list (cons "chat_id" chat-id)
  256. (cons "message_id" message-id)
  257. (cons "inline_message_id" inline-message-id)
  258. (cons "reply_markup" reply-markup))))
  259. (defun telegram-delete-message (chat-id message-id)
  260. (%telegram-api-call
  261. "deleteMessage" (list (cons "chat_id" chat-id)
  262. (cons "message_id" message-id))))
  263. (defun telegram-answer-inline-query (query-id results &key cache-time is-personal next-offset switch-pm-text switch-pm-parameter)
  264. (%telegram-api-call
  265. "answerInlineQuery"
  266. (list (cons "inline_query_id" query-id)
  267. (cons "results" (plist-json results))
  268. (cons "cache_time" cache-time)
  269. (cons "is_personal" is-personal)
  270. (cons "next_offset" next-offset)
  271. (cons "switch_pm_text" switch-pm-text)
  272. (cons "switch_pm_parameter" switch-pm-parameter))))
  273. (defun telegram-file-contents (file-id)
  274. (let* ((file (telegram-get-file file-id))
  275. (file-path (aget "file_path" file))
  276. (file-url (format nil +telegram-file-format+ *telegram-token* file-path)))
  277. (http-request file-url :force-binary t)))
  278. (defun telegram-inline-keyboard-markup (inline-keyboard)
  279. (when inline-keyboard
  280. (plist-json
  281. (list :inline-keyboard inline-keyboard))))
  282. (defun telegram-reply-keyboard-markup (keyboard &key resize-keyboard one-time-keyboard selective)
  283. (when keyboard
  284. (plist-json
  285. (list :keyboard keyboard
  286. :resize-keyboard resize-keyboard
  287. :one-time-keyboard one-time-keyboard
  288. :selective selective))))
  289. (defun telegram-reply-keyboard-hide (&optional selective)
  290. (plist-json (list :hide-keyboard t :selective selective)))
  291. (defun telegram-force-reply (&optional selective)
  292. (plist-json (list :force-reply t :selective selective)))
  293. ;; Simplified interface
  294. ;;
  295. (defun bot-send-message (message &key (chat-id *chat-id*) parse-mode disable-web-preview disable-notification reply-to reply-markup duration)
  296. (handler-case (if (consp message)
  297. (if (keywordp (car message))
  298. (case (car message)
  299. (:text (telegram-send-message chat-id (cdr message)
  300. :parse-mode parse-mode
  301. :disable-web-preview disable-web-preview
  302. :disable-notification disable-notification
  303. :reply-to reply-to
  304. :reply-markup reply-markup))
  305. (:voice (telegram-send-voice chat-id (cdr message)
  306. :duration duration
  307. :reply-to reply-to
  308. :reply-markup reply-markup))
  309. (:sticker (telegram-send-sticker chat-id (cdr message)
  310. :reply-to reply-to
  311. :reply-markup reply-markup)))
  312. (mapc #'(lambda (m) (bot-send-message m
  313. :chat-id chat-id
  314. :parse-mode parse-mode
  315. :disable-web-preview disable-web-preview
  316. :disable-notification disable-notification
  317. :reply-to reply-to
  318. :reply-markup reply-markup
  319. :duration duration)) message))
  320. (telegram-send-message chat-id message
  321. :parse-mode parse-mode
  322. :disable-web-preview disable-web-preview
  323. :disable-notification disable-notification
  324. :reply-to reply-to
  325. :reply-markup reply-markup))
  326. (error (e)
  327. (log:error e))))