1
0

chatikbot.lisp 4.3 KB

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