1
0

telegram.lisp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. (defvar *telegram-timeout* 30 "Default Telegram timeout")
  5. (defun %telegram-api-call (method &optional args)
  6. (let* ((params (loop for (k . v) in args when v
  7. collect (cons
  8. (princ-to-string k)
  9. (if (pathnamep v) v
  10. (princ-to-string v)))))
  11. (timeout (+ 5 (or (aget "timeout" args)
  12. *telegram-timeout*)))
  13. (response
  14. (handler-case
  15. (bordeaux-threads:with-timeout (timeout)
  16. (json-request (format nil +telegram-api-format+
  17. *telegram-token* method)
  18. :method :post
  19. :parameters params))
  20. (bordeaux-threads:timeout () (error "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-send-message (chat-id text &key parse-mode disable-web-preview reply-to reply-markup)
  31. (%telegram-api-call
  32. "sendMessage"
  33. (list (cons "chat_id" chat-id)
  34. (cons "text" text)
  35. (cons "parse_mode" parse-mode)
  36. (cons "disable_web_page_preview" disable-web-preview)
  37. (cons "reply_to_message_id" reply-to)
  38. (cons "reply_markup" reply-markup))))
  39. (defun telegram-forward-message (chat-id from-chat-id message-id)
  40. (%telegram-api-call
  41. "forwardMessage"
  42. `(("chat_id" . ,chat-id)
  43. ("from_chat_id" . ,from-chat-id)
  44. ("message_id" . ,message-id))))
  45. (defun telegram-send-photo (chat-id photo &key caption reply-to reply-markup)
  46. (%telegram-api-call
  47. "sendPhoto"
  48. (list (cons "chat_id" chat-id)
  49. (cons "photo" photo)
  50. (cons "caption" caption)
  51. (cons "reply_to_message_id" reply-to)
  52. (cons "reply_markup" reply-markup))))
  53. (defun telegram-send-audio (chat-id audio &key duration performer title reply-to reply-markup)
  54. (%telegram-api-call
  55. "sendAudio"
  56. (list (cons "chat_id" chat-id)
  57. (cons "audio" audio)
  58. (cons "duration" duration)
  59. (cons "performer" performer)
  60. (cons "title" title)
  61. (cons "reply_to_message_id" reply-to)
  62. (cons "reply_markup" reply-markup))))
  63. (defun telegram-send-document (chat-id document &key reply-to reply-markup)
  64. (%telegram-api-call
  65. "sendDocument"
  66. (list (cons "chat_id" chat-id)
  67. (cons "document" document)
  68. (cons "reply_to_message_id" reply-to)
  69. (cons "reply_markup" reply-markup))))
  70. (defun telegram-send-sticker (chat-id sticker &key reply-to reply-markup)
  71. (%telegram-api-call
  72. "sendSticker"
  73. (list (cons "chat_id" chat-id)
  74. (cons "sticker" sticker)
  75. (cons "reply_to_message_id" reply-to)
  76. (cons "reply_markup" reply-markup))))
  77. (defun telegram-send-video (chat-id video &key duration caption reply-to reply-markup)
  78. (%telegram-api-call
  79. "sendVideo"
  80. (list (cons "chat_id" chat-id)
  81. (cons "video" video)
  82. (cons "duration" duration)
  83. (cons "caption" caption)
  84. (cons "reply_to_message_id" reply-to)
  85. (cons "reply_markup" reply-markup))))
  86. (defun telegram-send-voice (chat-id voice &key duration reply-to reply-markup)
  87. (%telegram-api-call
  88. "sendVoice"
  89. (list (cons "chat_id" chat-id)
  90. (cons "voice" voice)
  91. (cons "duration" duration)
  92. (cons "reply_to_message_id" reply-to)
  93. (cons "reply_markup" reply-markup))))
  94. (defun telegram-send-location (chat-id latitude longitude &key reply-to reply-markup)
  95. (%telegram-api-call
  96. "sendLocation"
  97. (list (cons "chat_id" chat-id)
  98. (cons "latitude" latitude)
  99. (cons "longitude" longitude)
  100. (cons "reply_to_message_id" reply-to)
  101. (cons "reply_markup" reply-markup))))
  102. (defun telegram-send-chat-action (chat-id action)
  103. (%telegram-api-call
  104. "sendChatAction"
  105. (list (cons "chat_id" chat-id)
  106. (cons "action" action))))
  107. (defun telegram-get-user-profile-photos (user-id &key offset limit)
  108. (%telegram-api-call
  109. "getUserProfilePhotos"
  110. `(("user_id" . ,user-id) ("offset" . ,offset) ("limit" . ,limit))))
  111. (defun telegram-get-file (file-id)
  112. (%telegram-api-call "getFile" `(("file_id" . ,file-id))))