telegram.lisp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. ;; (princ-to-string v))))
  9. ;; (response (yason:parse
  10. ;; (flexi-streams:octets-to-string
  11. ;; (drakma:http-request (format nil +telegram-api-format+ *telegram-token* method)
  12. ;; :method :post
  13. ;; :parameters params
  14. ;; :external-format-out :utf8)
  15. ;; :external-format :utf8)
  16. ;; :object-as :alist)))
  17. ;; (unless (aget "ok" response)
  18. ;; (error (aget "description" response)))
  19. ;; (aget "result" response)))
  20. (defun %telegram-api-call (method &optional args)
  21. (let* ((params (loop for (k . v) in args collect (cons
  22. (princ-to-string k)
  23. (if (pathnamep v) v
  24. (princ-to-string v)))))
  25. (timeout (+ 5 (or (cdr (assoc :timeout args))
  26. *telegram-timeout*)))
  27. (response (yason:parse
  28. (flexi-streams:octets-to-string
  29. (handler-case
  30. (bordeaux-threads:with-timeout (timeout)
  31. (dex:post (format nil +telegram-api-format+ *telegram-token* method)
  32. :content params
  33. :use-connection-pool t
  34. :force-binary t))
  35. (bordeaux-threads:timeout (e)
  36. (declare (ignore e))
  37. (error "Timeout")))
  38. :external-format :utf8)
  39. :object-as :alist)))
  40. (unless (aget "ok" response)
  41. (error (aget "description" response)))
  42. (aget "result" response)))
  43. (defun telegram-get-updates (&key offset limit timeout)
  44. (%telegram-api-call
  45. "getUpdates"
  46. (list (cons "offset" offset)
  47. (cons "limit" limit)
  48. (cons "timeout" timeout))))
  49. (defun telegram-send-message (chat-id text &key disable-web-preview reply-to reply-markup)
  50. (%telegram-api-call
  51. "sendMessage"
  52. (list (cons "chat_id" chat-id)
  53. (cons "text" text)
  54. (cons "disable_web_page_preview" disable-web-preview)
  55. (cons "reply_to_message_id" reply-to)
  56. (cons "reply_markup" reply-markup))))
  57. (defun telegram-send-sticker (chat-id sticker &key reply-to reply-markup)
  58. (%telegram-api-call
  59. "sendSticker"
  60. (list (cons "chat_id" chat-id)
  61. (cons "sticker" sticker)
  62. (cons "reply_to_message_id" reply-to)
  63. (cons "reply_markup" reply-markup))))
  64. (defun telegram-send-photo (chat-id photo &key caption reply-to reply-markup)
  65. (%telegram-api-call
  66. "sendPhoto"
  67. (list (cons "chat_id" chat-id)
  68. (cons "photo" photo)
  69. (cons "caption" caption)
  70. (cons "reply_to_message_id" reply-to)
  71. (cons "reply_markup" reply-markup))))
  72. (defun telegram-send-chat-action (chat-id action)
  73. (%telegram-api-call
  74. "sendChatAction"
  75. (list (cons "chat_id" chat-id)
  76. (cons "action" action))))