chatikbot.lisp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. (handler-case
  11. (loop for update in (telegram-get-updates :offset (and *telegram-last-update*
  12. (1+ *telegram-last-update*))
  13. :timeout 300)
  14. do (handle-message (aget "message" update))
  15. do (setf *telegram-last-update*
  16. (max (or *telegram-last-update* 0)
  17. (aget "update_id" update))))
  18. (error (e)
  19. (log:error e))))
  20. (defvar *responses*
  21. '((:text . "И чё?")
  22. (:text . "Сам-то понял?")
  23. (:text . "Ну хуй знает")
  24. (:text . "Бля...")
  25. (:text . "В душе не ебу")
  26. (:text . "Мне похуй")
  27. (:text . "Eбаный ты нахуй")
  28. (:text . "Отъебись")
  29. (:sticker . "BQADAgADFAADOoERAAGoLKS_Vgs6GgI") ;; ЭЭ епта Чо
  30. (:sticker . "BQADAgADGQADOoERAAFDXJisD4fClgI") ;; Ну чё ты несёшь
  31. (:sticker . "BQADAgADFwADOoERAAHCw-fBiFjACgI") ;; А у меня собака, я не могу
  32. (:sticker . "BQADAgADEgADOoERAAFtU3uF9HvtQgI") ;; Бухнём?
  33. (:sticker . "BQADBAADQAEAAnscSQABqWydSKTnASoC")) ;; Trollface
  34. "Unknown command respond strings")
  35. (defun random-choice (messages)
  36. (nth (random (length messages)) messages))
  37. (defun send-dont-understand (chat-id &optional text reply-id)
  38. (if (and text (zerop (random 5)))
  39. ;; Reply to "пидор" with "сам пидор" in 20%
  40. (telegram-send-message chat-id
  41. (format nil "Сам ~A"
  42. (replace-all
  43. (if (equal (char text 0) #\/)
  44. (subseq text 1)
  45. text)
  46. "@chatikbot" ""))
  47. :reply-to reply-id)
  48. ;; Reply with predefined responses
  49. (let ((r (random-choice *responses*)))
  50. (case (car r)
  51. (:text (telegram-send-message chat-id (cdr r) :reply-to reply-id))
  52. (:sticker (telegram-send-sticker chat-id (cdr r) :reply-to reply-id))))))
  53. (defvar *chat-locations* nil "ALIST of chat->location")
  54. (defun handle-message (message)
  55. (let ((id (aget "message_id" message))
  56. (chat-id (aget "id" (aget "chat" message)))
  57. (text (aget "text" message))
  58. (location (aget "location" message))
  59. (sticker (aget "file_id" (aget "sticker" message))))
  60. (log:info "handle-message" message)
  61. (when text
  62. (if (equal #\/ (char text 0))
  63. (let ((cmd (intern (string-upcase
  64. (subseq text 1 (position #\Space text)))
  65. "KEYWORD"))
  66. (args (when (position #\Space text)
  67. (split-sequence:split-sequence
  68. #\Space (subseq text (1+ (position #\Space text)))))))
  69. (case cmd
  70. (:postakb (handle-cmd-post-akb chat-id id args))
  71. (:akb (handle-cmd-akb chat-id id args))
  72. (:weather (handle-cmd-weather chat-id id args))
  73. (:hourly (handle-cmd-weather chat-id id '("hourly")))
  74. (:daily (handle-cmd-weather chat-id id '("daily")))
  75. (:help (handle-cmd-help chat-id id args))
  76. (otherwise (send-dont-understand chat-id text))))
  77. (send-dont-understand chat-id text)))
  78. (when location
  79. (push (cons chat-id location) *chat-locations*)
  80. (telegram-send-message chat-id "Взял на карандаш"))
  81. (when sticker
  82. ;; Save incoming stickers in 20% of the cases if it's not already there
  83. (if (and (zerop (random 5))
  84. (not (find sticker *responses* :key #'cdr :test #'equal)))
  85. (progn
  86. (push (cons :sticker sticker) *responses*)
  87. (telegram-send-message chat-id "Припомним"))
  88. (send-dont-understand chat-id)))))
  89. (defparameter +akb-vk-domain+ "baneks" "VK.com username of 'B-category anekdotes'")
  90. (defvar *akb-send-to* nil "List of chat-id's to send AKBs to")
  91. (defun handle-cmd-post-akb (chat-id message-id args)
  92. (log:info "handle-cmd-post-akb" chat-id message-id args)
  93. (let ((message "Хуярим аники"))
  94. (if (member chat-id *akb-send-to*)
  95. (setf message "Не хуярим больше аники"
  96. *akb-send-to* (set-difference *akb-send-to*
  97. (list chat-id)))
  98. (setf *akb-send-to* (cons chat-id *akb-send-to*)))
  99. (telegram-send-message chat-id message)))
  100. (defvar *akb-max-count* 5 "Max number of tweets to return per run")
  101. (defvar *akb-last-id* 0 "id of last AKB tweet")
  102. (defun format-akb (post)
  103. (let* ((id (aget "id" post))
  104. (url (format nil "https://vk.com/~A?w=wall~A_~A"
  105. +akb-vk-domain+ (aget "from_id" post) id)))
  106. (format nil "~A~%~A" (aget "text" post) url)))
  107. (defun process-latest-akb ()
  108. (log:info "Getting latest AKBs")
  109. (handler-case
  110. (dolist (post (reverse (aget "items" (vk-wall-get :domain +akb-vk-domain+
  111. :count *akb-max-count*))))
  112. (let ((id (aget "id" post)))
  113. (when (> id *akb-last-id*)
  114. (send-akb (format-akb post))
  115. (setf *akb-last-id* id))))
  116. (error (e) (log:error e))))
  117. (defun send-akb (text)
  118. (log:info "send-akb: ~A" text)
  119. (dolist (chat-id *akb-send-to*)
  120. (handler-case
  121. (telegram-send-message chat-id text
  122. :disable-web-preview 1)
  123. (error (e) (log:error e)))))
  124. (defun handle-cmd-akb (chat-id message-id args)
  125. (log:info "handle-cmd-akb" chat-id message-id args)
  126. (let ((total-aneks
  127. (aget "count" (vk-wall-get :domain +akb-vk-domain+ :count 1 :offset 10000000))))
  128. (dolist (post (aget "items" (vk-wall-get :domain +akb-vk-domain+
  129. :count (or (ignore-errors (parse-integer (car args))) 1)
  130. :offset (random total-aneks))))
  131. (handler-case
  132. (telegram-send-message chat-id
  133. (format-akb post)
  134. :disable-web-preview 1)
  135. (error (e) (log:error e))))))
  136. (defvar *help-responses*
  137. (list "Сам себе помоги, нахуй!" "Вот заняться мне больше нечем" "Нахуй пошел!"
  138. "Хэлп, ай нид самбади, хелп нот джаст энибади" "Отъебись"))
  139. (defun handle-cmd-help (chat-id message-id args)
  140. (log:info "handle-cmd-help" chat-id message-id args)
  141. (telegram-send-message chat-id (random-choice *help-responses*)))
  142. (defun handle-cmd-weather (chat-id message-id args)
  143. (log:info "handle-cmd-weather" chat-id message-id args)
  144. (let ((location (cdr (assoc chat-id *chat-locations*))))
  145. (telegram-send-message
  146. chat-id
  147. (if location
  148. (forecast-format (forecast
  149. (aget "latitude" location)
  150. (aget "longitude" location)
  151. :hourly (find "hourly" args :key #'string-downcase :test #'equal)
  152. :daily (find "daily" args :key #'string-downcase :test #'equal)))
  153. "Так а ты чьих будешь?"))))
  154. (defun start ()
  155. (mapc #'sb-ext:unschedule-timer (trivial-timers:list-all-timers))
  156. (let ((old-updates (find "process-updates"
  157. (bordeaux-threads:all-threads)
  158. :key #'bordeaux-threads:thread-name
  159. :test #'equal)))
  160. (when old-updates
  161. (bordeaux-threads:destroy-thread old-updates)))
  162. (clon:schedule-function
  163. (lambda () (process-latest-akb))
  164. (clon:make-scheduler
  165. (clon:make-typed-cron-schedule :minute '* :hour '*)
  166. :allow-now-p t)
  167. :thread t)
  168. (bordeaux-threads:make-thread
  169. (lambda ()
  170. (loop
  171. do (process-updates)))
  172. :name "process-updates"))