浏览代码

/lastakb command

Innocenty Enikeew 10 年之前
父节点
当前提交
9676db8cb9
共有 1 个文件被更改,包括 31 次插入13 次删除
  1. 31 13
      chatikbot.lisp

+ 31 - 13
chatikbot.lisp

@@ -23,24 +23,33 @@
   (telegram-send-message chat-id "Ну хуууй знает" :reply-to reply-id))
 
 (defun handle-message (message)
-  (let ((id (aget "message-id" message))
+  (let ((id (aget "message_id" message))
         (chat-id (aget "id" (aget "chat" message)))
         (text (aget "text" message)))
-    (log:info "handle-message: ~A" message)
+    (log:info "handle-message" message)
     (when text
       (if (equal #\/ (char text 0))
-          (let ((cmd (intern (string-upcase (subseq text 1)) "KEYWORD")))
+          (let ((cmd (intern (string-upcase
+                              (subseq text 1 (position #\Space text)))
+                             "KEYWORD"))
+                (args (when (position #\Space text)
+                        (split-sequence:split-sequence
+                         #\Space (subseq text (1+ (position #\Space text)))))))
             (case cmd
-              (:akb (toggle-akb chat-id id))
+              (:akb (handle-cmd-akb chat-id id args))
+              (:lastakb (handle-cmd-last-akb chat-id id args))
               (otherwise (send-dont-understand chat-id id))))
-          (send-dont-understand chat-id id)))))
+          (telegram-send-message chat-id
+                                 (format nil "Сам ~A"
+                                         (replace-all text "@chatikbot " "")))))))
 
 (defparameter +akb-user-id+ "3021296351" "Twitter user id of 'B-category anecdotes'")
 (defvar *akb-max-count* 5 "Max number of tweets to return per run")
 (defvar *akb-last-id* nil "id of last AKB tweet")
 (defvar *akb-send-to* nil "List of chat-id's to send AKBs to")
 
-(defun toggle-akb (chat-id message-id)
+(defun handle-cmd-akb (chat-id message-id args)
+  (log:info "handle-cmd-akb" chat-id message-id args)
   (let ((message "Хуярим аники"))
     (if (member chat-id *akb-send-to*)
         (setf message "Не хуярим больше аники"
@@ -50,27 +59,36 @@
     (telegram-send-message chat-id message :reply-to message-id)))
 
 (defun process-latest-akb ()
+  (log:info "Getting latest AKBs")
   (loop for (id . text) in (get-tweets +akb-user-id+
                                        :since-id *akb-last-id*
                                        :count *akb-max-count*)
-     do (handle-akb text)
+     do (send-akb text)
      do (setf *akb-last-id* (max (or *akb-last-id* 0) id))))
 
-(defun handle-akb (text)
-  (log:info "handle-akb: ~A" text)
+(defun send-akb (text)
+  (log:info "send-akb: ~A" text)
   (loop for chat-id in *akb-send-to*
      do (telegram-send-message chat-id
-                               (replace-all text " / " (coerce '(#\Newline) 'string)))))
+                               (replace-all text " / " (coerce '(#\Newline) 'string))
+                               :disable-web-preview 1)))
+
+(defun handle-cmd-last-akb (chat-id message-id args)
+  (log:info "handle-cmd-last-akb" chat-id message-id args)
+  (loop for (id . text) in (get-tweets +akb-user-id+
+                                       :count (or (car args) 1))
+     do (telegram-send-message chat-id
+                               (replace-all text " / " (coerce '(#\Newline) 'string))
+                               :disable-web-preview 1)))
 
 
 (defvar *crons* (list
-                 (list #'process-latest-akb '(:minute (member 0 5 10 15 20 25 30 35 40 45 50 55)))
-                 )
+                 (list #'process-latest-akb '(:minute * :hour *)))
   "List of cron functions with their schedules")
 (defvar *cron-timers* nil)
 
-
 (defun start ()
+  (mapc #'sb-ext:unschedule-timer *cron-timers*)
   (setf *cron-timers*
         (loop for (function schedule) in *crons*
            do (log:info "Starting cron" function schedule)