telegram.lisp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. (response
  11. (json-request (format nil +telegram-api-format+
  12. *telegram-token* method)
  13. :method :post
  14. :parameters params)))
  15. (unless (aget "ok" response)
  16. (error (aget "description" response)))
  17. (aget "result" response)))
  18. (defun telegram-get-updates (&key offset limit timeout)
  19. (%telegram-api-call
  20. "getUpdates"
  21. (list (cons "offset" offset)
  22. (cons "limit" limit)
  23. (cons "timeout" timeout))))
  24. (defun telegram-send-message (chat-id text &key disable-web-preview reply-to reply-markup)
  25. (%telegram-api-call
  26. "sendMessage"
  27. (list (cons "chat_id" chat-id)
  28. (cons "text" text)
  29. (cons "disable_web_page_preview" disable-web-preview)
  30. (cons "reply_to_message_id" reply-to)
  31. (cons "reply_markup" reply-markup))))
  32. (defun telegram-send-sticker (chat-id sticker &key reply-to reply-markup)
  33. (%telegram-api-call
  34. "sendSticker"
  35. (list (cons "chat_id" chat-id)
  36. (cons "sticker" sticker)
  37. (cons "reply_to_message_id" reply-to)
  38. (cons "reply_markup" reply-markup))))
  39. (defun telegram-send-photo (chat-id photo &key caption reply-to reply-markup)
  40. (%telegram-api-call
  41. "sendPhoto"
  42. (list (cons "chat_id" chat-id)
  43. (cons "photo" photo)
  44. (cons "caption" caption)
  45. (cons "reply_to_message_id" reply-to)
  46. (cons "reply_markup" reply-markup))))
  47. (defun telegram-send-chat-action (chat-id action)
  48. (%telegram-api-call
  49. "sendChatAction"
  50. (list (cons "chat_id" chat-id)
  51. (cons "action" action))))