chatikbot.lisp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. (defun send-response (chat-id response &optional reply-id)
  19. (if (consp response)
  20. (if (keywordp (car response))
  21. (case (car response)
  22. (:text (telegram-send-message chat-id (cdr response) :reply-to reply-id))
  23. (:sticker (telegram-send-sticker chat-id (cdr response) :reply-to reply-id)))
  24. (mapc #'(lambda (r) (send-response chat-id r reply-id)) response))
  25. (telegram-send-message chat-id response :reply-to reply-id)))
  26. (defun send-dont-understand (chat-id &optional text reply-id)
  27. (let ((resp (eliza text)))
  28. (log:info text resp)
  29. (when resp
  30. (send-response chat-id resp reply-id))))
  31. (defvar *chat-locations* nil "ALIST of chat->location")
  32. (defun preprocess-input (text)
  33. (when text
  34. (let ((first-word (subseq text 0 (position #\Space text))))
  35. (if (equal first-word "@chatikbot")
  36. (preprocess-input (subseq text 11))
  37. (replace-all text "@chatikbot" "ты")))))
  38. (defun handle-message (message)
  39. (let ((id (aget "message_id" message))
  40. (chat-id (aget "id" (aget "chat" message)))
  41. (text (aget "text" message))
  42. (location (aget "location" message))
  43. (sticker (aget "file_id" (aget "sticker" message))))
  44. (log:info "handle-message" message)
  45. (when text
  46. (if (equal #\/ (char text 0))
  47. (let ((cmd (intern (string-upcase
  48. (subseq text 1 (position #\Space text)))
  49. "KEYWORD"))
  50. (args (when (position #\Space text)
  51. (split-sequence:split-sequence
  52. #\Space (subseq text (1+ (position #\Space text)))))))
  53. (case cmd
  54. (:postakb (handle-cmd-post-akb chat-id id args))
  55. (:akb (handle-cmd-akb chat-id id args))
  56. (:weather (handle-cmd-weather chat-id id args))
  57. (:hourly (handle-cmd-weather chat-id id '("hourly")))
  58. (:daily (handle-cmd-weather chat-id id '("daily")))
  59. (:rates (handle-cmd-rates chat-id id args))
  60. (:postcheckins (handle-cmd-post-checkins chat-id id args))
  61. (:friends (handle-cmd-fsq-friends chat-id id args))
  62. (:checkins (handle-cmd-checkins chat-id id args))
  63. (otherwise (handle-admin-cmd chat-id text cmd args))))
  64. (send-dont-understand chat-id (preprocess-input text))))
  65. (when location
  66. (push (cons chat-id location) *chat-locations*)
  67. (telegram-send-message chat-id "Взял на карандаш"))
  68. (when sticker
  69. (send-dont-understand chat-id))))
  70. (defmacro handling-errors (&body body)
  71. `(handler-case (progn ,@body)
  72. (simple-condition (err)
  73. (format *error-output* "~&~A: ~%" (class-name (class-of err)))
  74. (apply (function format) *error-output*
  75. (simple-condition-format-control err)
  76. (simple-condition-format-arguments err))
  77. (format *error-output* "~&"))
  78. (condition (err)
  79. (format *error-output* "~&~A: ~% ~S~%"
  80. (class-name (class-of err)) err))))
  81. (defun rep (input)
  82. (when input
  83. (with-output-to-string (*standard-output*)
  84. (let ((*package* (find-package 'chatikbot))
  85. (*error-output* *standard-output*))
  86. (handling-errors
  87. (format t "~{~S~^ ;~% ~}~%"
  88. (multiple-value-list (eval (read-from-string input)))))))))
  89. (defun handle-admin-cmd (chat-id text cmd args)
  90. (if (find chat-id *admins*)
  91. (case cmd
  92. (:eval (telegram-send-message chat-id (rep (format nil "~{~A~^ ~}" args))))
  93. (otherwise (send-dont-understand chat-id (preprocess-input (subseq text 1)))))
  94. (send-dont-understand chat-id (preprocess-input (subseq text 1)))))
  95. ;; AKB
  96. (defparameter +akb-vk-domain+ "baneks" "VK.com username of 'B-category anekdotes'")
  97. (defvar *akb-send-to* nil "List of chat-id's to send AKBs to")
  98. (defun handle-cmd-post-akb (chat-id message-id args)
  99. (log:info "handle-cmd-post-akb" chat-id message-id args)
  100. (let ((message "Хуярим аники"))
  101. (if (member chat-id *akb-send-to*)
  102. (setf message "Не хуярим больше аники"
  103. *akb-send-to* (set-difference *akb-send-to*
  104. (list chat-id)))
  105. (setf *akb-send-to* (cons chat-id *akb-send-to*)))
  106. (telegram-send-message chat-id message)))
  107. (defvar *akb-max-count* 5 "Max number of tweets to return per run")
  108. (defvar *akb-last-id* 0 "id of last AKB tweet")
  109. (defun format-akb (post)
  110. (let* ((id (aget "id" post))
  111. (url (format nil "https://vk.com/~A?w=wall~A_~A"
  112. +akb-vk-domain+ (aget "from_id" post) id)))
  113. (format nil "~A~%~A" (aget "text" post) url)))
  114. (defun process-latest-akb ()
  115. (handler-case
  116. (dolist (post (reverse (aget "items" (vk-wall-get :domain +akb-vk-domain+
  117. :count *akb-max-count*))))
  118. (let ((id (aget "id" post)))
  119. (when (> id *akb-last-id*)
  120. (send-akb (format-akb post))
  121. (setf *akb-last-id* id))))
  122. (error (e) (log:error e))))
  123. (defun send-akb (text)
  124. (log:info "send-akb: ~A" text)
  125. (dolist (chat-id *akb-send-to*)
  126. (handler-case
  127. (telegram-send-message chat-id text
  128. :disable-web-preview 1)
  129. (error (e) (log:error e)))))
  130. (defun handle-cmd-akb (chat-id message-id args)
  131. (log:info "handle-cmd-akb" chat-id message-id args)
  132. (handler-case
  133. (progn
  134. (let ((total-aneks
  135. (aget "count" (vk-wall-get :domain +akb-vk-domain+ :count 1 :offset 10000000))))
  136. (dolist (post (aget "items" (vk-wall-get :domain +akb-vk-domain+
  137. :count (or (ignore-errors (parse-integer (car args))) 1)
  138. :offset (random total-aneks))))
  139. (handler-case
  140. (telegram-send-message chat-id
  141. (format-akb post)
  142. :disable-web-preview 1)
  143. (error (e) (log:error e))))))
  144. (error (e)
  145. (log:error e)
  146. (telegram-send-message chat-id "Ошибочка вышла"))))
  147. ;; Finance
  148. (defun handle-cmd-rates (chat-id message-id args)
  149. (log:info "handle-cmd-rates" chat-id message-id args)
  150. (let ((rates (get-rates)))
  151. (telegram-send-message chat-id
  152. (format nil "Зеленый ~A, гейро ~A, британец ~A" (cdar rates) (cdadr rates) (cdaddr rates)))))
  153. ;; Weather
  154. (defun handle-cmd-weather (chat-id message-id args)
  155. (log:info "handle-cmd-weather" chat-id message-id args)
  156. (let ((location (cdr (assoc chat-id *chat-locations*))))
  157. (telegram-send-message
  158. chat-id
  159. (if location
  160. (handler-case
  161. (forecast-format (forecast
  162. (aget "latitude" location)
  163. (aget "longitude" location)
  164. :hourly (find "hourly" args :key #'string-downcase :test #'equal)
  165. :daily (find "daily" args :key #'string-downcase :test #'equal)))
  166. (error (e)
  167. (log:error e)
  168. "Ошибочка вышла"))
  169. "Так а ты чьих будешь?"))))
  170. ;; Foursquare
  171. (defvar *fsq-send-to* (make-hash-table)
  172. "Hash of chat-id's to fsq users list to send checkings to")
  173. (defun fsq-user-name (user)
  174. (when user
  175. (format nil "~@[~A~]~@[ ~A~]"
  176. (aget "firstName" user)
  177. (aget "lastName" user))))
  178. (defun handle-cmd-post-checkins (chat-id message-id args)
  179. (log:info "handle-cmd-post-checkins" chat-id message-id args)
  180. (handler-case
  181. (let ((users (gethash chat-id *fsq-send-to*))
  182. (friends (fsq-fetch-friends)))
  183. (if (null args)
  184. (telegram-send-message chat-id
  185. (if (null users)
  186. "Пока никого не палим"
  187. (format nil "Палим ~{~A~^, ~}"
  188. (loop for user in friends
  189. when (member (aget "id" user)
  190. users :test #'equal)
  191. collect (fsq-user-name user)))))
  192. (progn
  193. (dolist (user args)
  194. (let ((username (fsq-user-name
  195. (find user friends
  196. :test #'equal
  197. :key #'(lambda (f) (aget "id" f))))))
  198. (when username
  199. (if (member user users :test #'equal)
  200. (progn
  201. (setf users (remove user users :test #'equal))
  202. (telegram-send-message chat-id
  203. (format nil "Больше не палим ~A" username)))
  204. (progn
  205. (push user users)
  206. (telegram-send-message chat-id (format nil "Теперь палим ~A" username)))))))
  207. (setf (gethash chat-id *fsq-send-to*) users))))
  208. (error (e)
  209. (log:error e)
  210. (telegram-send-message chat-id "Ошибочка вышла"))))
  211. (defun handle-cmd-fsq-friends (chat-id message-id args)
  212. (log:info "handle-cmd-fsq-friends" chat-id message-id args)
  213. (handler-case
  214. (let ((users (gethash chat-id *fsq-send-to*))
  215. (friends (fsq-fetch-friends)))
  216. (telegram-send-message
  217. chat-id
  218. (format
  219. nil "~{~A: ~:[~;📍 ~]~A~^~%~}"
  220. (loop for user in friends
  221. append (list
  222. (aget "id" user)
  223. (member (aget "id" user) users :test #'equal)
  224. (fsq-user-name user))))))
  225. (error (e) (log:error e))))
  226. (defun handle-cmd-checkins (chat-id message-id args)
  227. (log:info "handle-cmd-checkins" chat-id message-id args)
  228. (handler-case
  229. (let ((users (gethash chat-id *fsq-send-to*)))
  230. (when users
  231. (telegram-send-message
  232. chat-id
  233. (format nil "~{~A~^~%~}"
  234. (loop for checkin in (fsq-fetch-checkins)
  235. if (member (aget "id" (aget "user" checkin)) users :test #'equal)
  236. collect (fsq-format-checkin checkin t))))))
  237. (error (e) (log:error e))))
  238. (defvar *fsq-seen-ids* nil "Ids of seen checkins")
  239. (defun process-latest-checkins ()
  240. (handler-case
  241. (dolist (checkin (fsq-fetch-new-checkins))
  242. (let ((id (aget "id" checkin))
  243. (user (aget "id" (aget "user" checkin))))
  244. (unless (find id *fsq-seen-ids* :test #'equal)
  245. (loop for chat-id being the hash-keys in *fsq-send-to* using (hash-value users)
  246. do (when (member user users :test #'equal)
  247. (telegram-send-message chat-id (fsq-format-checkin checkin))))
  248. (push id *fsq-seen-ids*))))
  249. (error (e) (log:error e))))
  250. (defun start ()
  251. (mapc #'trivial-timers:unschedule-timer (trivial-timers:list-all-timers))
  252. (let ((old-updates (find "process-updates"
  253. (bordeaux-threads:all-threads)
  254. :key #'bordeaux-threads:thread-name
  255. :test #'equal)))
  256. (when old-updates
  257. (bordeaux-threads:destroy-thread old-updates)))
  258. (clon:schedule-function
  259. (lambda () (process-latest-akb))
  260. (clon:make-scheduler
  261. (clon:make-typed-cron-schedule :minute '* :hour '*)
  262. :allow-now-p t)
  263. :thread t)
  264. (clon:schedule-function
  265. (lambda () (process-latest-checkins))
  266. (clon:make-scheduler
  267. (clon:make-typed-cron-schedule :minute '* :hour '*)
  268. :allow-now-p t)
  269. :thread t)
  270. (bordeaux-threads:make-thread
  271. (lambda ()
  272. (in-package #:chatikbot)
  273. (loop-with-error-backoff #'process-updates))
  274. :name "process-updates"))