chatikbot.lisp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. (case (car response)
  21. (:text (telegram-send-message chat-id (cdr response) :reply-to reply-id))
  22. (:sticker (telegram-send-sticker chat-id (cdr response) :reply-to reply-id)))
  23. (telegram-send-message chat-id response :reply-to reply-id)))
  24. (defun send-dont-understand (chat-id &optional text reply-id)
  25. (let ((resp (eliza text)))
  26. (log:info text resp)
  27. (when resp
  28. (send-response chat-id resp reply-id))))
  29. (defvar *chat-locations* nil "ALIST of chat->location")
  30. (defun preprocess-input (text)
  31. (when text
  32. (let ((first-word (subseq text 0 (position #\Space text))))
  33. (if (equal first-word "@chatikbot")
  34. (preprocess-input (subseq text 11))
  35. (replace-all text "@chatikbot" "ты")))))
  36. (defun handle-message (message)
  37. (let ((id (aget "message_id" message))
  38. (chat-id (aget "id" (aget "chat" message)))
  39. (text (aget "text" message))
  40. (location (aget "location" message))
  41. (sticker (aget "file_id" (aget "sticker" message))))
  42. (log:info "handle-message" message)
  43. (when text
  44. (if (equal #\/ (char text 0))
  45. (let ((cmd (intern (string-upcase
  46. (subseq text 1 (position #\Space text)))
  47. "KEYWORD"))
  48. (args (when (position #\Space text)
  49. (split-sequence:split-sequence
  50. #\Space (subseq text (1+ (position #\Space text)))))))
  51. (case cmd
  52. (:postakb (handle-cmd-post-akb chat-id id args))
  53. (:akb (handle-cmd-akb chat-id id args))
  54. (:weather (handle-cmd-weather chat-id id args))
  55. (:hourly (handle-cmd-weather chat-id id '("hourly")))
  56. (:daily (handle-cmd-weather chat-id id '("daily")))
  57. (:rates (handle-cmd-rates chat-id id args))
  58. (otherwise (handle-admin-cmd chat-id text cmd args))))
  59. (send-dont-understand chat-id (preprocess-input text))))
  60. (when location
  61. (push (cons chat-id location) *chat-locations*)
  62. (telegram-send-message chat-id "Взял на карандаш"))
  63. (when sticker
  64. (send-dont-understand chat-id))))
  65. (defmacro handling-errors (&body body)
  66. `(handler-case (progn ,@body)
  67. (simple-condition (err)
  68. (format *error-output* "~&~A: ~%" (class-name (class-of err)))
  69. (apply (function format) *error-output*
  70. (simple-condition-format-control err)
  71. (simple-condition-format-arguments err))
  72. (format *error-output* "~&"))
  73. (condition (err)
  74. (format *error-output* "~&~A: ~% ~S~%"
  75. (class-name (class-of err)) err))))
  76. (defun rep (input)
  77. (when input
  78. (with-output-to-string (*standard-output*)
  79. (let ((*package* (find-package 'chatikbot))
  80. (*error-output* *standard-output*))
  81. (handling-errors
  82. (format t "~{~S~^ ;~% ~}~%"
  83. (multiple-value-list (eval (read-from-string input)))))))))
  84. (defun handle-admin-cmd (chat-id text cmd args)
  85. (if (find chat-id *admins*)
  86. (case cmd
  87. (:eval (telegram-send-message chat-id (rep (format nil "~{~A~^ ~}" args))))
  88. (otherwise (send-dont-understand chat-id text)))
  89. (send-dont-understand chat-id text)))
  90. (defparameter +akb-vk-domain+ "baneks" "VK.com username of 'B-category anekdotes'")
  91. (defvar *akb-send-to* nil "List of chat-id's to send AKBs to")
  92. (defun handle-cmd-post-akb (chat-id message-id args)
  93. (log:info "handle-cmd-post-akb" chat-id message-id args)
  94. (let ((message "Хуярим аники"))
  95. (if (member chat-id *akb-send-to*)
  96. (setf message "Не хуярим больше аники"
  97. *akb-send-to* (set-difference *akb-send-to*
  98. (list chat-id)))
  99. (setf *akb-send-to* (cons chat-id *akb-send-to*)))
  100. (telegram-send-message chat-id message)))
  101. (defvar *akb-max-count* 5 "Max number of tweets to return per run")
  102. (defvar *akb-last-id* 0 "id of last AKB tweet")
  103. (defun format-akb (post)
  104. (let* ((id (aget "id" post))
  105. (url (format nil "https://vk.com/~A?w=wall~A_~A"
  106. +akb-vk-domain+ (aget "from_id" post) id)))
  107. (format nil "~A~%~A" (aget "text" post) url)))
  108. (defun process-latest-akb ()
  109. (handler-case
  110. (dolist (post (reverse (aget "items" (vk-wall-get :domain +akb-vk-domain+
  111. :count *akb-max-count*))))
  112. (let ((id (aget "id" post)))
  113. (when (> id *akb-last-id*)
  114. (send-akb (format-akb post))
  115. (setf *akb-last-id* id))))
  116. (error (e) (log:error e))))
  117. (defun send-akb (text)
  118. (log:info "send-akb: ~A" text)
  119. (dolist (chat-id *akb-send-to*)
  120. (handler-case
  121. (telegram-send-message chat-id text
  122. :disable-web-preview 1)
  123. (error (e) (log:error e)))))
  124. (defun handle-cmd-akb (chat-id message-id args)
  125. (log:info "handle-cmd-akb" chat-id message-id args)
  126. (let ((total-aneks
  127. (aget "count" (vk-wall-get :domain +akb-vk-domain+ :count 1 :offset 10000000))))
  128. (dolist (post (aget "items" (vk-wall-get :domain +akb-vk-domain+
  129. :count (or (ignore-errors (parse-integer (car args))) 1)
  130. :offset (random total-aneks))))
  131. (handler-case
  132. (telegram-send-message chat-id
  133. (format-akb post)
  134. :disable-web-preview 1)
  135. (error (e) (log:error e))))))
  136. (defun handle-cmd-rates (chat-id message-id args)
  137. (log:info "handle-cmd-rates" chat-id message-id args)
  138. (let ((rates (get-rates)))
  139. (telegram-send-message chat-id
  140. (format nil "Зеленый ~A, гейро ~A" (cdar rates) (cdadr rates)))))
  141. (defun handle-cmd-weather (chat-id message-id args)
  142. (log:info "handle-cmd-weather" chat-id message-id args)
  143. (let ((location (cdr (assoc chat-id *chat-locations*))))
  144. (telegram-send-message
  145. chat-id
  146. (if location
  147. (forecast-format (forecast
  148. (aget "latitude" location)
  149. (aget "longitude" location)
  150. :hourly (find "hourly" args :key #'string-downcase :test #'equal)
  151. :daily (find "daily" args :key #'string-downcase :test #'equal)))
  152. "Так а ты чьих будешь?"))))
  153. (defun start ()
  154. (mapc #'trivial-timers:unschedule-timer (trivial-timers:list-all-timers))
  155. (let ((old-updates (find "process-updates"
  156. (bordeaux-threads:all-threads)
  157. :key #'bordeaux-threads:thread-name
  158. :test #'equal)))
  159. (when old-updates
  160. (bordeaux-threads:destroy-thread old-updates)))
  161. (clon:schedule-function
  162. (lambda () (process-latest-akb))
  163. (clon:make-scheduler
  164. (clon:make-typed-cron-schedule :minute '* :hour '*)
  165. :allow-now-p t)
  166. :thread t)
  167. (bordeaux-threads:make-thread
  168. (lambda ()
  169. (in-package #:chatikbot)
  170. (loop-with-error-backoff #'process-updates))
  171. :name "process-updates"))