1
0

chatikbot.lisp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. (:hourly (handle-cmd-weather chat-id id '("hourly")))
  57. (:daily (handle-cmd-weather chat-id id '("daily")))
  58. (:help (handle-cmd-help chat-id id args))
  59. (otherwise (send-dont-understand chat-id text))))
  60. (send-dont-understand chat-id text)))
  61. (when location
  62. (push (cons chat-id location) *chat-locations*)
  63. (telegram-send-message chat-id "Взял на карандаш"))))
  64. (defparameter +akb-vk-domain+ "baneks" "VK.com username of 'B-category anekdotes'")
  65. (defvar *akb-send-to* nil "List of chat-id's to send AKBs to")
  66. (defun handle-cmd-post-akb (chat-id message-id args)
  67. (log:info "handle-cmd-post-akb" chat-id message-id args)
  68. (let ((message "Хуярим аники"))
  69. (if (member chat-id *akb-send-to*)
  70. (setf message "Не хуярим больше аники"
  71. *akb-send-to* (set-difference *akb-send-to*
  72. (list chat-id)))
  73. (setf *akb-send-to* (cons chat-id *akb-send-to*)))
  74. (telegram-send-message chat-id message)))
  75. (defvar *akb-max-count* 5 "Max number of tweets to return per run")
  76. (defvar *akb-last-id* 0 "id of last AKB tweet")
  77. (defun format-akb (post)
  78. (let* ((id (aget "id" post))
  79. (url (format nil "https://vk.com/~A?w=wall~A_~A"
  80. +akb-vk-domain+ (aget "from_id" post) id)))
  81. (format nil "~A~%~A" (aget "text" post) url)))
  82. (defun process-latest-akb ()
  83. (log:info "Getting latest AKBs")
  84. (handler-case
  85. (dolist (post (reverse (aget "items" (vk-wall-get :domain +akb-vk-domain+
  86. :count *akb-max-count*))))
  87. (let ((id (aget "id" post)))
  88. (when (> id *akb-last-id*)
  89. (send-akb (format-akb post))
  90. (setf *akb-last-id* id))))
  91. (error (e) (log:error e))))
  92. (defun send-akb (text)
  93. (log:info "send-akb: ~A" text)
  94. (dolist (chat-id *akb-send-to*)
  95. (handler-case
  96. (telegram-send-message chat-id text
  97. :disable-web-preview 1)
  98. (error (e) (log:error e)))))
  99. (defun handle-cmd-akb (chat-id message-id args)
  100. (log:info "handle-cmd-akb" chat-id message-id args)
  101. (let ((total-aneks
  102. (aget "count" (vk-wall-get :domain +akb-vk-domain+ :count 1 :offset 10000000))))
  103. (dolist (post (aget "items" (vk-wall-get :domain +akb-vk-domain+
  104. :count (or (ignore-errors (parse-integer (car args))) 1)
  105. :offset (random total-aneks))))
  106. (handler-case
  107. (telegram-send-message chat-id
  108. (format-akb post)
  109. :disable-web-preview 1)
  110. (error (e) (log:error e))))))
  111. (defvar *help-responses*
  112. (list "Сам себе помоги, нахуй!" "Вот заняться мне больше нечем" "Нахуй пошел!"
  113. "Хэлп, ай нид самбади, хелп нот джаст энибади" "Отъебись"))
  114. (defun handle-cmd-help (chat-id message-id args)
  115. (log:info "handle-cmd-help" chat-id message-id args)
  116. (telegram-send-message chat-id (random-choice *help-responses*)))
  117. (defun handle-cmd-weather (chat-id message-id args)
  118. (log:info "handle-cmd-weather" chat-id message-id args)
  119. (let ((location (cdr (assoc chat-id *chat-locations*))))
  120. (telegram-send-message
  121. chat-id
  122. (if location
  123. (forecast-format (forecast
  124. (aget "latitude" location)
  125. (aget "longitude" location)
  126. :hourly (find "hourly" args :key #'string-downcase :test #'equal)
  127. :daily (find "daily" args :key #'string-downcase :test #'equal)))
  128. "Так а ты чьих будешь?"))))
  129. (defun start ()
  130. (mapc #'sb-ext:unschedule-timer (trivial-timers:list-all-timers))
  131. (let ((old-updates (find "process-updates"
  132. (bordeaux-threads:all-threads)
  133. :key #'bordeaux-threads:thread-name
  134. :test #'equal)))
  135. (when old-updates
  136. (bordeaux-threads:destroy-thread old-updates)))
  137. (clon:schedule-function
  138. (lambda () (process-latest-akb))
  139. (clon:make-scheduler
  140. (clon:make-typed-cron-schedule :minute '* :hour '*)
  141. :allow-now-p t)
  142. :thread t)
  143. (bordeaux-threads:make-thread
  144. (lambda ()
  145. (loop
  146. do (process-updates)))
  147. :name "process-updates"))