| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- (in-package #:chatikbot)
- (defvar *telegram-token* nil "Telegram bot token")
- (defparameter +telegram-api-format+ "https://api.telegram.org/bot~A/~A")
- (defvar *telegram-timeout* 30 "Default Telegram timeout")
- ;; (defun %telegram-api-call (method &optional args)
- ;; (let* ((params (loop for (k . v) in args collect (cons
- ;; (princ-to-string k)
- ;; (princ-to-string v))))
- ;; (response (yason:parse
- ;; (flexi-streams:octets-to-string
- ;; (drakma:http-request (format nil +telegram-api-format+ *telegram-token* method)
- ;; :method :post
- ;; :parameters params
- ;; :external-format-out :utf8)
- ;; :external-format :utf8)
- ;; :object-as :alist)))
- ;; (unless (aget "ok" response)
- ;; (error (aget "description" response)))
- ;; (aget "result" response)))
- (defun %telegram-api-call (method &optional args)
- (let* ((params (loop for (k . v) in args collect (cons
- (princ-to-string k)
- (princ-to-string v))))
- (timeout (+ 5 (or (cdr (assoc :timeout args))
- *telegram-timeout*)))
- (response (yason:parse
- (flexi-streams:octets-to-string
- (handler-case
- (bordeaux-threads:with-timeout (timeout)
- (dex:post (format nil +telegram-api-format+ *telegram-token* method)
- :content params
- :use-connection-pool t
- :force-binary t))
- (bordeaux-threads:timeout (e)
- (error e)))
- :external-format :utf8)
- :object-as :alist)))
- (unless (aget "ok" response)
- (error (aget "description" response)))
- (aget "result" response)))
- (defun telegram-get-updates (&key offset limit timeout)
- (%telegram-api-call
- "getUpdates"
- (list (cons "offset" offset)
- (cons "limit" limit)
- (cons "timeout" timeout))))
- (defun telegram-send-message (chat-id text &key disable-web-preview reply-to reply-markup)
- (%telegram-api-call
- "sendMessage"
- (list (cons "chat_id" chat-id)
- (cons "text" text)
- (cons "disable_web_page_preview" disable-web-preview)
- (cons "reply_to_message_id" reply-to)
- (cons "reply_markup" reply-markup))))
- (defun telegram-send-sticker (chat-id sticker &key reply-to reply-markup)
- (%telegram-api-call
- "sendSticker"
- (list (cons "chat_id" chat-id)
- (cons "sticker" sticker)
- (cons "reply_to_message_id" reply-to)
- (cons "reply_markup" reply-markup))))
|