chatikbot.lisp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. (:checkins (handle-cmd-checkins chat-id id args))
  62. (otherwise (handle-admin-cmd chat-id text cmd args))))
  63. (send-dont-understand chat-id (preprocess-input text))))
  64. (when location
  65. (push (cons chat-id location) *chat-locations*)
  66. (telegram-send-message chat-id "Взял на карандаш"))
  67. (when sticker
  68. (send-dont-understand chat-id))))
  69. (defmacro handling-errors (&body body)
  70. `(handler-case (progn ,@body)
  71. (simple-condition (err)
  72. (format *error-output* "~&~A: ~%" (class-name (class-of err)))
  73. (apply (function format) *error-output*
  74. (simple-condition-format-control err)
  75. (simple-condition-format-arguments err))
  76. (format *error-output* "~&"))
  77. (condition (err)
  78. (format *error-output* "~&~A: ~% ~S~%"
  79. (class-name (class-of err)) err))))
  80. (defun rep (input)
  81. (when input
  82. (with-output-to-string (*standard-output*)
  83. (let ((*package* (find-package 'chatikbot))
  84. (*error-output* *standard-output*))
  85. (handling-errors
  86. (format t "~{~S~^ ;~% ~}~%"
  87. (multiple-value-list (eval (read-from-string input)))))))))
  88. (defun handle-admin-cmd (chat-id text cmd args)
  89. (if (find chat-id *admins*)
  90. (case cmd
  91. (:eval (telegram-send-message chat-id (rep (format nil "~{~A~^ ~}" args))))
  92. (otherwise (send-dont-understand chat-id (preprocess-input (subseq text 1)))))
  93. (send-dont-understand chat-id (preprocess-input (subseq text 1)))))
  94. ;; AKB
  95. (defparameter +akb-vk-domain+ "baneks" "VK.com username of 'B-category anekdotes'")
  96. (defvar *akb-send-to* nil "List of chat-id's to send AKBs to")
  97. (defun handle-cmd-post-akb (chat-id message-id args)
  98. (log:info "handle-cmd-post-akb" chat-id message-id args)
  99. (let ((message "Хуярим аники"))
  100. (if (member chat-id *akb-send-to*)
  101. (setf message "Не хуярим больше аники"
  102. *akb-send-to* (set-difference *akb-send-to*
  103. (list chat-id)))
  104. (setf *akb-send-to* (cons chat-id *akb-send-to*)))
  105. (telegram-send-message chat-id message)))
  106. (defvar *akb-max-count* 5 "Max number of tweets to return per run")
  107. (defvar *akb-last-id* 0 "id of last AKB tweet")
  108. (defun format-akb (post)
  109. (let* ((id (aget "id" post))
  110. (url (format nil "https://vk.com/~A?w=wall~A_~A"
  111. +akb-vk-domain+ (aget "from_id" post) id)))
  112. (format nil "~A~%~A" (aget "text" post) url)))
  113. (defun process-latest-akb ()
  114. (handler-case
  115. (dolist (post (reverse (aget "items" (vk-wall-get :domain +akb-vk-domain+
  116. :count *akb-max-count*))))
  117. (let ((id (aget "id" post)))
  118. (when (> id *akb-last-id*)
  119. (send-akb (format-akb post))
  120. (setf *akb-last-id* id))))
  121. (error (e) (log:error e))))
  122. (defun send-akb (text)
  123. (log:info "send-akb: ~A" text)
  124. (dolist (chat-id *akb-send-to*)
  125. (handler-case
  126. (telegram-send-message chat-id text
  127. :disable-web-preview 1)
  128. (error (e) (log:error e)))))
  129. (defun handle-cmd-akb (chat-id message-id args)
  130. (log:info "handle-cmd-akb" chat-id message-id args)
  131. (handler-case
  132. (progn
  133. (let ((total-aneks
  134. (aget "count" (vk-wall-get :domain +akb-vk-domain+ :count 1 :offset 10000000))))
  135. (dolist (post (aget "items" (vk-wall-get :domain +akb-vk-domain+
  136. :count (or (ignore-errors (parse-integer (car args))) 1)
  137. :offset (random total-aneks))))
  138. (handler-case
  139. (telegram-send-message chat-id
  140. (format-akb post)
  141. :disable-web-preview 1)
  142. (error (e) (log:error e))))))
  143. (error (e)
  144. (log:error e)
  145. (telegram-send-message chat-id "Ошибочка вышла"))))
  146. ;; Finance
  147. (defun handle-cmd-rates (chat-id message-id args)
  148. (log:info "handle-cmd-rates" chat-id message-id args)
  149. (let ((rates (get-rates)))
  150. (telegram-send-message chat-id
  151. (format nil "Зеленый ~A, гейро ~A, британец ~A" (cdar rates) (cdadr rates) (cdaddr rates)))))
  152. ;; Weather
  153. (defun handle-cmd-weather (chat-id message-id args)
  154. (log:info "handle-cmd-weather" chat-id message-id args)
  155. (let ((location (cdr (assoc chat-id *chat-locations*))))
  156. (telegram-send-message
  157. chat-id
  158. (if location
  159. (handler-case
  160. (forecast-format (forecast
  161. (aget "latitude" location)
  162. (aget "longitude" location)
  163. :hourly (find "hourly" args :key #'string-downcase :test #'equal)
  164. :daily (find "daily" args :key #'string-downcase :test #'equal)))
  165. (error (e)
  166. (log:error e)
  167. "Ошибочка вышла"))
  168. "Так а ты чьих будешь?"))))
  169. ;; Foursquare
  170. (defvar *fsq-send-to* (make-hash-table)
  171. "Hash of chat-id's to fsq users list to send checkings to")
  172. (defun handle-cmd-post-checkins (chat-id message-id args)
  173. (log:info "handle-cmd-post-checkins" chat-id message-id args)
  174. (let ((users (gethash chat-id *fsq-send-to*)))
  175. (if (null args)
  176. (telegram-send-message chat-id
  177. (if (null users)
  178. "Пока никого не палим"
  179. (format nil "Палим ~{~A~^, ~}" users)))
  180. (progn
  181. (dolist (user args)
  182. (if (member user users :test #'equal)
  183. (progn
  184. (setf users (remove user users :test #'equal))
  185. (telegram-send-message chat-id (format nil "Больше не палим ~A" user)))
  186. (progn
  187. (push user users)
  188. (telegram-send-message chat-id (format nil "Теперь палим ~A" user)))))
  189. (setf (gethash chat-id *fsq-send-to*) users)))))
  190. (defun handle-cmd-checkins (chat-id message-id args)
  191. (log:info "handle-cmd-checkins" chat-id message-id args)
  192. (handler-case
  193. (let ((users (gethash chat-id *fsq-send-to*)))
  194. (when users
  195. (telegram-send-message
  196. chat-id
  197. (format nil "~{~A~^~%~}"
  198. (loop for checkin in (fsq-fetch-checkins)
  199. if (member (aget "id" (aget "user" checkin)) users :test #'equal)
  200. collect (fsq-format-checkin checkin t))))))
  201. (error (e) (log:error e))))
  202. (defvar *fsq-seen-ids* nil "Ids of seen checkins")
  203. (defun process-latest-checkins ()
  204. (handler-case
  205. (dolist (checkin (fsq-fetch-new-checkins))
  206. (let ((id (aget "id" checkin))
  207. (user (aget "id" (aget "user" checkin))))
  208. (unless (find id *fsq-seen-ids* :test #'equal)
  209. (loop for chat-id being the hash-keys in *fsq-send-to* using (hash-value users)
  210. do (when (member user users :test #'equal)
  211. (telegram-send-message chat-id (fsq-format-checkin checkin))))
  212. (push id *fsq-seen-ids*))))
  213. (error (e) (log:error e))))
  214. (defun start ()
  215. (mapc #'trivial-timers:unschedule-timer (trivial-timers:list-all-timers))
  216. (let ((old-updates (find "process-updates"
  217. (bordeaux-threads:all-threads)
  218. :key #'bordeaux-threads:thread-name
  219. :test #'equal)))
  220. (when old-updates
  221. (bordeaux-threads:destroy-thread old-updates)))
  222. (clon:schedule-function
  223. (lambda () (process-latest-akb))
  224. (clon:make-scheduler
  225. (clon:make-typed-cron-schedule :minute '* :hour '*)
  226. :allow-now-p t)
  227. :thread t)
  228. (clon:schedule-function
  229. (lambda () (process-latest-checkins))
  230. (clon:make-scheduler
  231. (clon:make-typed-cron-schedule :minute '* :hour '*)
  232. :allow-now-p t)
  233. :thread t)
  234. (bordeaux-threads:make-thread
  235. (lambda ()
  236. (in-package #:chatikbot)
  237. (loop-with-error-backoff #'process-updates))
  238. :name "process-updates"))