chatikbot.lisp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. (save-settings))
  69. (when sticker
  70. (send-dont-understand chat-id))))
  71. (defmacro handling-errors (&body body)
  72. `(handler-case (progn ,@body)
  73. (simple-condition (err)
  74. (format *error-output* "~&~A: ~%" (class-name (class-of err)))
  75. (apply (function format) *error-output*
  76. (simple-condition-format-control err)
  77. (simple-condition-format-arguments err))
  78. (format *error-output* "~&"))
  79. (condition (err)
  80. (format *error-output* "~&~A: ~% ~S~%"
  81. (class-name (class-of err)) err))))
  82. (defun rep (input)
  83. (when input
  84. (with-output-to-string (*standard-output*)
  85. (let ((*package* (find-package 'chatikbot))
  86. (*error-output* *standard-output*))
  87. (handling-errors
  88. (format t "~{~S~^ ;~% ~}~%"
  89. (multiple-value-list (eval (read-from-string input)))))))))
  90. (defun handle-admin-cmd (chat-id text cmd args)
  91. (if (find chat-id *admins*)
  92. (case cmd
  93. (:eval (telegram-send-message chat-id (rep (format nil "~{~A~^ ~}" args))))
  94. (otherwise (send-dont-understand chat-id (preprocess-input (subseq text 1)))))
  95. (send-dont-understand chat-id (preprocess-input (subseq text 1)))))
  96. ;; AKB
  97. (defparameter +akb-vk-domain+ "baneks" "VK.com username of 'B-category anekdotes'")
  98. (defvar *akb-send-to* nil "List of chat-id's to send AKBs to")
  99. (defun handle-cmd-post-akb (chat-id message-id args)
  100. (log:info "handle-cmd-post-akb" chat-id message-id args)
  101. (let ((message "Хуярим аники"))
  102. (if (member chat-id *akb-send-to*)
  103. (setf message "Не хуярим больше аники"
  104. *akb-send-to* (set-difference *akb-send-to*
  105. (list chat-id)))
  106. (setf *akb-send-to* (cons chat-id *akb-send-to*)))
  107. (telegram-send-message chat-id message)
  108. (save-settings)))
  109. (defvar *akb-max-count* 5 "Max number of tweets to return per run")
  110. (defvar *akb-last-id* 0 "id of last AKB tweet")
  111. (defun format-akb (post)
  112. (let* ((id (aget "id" post))
  113. (url (format nil "https://vk.com/~A?w=wall~A_~A"
  114. +akb-vk-domain+ (aget "from_id" post) id)))
  115. (format nil "~A~%~A" (aget "text" post) url)))
  116. (defun process-latest-akb ()
  117. (handler-case
  118. (dolist (post (reverse (aget "items" (vk-wall-get :domain +akb-vk-domain+
  119. :count *akb-max-count*))))
  120. (let ((id (aget "id" post)))
  121. (when (> id *akb-last-id*)
  122. (send-akb (format-akb post))
  123. (setf *akb-last-id* id)
  124. (save-settings))))
  125. (error (e) (log:error e))))
  126. (defun send-akb (text)
  127. (log:info "send-akb: ~A" text)
  128. (dolist (chat-id *akb-send-to*)
  129. (handler-case
  130. (telegram-send-message chat-id text
  131. :disable-web-preview 1)
  132. (error (e) (log:error e)))))
  133. (defun handle-cmd-akb (chat-id message-id args)
  134. (log:info "handle-cmd-akb" chat-id message-id args)
  135. (handler-case
  136. (progn
  137. (let ((total-aneks
  138. (aget "count" (vk-wall-get :domain +akb-vk-domain+ :count 1 :offset 10000000))))
  139. (dolist (post (aget "items" (vk-wall-get :domain +akb-vk-domain+
  140. :count (or (ignore-errors (parse-integer (car args))) 1)
  141. :offset (random total-aneks))))
  142. (handler-case
  143. (telegram-send-message chat-id
  144. (format-akb post)
  145. :disable-web-preview 1)
  146. (error (e) (log:error e))))))
  147. (error (e)
  148. (log:error e)
  149. (telegram-send-message chat-id "Ошибочка вышла"))))
  150. ;; Finance
  151. (defun handle-cmd-rates (chat-id message-id args)
  152. (log:info "handle-cmd-rates" chat-id message-id args)
  153. (let ((rates (get-rates)))
  154. (telegram-send-message chat-id
  155. (format nil "Зеленый ~A, гейро ~A, британец ~A" (cdar rates) (cdadr rates) (cdaddr rates)))))
  156. ;; Weather
  157. (defun handle-cmd-weather (chat-id message-id args)
  158. (log:info "handle-cmd-weather" chat-id message-id args)
  159. (let ((location (cdr (assoc chat-id *chat-locations*))))
  160. (telegram-send-message
  161. chat-id
  162. (if location
  163. (handler-case
  164. (forecast-format (forecast
  165. (aget "latitude" location)
  166. (aget "longitude" location)
  167. :hourly (find "hourly" args :key #'string-downcase :test #'equal)
  168. :daily (find "daily" args :key #'string-downcase :test #'equal)))
  169. (error (e)
  170. (log:error e)
  171. "Ошибочка вышла"))
  172. "Так а ты чьих будешь?"))))
  173. ;; Foursquare
  174. (defvar *fsq-send-to* (make-hash-table)
  175. "Hash of chat-id's to fsq users list to send checkings to")
  176. (defun fsq-user-name (user)
  177. (when user
  178. (format nil "~@[~A~]~@[ ~A~]"
  179. (aget "firstName" user)
  180. (aget "lastName" user))))
  181. (defun handle-cmd-post-checkins (chat-id message-id args)
  182. (log:info "handle-cmd-post-checkins" chat-id message-id args)
  183. (handler-case
  184. (let ((users (gethash chat-id *fsq-send-to*))
  185. (friends (fsq-fetch-friends)))
  186. (if (null args)
  187. (telegram-send-message chat-id
  188. (if (null users)
  189. "Пока никого не палим"
  190. (format nil "Палим ~{~A~^, ~}"
  191. (loop for user in friends
  192. when (member (aget "id" user)
  193. users :test #'equal)
  194. collect (fsq-user-name user)))))
  195. (progn
  196. (dolist (user args)
  197. (let ((username (fsq-user-name
  198. (find user friends
  199. :test #'equal
  200. :key #'(lambda (f) (aget "id" f))))))
  201. (when username
  202. (if (member user users :test #'equal)
  203. (progn
  204. (setf users (remove user users :test #'equal))
  205. (telegram-send-message chat-id
  206. (format nil "Больше не палим ~A" username)))
  207. (progn
  208. (push user users)
  209. (telegram-send-message chat-id (format nil "Теперь палим ~A" username)))))))
  210. (setf (gethash chat-id *fsq-send-to*) users)
  211. (save-settings))))
  212. (error (e)
  213. (log:error e)
  214. (telegram-send-message chat-id "Ошибочка вышла"))))
  215. (defun handle-cmd-fsq-friends (chat-id message-id args)
  216. (log:info "handle-cmd-fsq-friends" chat-id message-id args)
  217. (handler-case
  218. (let ((users (gethash chat-id *fsq-send-to*))
  219. (friends (fsq-fetch-friends)))
  220. (telegram-send-message
  221. chat-id
  222. (format
  223. nil "~{~A: ~:[~;📍 ~]~A~^~%~}"
  224. (loop for user in friends
  225. append (list
  226. (aget "id" user)
  227. (member (aget "id" user) users :test #'equal)
  228. (fsq-user-name user))))))
  229. (error (e) (log:error e))))
  230. (defun handle-cmd-checkins (chat-id message-id args)
  231. (log:info "handle-cmd-checkins" chat-id message-id args)
  232. (handler-case
  233. (let ((users (gethash chat-id *fsq-send-to*)))
  234. (when users
  235. (telegram-send-message
  236. chat-id
  237. (format nil "~{~A~^~%~}"
  238. (loop for checkin in (fsq-fetch-checkins)
  239. if (member (aget "id" (aget "user" checkin)) users :test #'equal)
  240. collect (fsq-format-checkin checkin t))))))
  241. (error (e) (log:error e))))
  242. (defvar *fsq-seen-ids* nil "Ids of seen checkins")
  243. (defun process-latest-checkins ()
  244. (handler-case
  245. (let ((checkins (make-hash-table)))
  246. (dolist (checkin (fsq-fetch-new-checkins))
  247. (let ((id (aget "id" checkin))
  248. (user (aget "id" (aget "user" checkin))))
  249. (unless (find id *fsq-seen-ids* :test #'equal)
  250. (loop for chat-id being the hash-keys in *fsq-send-to* using (hash-value users)
  251. do (when (member user users :test #'equal)
  252. (push (fsq-format-checkin checkin)
  253. (gethash chat-id checkins))))
  254. (push id *fsq-seen-ids*))))
  255. (loop for chat-id being the hash-keys in checkins using (hash-value texts)
  256. do (log:info "Sending checkins" chat-id texts)
  257. (telegram-send-message chat-id (format nil "~{~A~^~%~}" texts))))
  258. (error (e) (log:error e))))
  259. (defun save-settings()
  260. (with-open-file (s (merge-pathnames "settings.lisp"
  261. (asdf:component-pathname
  262. (asdf:find-system '#:chatikbot)))
  263. :direction :output
  264. :if-exists :supersede
  265. :if-does-not-exist :create)
  266. (write '(in-package #:chatikbot) :stream s)
  267. (write
  268. `(setf *fsq-send-to* (alexandria:alist-hash-table ',(alexandria:hash-table-alist *fsq-send-to*))
  269. *chat-locations* ',*chat-locations*
  270. *akb-send-to* ',*akb-send-to*
  271. *akb-last-id* ,*akb-last-id*)
  272. :stream s)))
  273. (defun start ()
  274. ;; Clear prev threads
  275. (mapc #'trivial-timers:unschedule-timer (trivial-timers:list-all-timers))
  276. (let ((old-updates (find "process-updates"
  277. (bordeaux-threads:all-threads)
  278. :key #'bordeaux-threads:thread-name
  279. :test #'equal)))
  280. (when old-updates
  281. (bordeaux-threads:destroy-thread old-updates)))
  282. ;; Load settings file
  283. (alexandria:when-let (file (probe-file
  284. (merge-pathnames "settings.lisp"
  285. (asdf:component-pathname
  286. (asdf:find-system '#:chatikbot)))))
  287. (load file))
  288. ;; Start timers
  289. (clon:schedule-function
  290. (lambda () (process-latest-akb))
  291. (clon:make-scheduler
  292. (clon:make-typed-cron-schedule :minute '* :hour '*)
  293. :allow-now-p t)
  294. :thread t)
  295. (clon:schedule-function
  296. (lambda () (process-latest-checkins))
  297. (clon:make-scheduler
  298. (clon:make-typed-cron-schedule :minute '* :hour '*)
  299. :allow-now-p t)
  300. :thread t)
  301. ;; Start getUpdates thread
  302. (bordeaux-threads:make-thread
  303. (lambda ()
  304. (in-package #:chatikbot)
  305. (loop-with-error-backoff #'process-updates))
  306. :name "process-updates"))