chatikbot.lisp 6.6 KB

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