telegram.lisp 4.5 KB

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