bot.lisp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. (in-package :cl-user)
  2. (defpackage chatikbot.bot
  3. (:use :cl :chatikbot.utils :chatikbot.macros)
  4. (:import-from :chatikbot.db
  5. :set-setting)
  6. (:import-from :chatikbot.telegram
  7. :telegram-get-updates
  8. :bot-send-message)
  9. (:export :handle-update
  10. :*bot-user-id*
  11. :on-next-message))
  12. (in-package :chatikbot.bot)
  13. (defvar *bot-user-id* nil "Bot user_id")
  14. (defvar *telegram-last-update* 0 "Telegram last update_id")
  15. ;; getUpdates handling
  16. (defun process-updates ()
  17. (loop for update in (telegram-get-updates :offset (and *telegram-last-update*
  18. (1+ *telegram-last-update*))
  19. :timeout 300)
  20. ;; do (setf *telegram-last-update*
  21. ;; (max *telegram-last-update* (aget "update_id" update)))
  22. do (handle-update update)))
  23. (defun handle-update (update)
  24. (log:info update)
  25. (let ((update-id (aget "update_id" update))
  26. (reply-to (agets update "message" "reply_to_message" "from" "id")))
  27. (if (> update-id *telegram-last-update*)
  28. (progn
  29. (if (and reply-to (not (equal reply-to *bot-user-id*)))
  30. (log:info "Reply not to bot")
  31. (loop for (key . value) in update
  32. unless (equal "update_id" key)
  33. do (run-hooks (keyify (format nil "update-~A" key)) value)))
  34. (setf *telegram-last-update* update-id))
  35. (log:warn "Out-of-order update" update-id))))
  36. (defvar *start-message* "Hello" "Welcome message. Override it")
  37. (def-message-cmd-handler handle-start (:start)
  38. (bot-send-message chat-id *start-message*))
  39. (def-message-admin-cmd-handler handle-admin-settings (:settings)
  40. (bot-send-message chat-id
  41. (format nil "~{~{~A~@[ (~A)~]: ~A~}~^~%~}"
  42. (loop for symbol in *settings*
  43. collect (list symbol (documentation symbol 'variable) (symbol-value symbol))))))
  44. (def-message-admin-cmd-handler handle-admin-setsetting (:setsetting)
  45. (let* ((*package* (find-package :chatikbot))
  46. (var (read-from-string (car args)))
  47. (val (read-from-string (format nil "~{~A~^ ~}" (rest args)))))
  48. (bot-send-message chat-id (format nil "OK, ~A" (set-setting var val)))))
  49. (defvar *chat-next-message-handlers* (make-hash-table) "Out-of-order chat message handler")
  50. (defmacro on-next-message (chat-id &body body)
  51. `(setf (gethash ,chat-id *chat-next-message-handlers*)
  52. (lambda (message)
  53. (with-parsed-message message
  54. ,@body))))
  55. (def-message-handler chat-next-message-handler (message 1000)
  56. (let ((handler (gethash chat-id *chat-next-message-handlers*)))
  57. (when handler
  58. (remhash chat-id *chat-next-message-handlers*)
  59. (handler-case (funcall handler message)
  60. (error (e)
  61. (log:error "~A" e)
  62. (bot-send-message chat-id
  63. (format nil "Ошибочка вышла~@[: ~A~]"
  64. (when (member chat-id *admins*) e))))))))