Procházet zdrojové kódy

[dex] ignore 'forbidden' errors

Innocenty Enikeew před 8 roky
rodič
revize
2190839487
2 změnil soubory, kde provedl 23 přidání a 18 odebrání
  1. 4 2
      chatikbot.lisp
  2. 19 16
      telegram.lisp

+ 4 - 2
chatikbot.lisp

@@ -1,6 +1,7 @@
 (in-package #:chatikbot)
 
 (defvar *admins* nil "Admins chat-ids")
+(defvar *bot-user-id* nil "Bot user_id")
 
 ;; Load config file
 (alexandria:when-let (file (probe-file
@@ -42,7 +43,7 @@
         (reply-to (aget "id" (aget "from" (aget "reply_to_message" (aget "message" update))))))
     (if (> update-id *telegram-last-update*)
         (progn
-          (if (and reply-to (not (equal reply-to (parse-integer *telegram-token* :end (position #\: *telegram-token*)))))
+          (if (and reply-to (not (equal reply-to *bot-user-id*)))
               (log:info "Reply not to bot")
               (loop for (key . value) in update
                  unless (equal "update_id" key)
@@ -88,7 +89,8 @@
 
 (defun start ()
   ;; Test telegram token
-  (setf *bot-name* (concatenate 'string "@" (aget "username" (telegram-get-me))))
+  (setf *bot-name* (concatenate 'string "@" (aget "username" (telegram-get-me)))
+        *bot-user-id* (parse-integer *telegram-token* :end (position #\: *telegram-token*)))
 
   (cleanup)
 

+ 19 - 16
telegram.lisp

@@ -7,22 +7,25 @@
 (defvar *telegram-timeout* 30 "Default Telegram timeout")
 
 (defun %telegram-api-call (method &optional args)
-  (let* ((params (loop for (k . v) in args when v
-                    collect (cons
-                             (princ-to-string k)
-                             (if (pathnamep v) v
-                                 (princ-to-string v)))))
-         (timeout (+ 5 (or (aget "timeout" args)
-                           *telegram-timeout*)))
-         (response
-           (json-request (format nil +telegram-api-format+
-                                 *telegram-token* method)
-                         :method :post
-                         :content params
-                         :timeout timeout)))
-    (unless (aget "ok" response)
-      (error (aget "description" response)))
-    (aget "result" response)))
+  (handler-case
+      (let* ((params (loop for (k . v) in args when v
+                        collect (cons
+                                 (princ-to-string k)
+                                 (if (pathnamep v) v
+                                     (princ-to-string v)))))
+             (timeout (+ 5 (or (aget "timeout" args)
+                               *telegram-timeout*)))
+             (response
+              (json-request (format nil +telegram-api-format+
+                                    *telegram-token* method)
+                            :method :post
+                            :content params
+                            :timeout timeout)))
+        (unless (aget "ok" response)
+          (error (aget "description" response)))
+        (aget "result" response))
+    (dex:http-request-forbidden (e)
+      (log:info "Forbidden" e))))
 
 (defun telegram-get-updates (&key offset limit timeout)
   (%telegram-api-call