1
0

chatikbot.lisp 9.8 KB

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