chatikbot.lisp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. (:postakb (handle-cmd-post-akb chat-id id args))
  42. (:akb (handle-cmd-akb chat-id id args))
  43. (otherwise (send-dont-understand chat-id text))))
  44. (send-dont-understand chat-id text)))))
  45. (defparameter +akb-vk-domain+ "baneks" "VK.com username of 'B-category anekdotes'")
  46. (defvar *akb-send-to* nil "List of chat-id's to send AKBs to")
  47. (defun handle-cmd-post-akb (chat-id message-id args)
  48. (log:info "handle-cmd-post-akb" chat-id message-id args)
  49. (let ((message "Хуярим аники"))
  50. (if (member chat-id *akb-send-to*)
  51. (setf message "Не хуярим больше аники"
  52. *akb-send-to* (set-difference *akb-send-to*
  53. (list chat-id)))
  54. (setf *akb-send-to* (cons chat-id *akb-send-to*)))
  55. (telegram-send-message chat-id message)))
  56. (defvar *akb-max-count* 5 "Max number of tweets to return per run")
  57. (defvar *akb-last-id* 0 "id of last AKB tweet")
  58. (defun format-akb (post)
  59. (let* ((id (aget "id" post))
  60. (url (format nil "https://vk.com/~A?w=wall~A_~A"
  61. +akb-vk-domain+ (aget "from_id" post) id)))
  62. (format nil "~A~%~A" (aget "text" post) url)))
  63. (defun process-latest-akb ()
  64. (log:info "Getting latest AKBs")
  65. (dolist (post (reverse (aget "items" (vk-wall-get :domain +akb-vk-domain+
  66. :count *akb-max-count*))))
  67. (let ((id (aget "id" post)))
  68. (when (> id *akb-last-id*)
  69. (send-akb (format-akb post))
  70. (setf *akb-last-id* id)))))
  71. (defun send-akb (text)
  72. (log:info "send-akb: ~A" text)
  73. (dolist (chat-id *akb-send-to*)
  74. (handler-case
  75. (telegram-send-message chat-id text
  76. :disable-web-preview 1)
  77. (error (e) (log:error e)))))
  78. (defun handle-cmd-akb (chat-id message-id args)
  79. (log:info "handle-cmd-akb" chat-id message-id args)
  80. (let ((total-aneks
  81. (aget "count" (vk-wall-get :domain +akb-vk-domain+ :count 1 :offset 10000000))))
  82. (dolist (post (aget "items" (vk-wall-get :domain +akb-vk-domain+
  83. :count (or (ignore-errors (parse-integer (car args))) 1)
  84. :offset (random total-aneks))))
  85. (handler-case
  86. (telegram-send-message chat-id
  87. (format-akb post)
  88. :disable-web-preview 1)
  89. (error (e) (log:error e))))))
  90. (defun start ()
  91. (mapc #'sb-ext:unschedule-timer (trivial-timers:list-all-timers))
  92. (clon:schedule-function
  93. (lambda () (process-latest-akb))
  94. (clon:make-scheduler
  95. (clon:make-typed-cron-schedule :minute '* :hour '*)
  96. :allow-now-p t)
  97. :thread t)
  98. (bordeaux-threads:make-thread
  99. (lambda ()
  100. (loop
  101. do (process-updates)))
  102. :name "process-updates"))