2017-08-07 15:30:38 +01:00
|
|
|
;; cl-telegram-bot
|
|
|
|
;;
|
|
|
|
;; MIT License
|
|
|
|
;;
|
|
|
|
;; Copyright (c) 2016 Rei <https://github.com/sovietspaceship>
|
|
|
|
;;
|
|
|
|
;; Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
;; of this software and associated documentation files (the "Software"), to deal
|
|
|
|
;; in the Software without restriction, including without limitation the rights
|
|
|
|
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
;; copies of the Software, and to permit persons to whom the Software is
|
|
|
|
;; furnished to do so, subject to the following conditions:
|
|
|
|
;;
|
|
|
|
;; The above copyright notice and this permission notice shall be included in all
|
|
|
|
;; copies or substantial portions of the Software.
|
|
|
|
;;
|
|
|
|
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
;; SOFTWARE.
|
2016-08-14 04:35:03 +02:00
|
|
|
|
2016-08-17 19:55:19 +02:00
|
|
|
(in-package :cl-telegram-bot)
|
2016-08-14 04:35:03 +02:00
|
|
|
|
2017-11-18 10:52:37 +02:00
|
|
|
(alexandria:define-constant +http-ok+ 200 :test #'=)
|
2017-07-10 20:56:04 +01:00
|
|
|
|
2016-08-14 04:35:03 +02:00
|
|
|
(defclass bot ()
|
|
|
|
((id
|
|
|
|
:documentation "Update id"
|
2016-08-14 13:18:43 +03:00
|
|
|
:initform 0
|
|
|
|
:accessor id)
|
2016-08-14 04:35:03 +02:00
|
|
|
(token
|
|
|
|
:initarg :token
|
|
|
|
:documentation "Bot token given by BotFather"
|
2016-08-17 19:57:24 +02:00
|
|
|
:accessor token
|
2016-08-14 13:18:43 +03:00
|
|
|
:initform nil)
|
2017-07-10 20:56:04 +01:00
|
|
|
(api-uri
|
|
|
|
:initarg :api-uri
|
|
|
|
:initform "https://api.telegram.org/"
|
|
|
|
:accessor api-uri)
|
2016-08-14 04:35:03 +02:00
|
|
|
(endpoint
|
|
|
|
:initarg :endpoint
|
2016-08-14 13:18:43 +03:00
|
|
|
:accessor endpoint
|
2017-07-10 20:56:04 +01:00
|
|
|
:documentation "HTTPS endpoint")
|
|
|
|
(file-endpoint
|
|
|
|
:initarg :file-endpoint
|
|
|
|
:accessor file-endpoint
|
|
|
|
:documentation "HTTPS file-endpoint"
|
2016-08-14 13:18:43 +03:00
|
|
|
:initform nil)))
|
2016-08-14 04:35:03 +02:00
|
|
|
|
2017-07-10 20:56:04 +01:00
|
|
|
(defmethod initialize-instance :after ((object bot) &key &allow-other-keys)
|
|
|
|
(with-accessors ((token token)
|
|
|
|
(endpoint endpoint)
|
|
|
|
(file-endpoint file-endpoint)
|
|
|
|
(api-uri api-uri)) object
|
2017-08-07 15:30:38 +01:00
|
|
|
(setf endpoint (concatenate 'string api-uri "bot" token "/")
|
|
|
|
file-endpoint (concatenate 'string api-uri "file/" "bot" token "/"))))
|
2016-08-14 04:35:03 +02:00
|
|
|
|
|
|
|
(defun make-bot (token)
|
|
|
|
"Create a new bot instance. Takes a token string."
|
|
|
|
(make-instance 'bot :token token))
|
|
|
|
|
|
|
|
#+sbcl
|
|
|
|
(defun get-class-slots (obj)
|
|
|
|
"Get a list of class slots, useful to inspect Fluid classes. SBCL only."
|
|
|
|
(mapcar #'sb-mop:slot-definition-name
|
|
|
|
(sb-mop:class-slots
|
|
|
|
(class-of obj))))
|
|
|
|
|
2019-08-16 17:35:34 +02:00
|
|
|
(defun recursive-change-class (object top-level-class)
|
|
|
|
"Casts and object and its members into the telegram specific classes."
|
|
|
|
(when (arrayp object)
|
|
|
|
(return-from recursive-change-class
|
|
|
|
(map 'vector #'(lambda (value)
|
|
|
|
(recursive-change-class value top-level-class))
|
|
|
|
object)))
|
|
|
|
|
|
|
|
(change-class object top-level-class)
|
|
|
|
(dolist (slot (c2mop:class-direct-slots (find-class top-level-class)))
|
|
|
|
(let* ((name (c2mop:slot-definition-name slot))
|
|
|
|
(type (c2mop:slot-definition-type slot)))
|
|
|
|
(when (slot-boundp object name)
|
|
|
|
(let ((value (slot-value object name)))
|
|
|
|
(when (and value (find type *api-types*))
|
|
|
|
(recursive-change-class value type))))))
|
|
|
|
object)
|
|
|
|
|
|
|
|
(defun make-request (b name options &key (streamp nil) (return-type nil))
|
2016-08-14 04:35:03 +02:00
|
|
|
"Perform HTTP request to 'name API method with 'options JSON-encoded object."
|
2017-11-25 08:55:45 +02:00
|
|
|
(let* ((results (multiple-value-list
|
|
|
|
(drakma:http-request
|
|
|
|
(concatenate 'string (endpoint b) name)
|
|
|
|
:method :post
|
|
|
|
:want-stream streamp
|
|
|
|
:content-type "application/json"
|
|
|
|
:content (json:encode-json-alist-to-string options))))
|
|
|
|
(status (cadr results))
|
2019-08-16 17:35:34 +02:00
|
|
|
(reason (car (last results)))
|
|
|
|
(message (nth 0 results)))
|
|
|
|
;; (when (<= 400 status 599)
|
|
|
|
;; (error 'request-error :what (format nil "request to ~A returned ~A (~A)" name status reason)))
|
|
|
|
(with-slots (ok result description) (decode message)
|
|
|
|
(if ok
|
|
|
|
(if return-type
|
|
|
|
(recursive-change-class result return-type)
|
|
|
|
result)
|
|
|
|
(error 'request-error :what description)
|
|
|
|
))))
|
2016-08-14 04:35:03 +02:00
|
|
|
|
|
|
|
(defun access (update &rest args)
|
|
|
|
"Access update field. update.first.second. ... => (access update 'first 'second ...). Nil if unbound."
|
2016-08-18 04:24:09 +02:00
|
|
|
(unless update
|
2017-08-07 15:30:38 +01:00
|
|
|
(return-from access nil))
|
2016-08-14 04:35:03 +02:00
|
|
|
(let ((current update))
|
|
|
|
(dolist (r args)
|
|
|
|
(unless (slot-boundp current r)
|
|
|
|
(return-from access nil))
|
|
|
|
(setf current (slot-value current r)))
|
|
|
|
current))
|
2016-08-14 13:18:43 +03:00
|
|
|
|
2016-08-14 04:35:03 +02:00
|
|
|
(defun get-slot (update slot)
|
|
|
|
"Access slot. Since fluid classes signal error on unbound slot access, this instead returns nil."
|
|
|
|
(if (slot-boundp update slot)
|
|
|
|
(slot-value update slot)
|
|
|
|
nil))
|
|
|
|
|
2016-08-18 04:24:09 +02:00
|
|
|
(defmacro with-package (package &rest body)
|
2017-08-07 15:30:38 +01:00
|
|
|
`(let ((json:*json-symbols-package* ,package)) ,@body))
|
2016-08-18 04:24:09 +02:00
|
|
|
|
2017-05-17 13:17:58 +02:00
|
|
|
(defgeneric decode (object))
|
|
|
|
|
|
|
|
(defmethod decode ((object stream))
|
|
|
|
(json:with-decoder-simple-clos-semantics
|
2019-08-16 17:35:34 +02:00
|
|
|
(prog1
|
|
|
|
(json:decode-json object)
|
|
|
|
(close object))))
|
2017-05-17 13:17:58 +02:00
|
|
|
|
|
|
|
(defmethod decode ((object string))
|
|
|
|
(json:with-decoder-simple-clos-semantics
|
2017-08-07 15:30:38 +01:00
|
|
|
(with-input-from-string (stream object)
|
|
|
|
(json:decode-json stream))))
|
2016-08-14 04:35:03 +02:00
|
|
|
|
2017-07-10 20:56:04 +01:00
|
|
|
(defmethod decode ((object vector))
|
|
|
|
(decode (map 'string #'code-char object)))
|
|
|
|
|
2016-08-14 04:35:03 +02:00
|
|
|
(define-condition request-error (error)
|
2017-11-25 08:55:45 +02:00
|
|
|
((what :initarg :what :reader what))
|
|
|
|
(:report (lambda (condition stream)
|
|
|
|
(format stream "Request error: ~A" (what condition)))))
|
2016-08-14 04:35:03 +02:00
|
|
|
|
2016-08-18 04:24:09 +02:00
|
|
|
(defmacro find-json-symbol (sym)
|
2017-08-07 15:30:38 +01:00
|
|
|
`(find-symbol (symbol-name ,sym) json:*json-symbols-package*))
|
2016-08-18 04:24:09 +02:00
|
|
|
|
|
|
|
(defmacro trace-http ()
|
2017-08-07 15:30:38 +01:00
|
|
|
'(setf drakma:*header-stream* *standard-output*))
|
2016-08-18 04:24:09 +02:00
|
|
|
|
2017-08-07 15:30:38 +01:00
|
|
|
(defun download-file (b file-id)
|
|
|
|
"Get the path for a file from a file-id (see: get-file) and then
|
|
|
|
download it. Returns nil if the value of the http response code is
|
|
|
|
not success (200); otherwise it will returns three values: the
|
|
|
|
data, the http headers and the exension of the original file"
|
|
|
|
(with-package :cl-telegram-bot
|
|
|
|
(let* ((file-spec (decode (get-file b file-id))))
|
|
|
|
(with-ok-results (file-spec results)
|
2017-11-20 17:54:46 +02:00
|
|
|
(alexandria:when-let* ((path (access results 'file--path))
|
2017-08-07 15:30:38 +01:00
|
|
|
(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))))))))
|
|
|
|
|
|
|
|
;; Telegram API methods, see https://core.telegram.org/bots/api
|
2016-08-14 04:35:03 +02:00
|
|
|
|
2017-07-10 20:56:04 +01:00
|
|
|
(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)
|
2017-08-07 15:30:38 +01:00
|
|
|
nil)))
|
2017-07-10 20:56:04 +01:00
|
|
|
|
2016-08-14 04:35:03 +02:00
|
|
|
(defun get-updates (b &key limit timeout)
|
|
|
|
"https://core.telegram.org/bots/api#getupdates"
|
2016-08-14 13:18:43 +03:00
|
|
|
(let* ((current-id (id b))
|
2017-05-17 13:17:58 +02:00
|
|
|
(request (decode (make-request b "getUpdates"
|
|
|
|
(list (cons :offset current-id)
|
|
|
|
(cons :limit limit)
|
|
|
|
(cons :timeout timeout))
|
|
|
|
:streamp t)))
|
2016-08-18 04:24:09 +02:00
|
|
|
(results (slot-value request (find-json-symbol :result))))
|
|
|
|
(when (eql (slot-value request (find-json-symbol :ok)) nil)
|
2016-08-14 04:35:03 +02:00
|
|
|
(error 'request-error :what request))
|
|
|
|
(when (> (length results) 0)
|
|
|
|
(let* ((last-update (elt results (- (length results) 1)))
|
2016-08-18 04:24:09 +02:00
|
|
|
(id (slot-value last-update (find-json-symbol :update--id))))
|
2016-08-14 04:35:03 +02:00
|
|
|
(when (= current-id 0)
|
2016-08-14 13:18:43 +03:00
|
|
|
(setf (id b) id))
|
|
|
|
(incf (id b))))
|
2016-08-14 04:35:03 +02:00
|
|
|
results))
|