chatikbot.lisp 10 KB

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