Explorar o código

Fsq friends command

Innocenty Enikeew %!s(int64=10) %!d(string=hai) anos
pai
achega
00dcbce774
Modificáronse 2 ficheiros con 63 adicións e 24 borrados
  1. 62 23
      chatikbot.lisp
  2. 1 1
      foursquare.lisp

+ 62 - 23
chatikbot.lisp

@@ -68,6 +68,7 @@
               (:daily (handle-cmd-weather chat-id id '("daily")))
               (:rates (handle-cmd-rates chat-id id args))
               (:postcheckins (handle-cmd-post-checkins chat-id id args))
+              (:friends (handle-cmd-fsq-friends chat-id id args))
               (:checkins (handle-cmd-checkins chat-id id args))
               (otherwise (handle-admin-cmd chat-id text cmd args))))
           (send-dont-understand chat-id (preprocess-input text))))
@@ -195,36 +196,74 @@
 (defvar *fsq-send-to* (make-hash-table)
   "Hash of chat-id's to fsq users list to send checkings to")
 
+(defun fsq-user-name (user)
+  (when user
+    (format nil "~@[~A~]~@[ ~A~]"
+            (aget "firstName" user)
+            (aget "lastName" user))))
+
 (defun handle-cmd-post-checkins (chat-id message-id args)
   (log:info "handle-cmd-post-checkins" chat-id message-id args)
-  (let ((users (gethash chat-id *fsq-send-to*)))
-    (if (null args)
-        (telegram-send-message chat-id
-                               (if (null users)
-                                   "Пока никого не палим"
-                                   (format nil "Палим ~{~A~^, ~}" users)))
-        (progn
-          (dolist (user args)
-            (if (member user users :test #'equal)
-                (progn
-                  (setf users (remove user users :test #'equal))
-                  (telegram-send-message chat-id (format nil "Больше не палим ~A" user)))
-                (progn
-                  (push user users)
-                  (telegram-send-message chat-id (format nil "Теперь палим ~A" user)))))
-          (setf (gethash chat-id *fsq-send-to*) users)))))
+  (handler-case
+      (let ((users (gethash chat-id *fsq-send-to*))
+            (friends (fsq-fetch-friends)))
+        (if (null args)
+            (telegram-send-message chat-id
+                                   (if (null users)
+                                       "Пока никого не палим"
+                                       (format nil "Палим ~{~A~^, ~}"
+                                               (loop for user in friends
+                                                  when (member (aget "id" user)
+                                                               users :test #'equal)
+                                                  collect (fsq-user-name user)))))
+            (progn
+              (dolist (user args)
+                (let ((username (fsq-user-name
+                                 (find user friends
+                                       :test #'equal
+                                       :key #'(lambda (f) (aget "id" f))))))
+                  (when username
+                    (if (member user users :test #'equal)
+                        (progn
+                          (setf users (remove user users :test #'equal))
+                          (telegram-send-message chat-id
+                                                 (format nil "Больше не палим ~A" username)))
+                        (progn
+                          (push user users)
+                          (telegram-send-message chat-id (format nil "Теперь палим ~A" username)))))))
+              (setf (gethash chat-id *fsq-send-to*) users))))
+    (error (e)
+      (log:error e)
+      (telegram-send-message chat-id "Ошибочка вышла"))))
+
+(defun handle-cmd-fsq-friends (chat-id message-id args)
+  (log:info "handle-cmd-fsq-friends" chat-id message-id args)
+  (handler-case
+      (let ((users (gethash chat-id *fsq-send-to*))
+            (friends (fsq-fetch-friends)))
+        (telegram-send-message
+         chat-id
+         (format
+          nil "~{~A: ~:[~;📍 ~]~A~^~%~}"
+          (loop for user in friends
+             append (list
+                     (aget "id" user)
+                     (member (aget "id" user) users :test #'equal)
+                     (fsq-user-name user))))))
+    (error (e) (log:error e))))
+
 
 (defun handle-cmd-checkins (chat-id message-id args)
   (log:info "handle-cmd-checkins" chat-id message-id args)
   (handler-case
       (let ((users (gethash chat-id *fsq-send-to*)))
-	(when users
-	  (telegram-send-message
-	   chat-id
-	   (format nil "~{~A~^~%~}"
-		   (loop for checkin in (fsq-fetch-checkins)
-		      if (member (aget "id" (aget "user" checkin)) users :test #'equal)
-		      collect (fsq-format-checkin checkin t))))))
+        (when users
+          (telegram-send-message
+           chat-id
+           (format nil "~{~A~^~%~}"
+                   (loop for checkin in (fsq-fetch-checkins)
+                      if (member (aget "id" (aget "user" checkin)) users :test #'equal)
+                      collect (fsq-format-checkin checkin t))))))
     (error (e) (log:error e))))
 
 

+ 1 - 1
foursquare.lisp

@@ -43,7 +43,7 @@
       (setf *fsq-last-timestamp* (princ-to-string (1+ (aget "createdAt" (first recent))))))
     recent))
 
-(defun fsq-friends (&optional offset)
+(defun fsq-fetch-friends (&optional offset)
   (aget "items"
         (aget "friends"
               (%fsq-api-call "users/self/friends"