1
0

telegram.lisp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. (princ-to-string v))))
  24. (timeout (+ 5 (or (cdr (assoc :timeout args))
  25. *telegram-timeout*)))
  26. (response (yason:parse
  27. (flexi-streams:octets-to-string
  28. (bordeaux-threads:with-timeout (timeout)
  29. (dex:post (format nil +telegram-api-format+ *telegram-token* method)
  30. :content params
  31. :use-connection-pool t
  32. :force-binary t))
  33. :external-format :utf8)
  34. :object-as :alist)))
  35. (unless (aget "ok" response)
  36. (error (aget "description" response)))
  37. (aget "result" response)))
  38. (defun telegram-get-updates (&key offset limit timeout)
  39. (%telegram-api-call
  40. "getUpdates"
  41. (list (cons "offset" offset)
  42. (cons "limit" limit)
  43. (cons "timeout" timeout))))
  44. (defun telegram-send-message (chat-id text &key disable-web-preview reply-to reply-markup)
  45. (%telegram-api-call
  46. "sendMessage"
  47. (list (cons "chat_id" chat-id)
  48. (cons "text" text)
  49. (cons "disable_web_page_preview" disable-web-preview)
  50. (cons "reply_to_message_id" reply-to)
  51. (cons "reply_markup" reply-markup))))
  52. (defun telegram-send-sticker (chat-id sticker &key reply-to reply-markup)
  53. (%telegram-api-call
  54. "sendSticker"
  55. (list (cons "chat_id" chat-id)
  56. (cons "sticker" sticker)
  57. (cons "reply_to_message_id" reply-to)
  58. (cons "reply_markup" reply-markup))))