fixed deconding of vectors, thanks cage2

b6cee53819
This commit is contained in:
Rei 2017-07-10 20:56:04 +01:00 committed by GitHub
parent 48732cbd3f
commit 3ccf8b8045

View file

@ -24,6 +24,8 @@
(in-package :cl-telegram-bot) (in-package :cl-telegram-bot)
(define-constant +http-ok+ 200 :test #'=)
(defclass bot () (defclass bot ()
((id ((id
:documentation "Update id" :documentation "Update id"
@ -34,15 +36,27 @@
:documentation "Bot token given by BotFather" :documentation "Bot token given by BotFather"
:accessor token :accessor token
:initform nil) :initform nil)
(api-uri
:initarg :api-uri
:initform "https://api.telegram.org/"
:accessor api-uri)
(endpoint (endpoint
:initarg :endpoint :initarg :endpoint
:accessor endpoint :accessor endpoint
:documentation "HTTPS endpoint" :documentation "HTTPS endpoint")
(file-endpoint
:initarg :file-endpoint
:accessor file-endpoint
:documentation "HTTPS file-endpoint"
:initform nil))) :initform nil)))
(defmethod initialize-instance :after ((b bot) &key (api-uri "https://api.telegram.org/bot")) (defmethod initialize-instance :after ((object bot) &key &allow-other-keys)
(setf (endpoint b) (with-accessors ((token token)
(concatenate 'string api-uri (token b) "/"))) (endpoint endpoint)
(file-endpoint file-endpoint)
(api-uri api-uri)) object
(setf endpoint (concatenate 'string api-uri "bot" token "/")
file-endpoint (concatenate 'string api-uri "file/" "bot" token "/"))))
(defun make-bot (token) (defun make-bot (token)
"Create a new bot instance. Takes a token string." "Create a new bot instance. Takes a token string."
@ -97,6 +111,9 @@
(with-input-from-string (stream object) (with-input-from-string (stream object)
(json:decode-json stream)))) (json:decode-json stream))))
(defmethod decode ((object vector))
(decode (map 'string #'code-char object)))
(define-condition request-error (error) (define-condition request-error (error)
((what :initarg :what :reader what))) ((what :initarg :what :reader what)))
@ -108,6 +125,12 @@
; Telegram API methods, see https://core.telegram.org/bots/api ; Telegram API methods, see https://core.telegram.org/bots/api
(defmacro with-ok-results ((unserialized results) &body body)
`(let ((,results (slot-value ,unserialized (find-json-symbol :result))))
(if (slot-value ,unserialized (find-json-symbol :ok))
(progn ,@body)
nil)))
(defun get-updates (b &key limit timeout) (defun get-updates (b &key limit timeout)
"https://core.telegram.org/bots/api#getupdates" "https://core.telegram.org/bots/api#getupdates"
(let* ((current-id (id b)) (let* ((current-id (id b))
@ -296,6 +319,19 @@
(cons :file_id file-id)))) (cons :file_id file-id))))
(make-request b "getFile" options))) (make-request b "getFile" options)))
(defun download-file (b file-id)
"Perform HTTP request to 'name API method with 'options JSON-encoded object."
(with-package :cl-telegram-bot
(let* ((file-spec (decode (get-file b file-id))))
(with-ok-results (file-spec results)
(when-let* ((path (access results 'file--path))
(uri (concatenate 'string (file-endpoint b) path))
(extension (cl-ppcre:scan-to-strings "\\..*$" path)))
(multiple-value-bind (body code headers)
(drakma:http-request uri :method :get)
(when (= code +http-ok+)
(values body headers extension))))))))
(defun kick-chat-member (b chat-id user-id) (defun kick-chat-member (b chat-id user-id)
"https://core.telegram.org/bots/api#kickchatmember" "https://core.telegram.org/bots/api#kickchatmember"
(let ((options (let ((options