chatikbot.lisp 10 KB

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