chatikbot.lisp 10 KB

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