1
0

chatikbot.lisp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. (in-package #:chatikbot)
  2. ;; Load config file
  3. (alexandria:when-let (file (probe-file
  4. (merge-pathnames "config.lisp"
  5. (asdf:component-pathname
  6. (asdf:find-system '#:chatikbot)))))
  7. (load file))
  8. (defvar *telegram-last-update* nil "Telegram last update_id")
  9. (defun process-updates ()
  10. (loop for update in (telegram-get-updates :offset (and *telegram-last-update*
  11. (1+ *telegram-last-update*))
  12. :timeout 60)
  13. do (handle-message (aget "message" update))
  14. do (setf *telegram-last-update*
  15. (max (or *telegram-last-update* 0)
  16. (aget "update_id" update)))))
  17. (defun send-dont-understand (chat-id &optional reply-id)
  18. (telegram-send-message chat-id "Ну хуууй знает" :reply-to reply-id))
  19. (defun handle-message (message)
  20. (let ((id (aget "message-id" message))
  21. (chat-id (aget "id" (aget "chat" message)))
  22. (text (aget "text" message)))
  23. (log:info "handle-message: ~A" message)
  24. (when text
  25. (if (equal #\/ (char text 0))
  26. (let ((cmd (intern (string-upcase (subseq text 1)) "KEYWORD")))
  27. (case cmd
  28. (:akb (toggle-akb chat-id id))
  29. (otherwise (send-dont-understand chat-id id))))
  30. (send-dont-understand chat-id id)))))
  31. (defparameter +akb-user-id+ "3021296351" "Twitter user id of 'B-category anecdotes'")
  32. (defvar *akb-max-count* 5 "Max number of tweets to return per run")
  33. (defvar *akb-last-id* nil "id of last AKB tweet")
  34. (defvar *akb-send-to* nil "List of chat-id's to send AKBs to")
  35. (defun toggle-akb (chat-id message-id)
  36. (let ((message "Хуярим аники"))
  37. (if (member chat-id *akb-send-to*)
  38. (setf message "Не хуярим больше аники"
  39. *akb-send-to* (set-difference *akb-send-to*
  40. (list chat-id)))
  41. (setf *akb-send-to* (cons chat-id *akb-send-to*)))
  42. (telegram-send-message chat-id message :reply-to message-id)))
  43. (defun process-latest-akb ()
  44. (loop for (id . text) in (get-tweets +akb-user-id+
  45. :since-id *akb-last-id*
  46. :count *akb-max-count*)
  47. do (handle-akb text)
  48. do (setf *akb-last-id* (max (or *akb-last-id* 0) id))))
  49. (defun handle-akb (text)
  50. (log:info "handle-akb: ~A" text)
  51. (loop for chat-id in *akb-send-to*
  52. do (telegram-send-message chat-id
  53. (replace-all text " / " (coerce '(#\Newline) 'string)))))
  54. (defvar *crons* (list
  55. (list #'process-latest-akb '(:minute (member 0 5 10 15 20 25 30 35 40 45 50 55)))
  56. )
  57. "List of cron functions with their schedules")
  58. (defvar *cron-timers* nil)
  59. (defun start ()
  60. (setf *cron-timers*
  61. (loop for (function schedule) in *crons*
  62. do (log:info "Starting cron" function schedule)
  63. collect
  64. (clon:schedule-function
  65. (lambda () (funcall function))
  66. (clon:make-scheduler
  67. (apply #'clon:make-typed-cron-schedule schedule)
  68. :allow-now-p t)
  69. :thread t)))
  70. (loop
  71. do (process-updates)))