chatikbot.lisp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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" message)
  24. (when text
  25. (if (equal #\/ (char text 0))
  26. (let ((cmd (intern (string-upcase
  27. (subseq text 1 (position #\Space text)))
  28. "KEYWORD"))
  29. (args (when (position #\Space text)
  30. (split-sequence:split-sequence
  31. #\Space (subseq text (1+ (position #\Space text)))))))
  32. (case cmd
  33. (:akb (handle-cmd-akb chat-id id args))
  34. (:lastakb (handle-cmd-last-akb chat-id id args))
  35. (otherwise (send-dont-understand chat-id id))))
  36. (telegram-send-message chat-id
  37. (format nil "Сам ~A"
  38. (replace-all text "@chatikbot " "")))))))
  39. (defparameter +akb-user-id+ "3021296351" "Twitter user id of 'B-category anecdotes'")
  40. (defvar *akb-max-count* 5 "Max number of tweets to return per run")
  41. (defvar *akb-last-id* nil "id of last AKB tweet")
  42. (defvar *akb-send-to* nil "List of chat-id's to send AKBs to")
  43. (defun handle-cmd-akb (chat-id message-id args)
  44. (log:info "handle-cmd-akb" chat-id message-id args)
  45. (let ((message "Хуярим аники"))
  46. (if (member chat-id *akb-send-to*)
  47. (setf message "Не хуярим больше аники"
  48. *akb-send-to* (set-difference *akb-send-to*
  49. (list chat-id)))
  50. (setf *akb-send-to* (cons chat-id *akb-send-to*)))
  51. (telegram-send-message chat-id message :reply-to message-id)))
  52. (defun process-latest-akb ()
  53. (log:info "Getting latest AKBs")
  54. (loop for (id . text) in (get-tweets +akb-user-id+
  55. :since-id *akb-last-id*
  56. :count *akb-max-count*)
  57. do (send-akb text)
  58. do (setf *akb-last-id* (max (or *akb-last-id* 0) id))))
  59. (defun send-akb (text)
  60. (log:info "send-akb: ~A" text)
  61. (loop for chat-id in *akb-send-to*
  62. do (telegram-send-message chat-id
  63. (replace-all text " / " (coerce '(#\Newline) 'string))
  64. :disable-web-preview 1)))
  65. (defun handle-cmd-last-akb (chat-id message-id args)
  66. (log:info "handle-cmd-last-akb" chat-id message-id args)
  67. (loop for (id . text) in (get-tweets +akb-user-id+
  68. :count (or (car args) 1))
  69. do (telegram-send-message chat-id
  70. (replace-all text " / " (coerce '(#\Newline) 'string))
  71. :disable-web-preview 1)))
  72. (defvar *crons* (list
  73. (list #'process-latest-akb '(:minute * :hour *)))
  74. "List of cron functions with their schedules")
  75. (defvar *cron-timers* nil)
  76. (defun start ()
  77. (mapc #'sb-ext:unschedule-timer *cron-timers*)
  78. (setf *cron-timers*
  79. (loop for (function schedule) in *crons*
  80. do (log:info "Starting cron" function schedule)
  81. collect
  82. (clon:schedule-function
  83. (lambda () (funcall function))
  84. (clon:make-scheduler
  85. (apply #'clon:make-typed-cron-schedule schedule)
  86. :allow-now-p t)
  87. :thread t)))
  88. (loop
  89. do (process-updates)))