chatikbot.lisp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. (defun process-updates ()
  10. (handler-case
  11. (loop for update in (telegram-get-updates :offset (and *telegram-last-update*
  12. (1+ *telegram-last-update*))
  13. :timeout 300)
  14. do (handle-message (aget "message" update))
  15. do (setf *telegram-last-update*
  16. (max (or *telegram-last-update* 0)
  17. (aget "update_id" update))))
  18. (error (e)
  19. (log:error e))))
  20. (defvar *responses*
  21. '("И чё?" "Сам-то понял?" "Ну хуй знает" "Бля..." "В душе не ебу" "Мне похуй"
  22. "Eбаный ты нахуй" "Отъебись")
  23. "Unknown command respond strings")
  24. (defun random-choice (messages)
  25. (nth (random (length messages)) messages))
  26. (defun send-dont-understand (chat-id &optional text reply-id)
  27. (telegram-send-message chat-id
  28. (if (and text (zerop (random 5)))
  29. (format nil "Сам ~A"
  30. (replace-all
  31. (if (equal (char text 0) #\/)
  32. (subseq text 1)
  33. text)
  34. "@chatikbot" ""))
  35. (random-choice *responses*))
  36. :reply-to reply-id))
  37. (defvar *chat-locations* nil "ALIST of chat->location")
  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. (log:info "handle-message" message)
  44. (when text
  45. (if (equal #\/ (char text 0))
  46. (let ((cmd (intern (string-upcase
  47. (subseq text 1 (position #\Space text)))
  48. "KEYWORD"))
  49. (args (when (position #\Space text)
  50. (split-sequence:split-sequence
  51. #\Space (subseq text (1+ (position #\Space text)))))))
  52. (case cmd
  53. (:postakb (handle-cmd-post-akb chat-id id args))
  54. (:akb (handle-cmd-akb chat-id id args))
  55. (:weather (handle-cmd-weather chat-id id args))
  56. (:help (handle-cmd-help chat-id id args))
  57. (otherwise (send-dont-understand chat-id text))))
  58. (send-dont-understand chat-id text)))
  59. (when location
  60. (push (cons chat-id location) *chat-locations*)
  61. (telegram-send-message chat-id "Взял на карандаш"))))
  62. (defparameter +akb-vk-domain+ "baneks" "VK.com username of 'B-category anekdotes'")
  63. (defvar *akb-send-to* nil "List of chat-id's to send AKBs to")
  64. (defun handle-cmd-post-akb (chat-id message-id args)
  65. (log:info "handle-cmd-post-akb" chat-id message-id args)
  66. (let ((message "Хуярим аники"))
  67. (if (member chat-id *akb-send-to*)
  68. (setf message "Не хуярим больше аники"
  69. *akb-send-to* (set-difference *akb-send-to*
  70. (list chat-id)))
  71. (setf *akb-send-to* (cons chat-id *akb-send-to*)))
  72. (telegram-send-message chat-id message)))
  73. (defvar *akb-max-count* 5 "Max number of tweets to return per run")
  74. (defvar *akb-last-id* 0 "id of last AKB tweet")
  75. (defun format-akb (post)
  76. (let* ((id (aget "id" post))
  77. (url (format nil "https://vk.com/~A?w=wall~A_~A"
  78. +akb-vk-domain+ (aget "from_id" post) id)))
  79. (format nil "~A~%~A" (aget "text" post) url)))
  80. (defun process-latest-akb ()
  81. (log:info "Getting latest AKBs")
  82. (handler-case
  83. (dolist (post (reverse (aget "items" (vk-wall-get :domain +akb-vk-domain+
  84. :count *akb-max-count*))))
  85. (let ((id (aget "id" post)))
  86. (when (> id *akb-last-id*)
  87. (send-akb (format-akb post))
  88. (setf *akb-last-id* id))))
  89. (error (e) (log:error e))))
  90. (defun send-akb (text)
  91. (log:info "send-akb: ~A" text)
  92. (dolist (chat-id *akb-send-to*)
  93. (handler-case
  94. (telegram-send-message chat-id text
  95. :disable-web-preview 1)
  96. (error (e) (log:error e)))))
  97. (defun handle-cmd-akb (chat-id message-id args)
  98. (log:info "handle-cmd-akb" chat-id message-id args)
  99. (let ((total-aneks
  100. (aget "count" (vk-wall-get :domain +akb-vk-domain+ :count 1 :offset 10000000))))
  101. (dolist (post (aget "items" (vk-wall-get :domain +akb-vk-domain+
  102. :count (or (ignore-errors (parse-integer (car args))) 1)
  103. :offset (random total-aneks))))
  104. (handler-case
  105. (telegram-send-message chat-id
  106. (format-akb post)
  107. :disable-web-preview 1)
  108. (error (e) (log:error e))))))
  109. (defvar *help-responses*
  110. (list "Сам себе помоги, нахуй!" "Вот заняться мне больше нечем" "Нахуй пошел!"
  111. "Хэлп, ай нид самбади, хелп нот джаст энибади" "Отъебись"))
  112. (defun handle-cmd-help (chat-id message-id args)
  113. (log:info "handle-cmd-help" chat-id message-id args)
  114. (telegram-send-message chat-id (random-choice *help-responses*)))
  115. (defun handle-cmd-weather (chat-id message-id args)
  116. (log:info "handle-cmd-weather" chat-id message-id args)
  117. (let ((location (cdr (assoc chat-id *chat-locations*))))
  118. (telegram-send-message
  119. chat-id
  120. (if location
  121. (forecast-format (forecast
  122. (aget "latitude" location)
  123. (aget "longitude" location)
  124. :hourly (find "hourly" args :key #'string-downcase :test #'equal)
  125. :daily (find "daily" args :key #'string-downcase :test #'equal)))
  126. "Так а ты чьих будешь?"))))
  127. (defun start ()
  128. (mapc #'sb-ext:unschedule-timer (trivial-timers:list-all-timers))
  129. (let ((old-updates (find "process-updates"
  130. (bordeaux-threads:all-threads)
  131. :key #'bordeaux-threads:thread-name
  132. :test #'equal)))
  133. (when old-updates
  134. (bordeaux-threads:destroy-thread old-updates)))
  135. (clon:schedule-function
  136. (lambda () (process-latest-akb))
  137. (clon:make-scheduler
  138. (clon:make-typed-cron-schedule :minute '* :hour '*)
  139. :allow-now-p t)
  140. :thread t)
  141. (bordeaux-threads:make-thread
  142. (lambda ()
  143. (loop
  144. do (process-updates)))
  145. :name "process-updates"))