Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send files via pathanmes #19

Draft
wants to merge 3 commits into
base: clos-everywhere
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/message.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,6 @@ the file.")
allow-sending-without-reply reply-markup)
"A method for photo sending based on photo ID.

The file-based method does not work yet.

https://core.telegram.org/bots/api#sendphoto"
(declare (ignorable caption parse-mode caption-entities
disable-notification protect-content reply-to-message-id
Expand All @@ -381,6 +379,24 @@ https://core.telegram.org/bots/api#sendphoto"
(log:debug "Sending photo" chat (get-file-name photo))
(apply #'send-photo bot chat (get-file-id photo) options))

(defmethod send-photo (bot chat (photo pathname)
&rest options
&key caption parse-mode caption-entities
disable-notification protect-content reply-to-message-id
allow-sending-without-reply reply-markup)
"A method for photo sending based on pathname.

https://core.telegram.org/bots/api#sendphoto"
(declare (ignorable caption parse-mode caption-entities
disable-notification protect-content reply-to-message-id
allow-sending-without-reply reply-markup))
(log:debug "Sending photo" chat photo)
(apply #'make-request bot "sendPhoto"
:|chat_id| (get-chat-id chat)
:|photo| photo
:pathname-p t
options))

(defmethod send-audio (bot chat (audio string)
&rest options
&key caption parse-mode caption-entities
Expand Down
42 changes: 21 additions & 21 deletions src/network.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,36 @@
(:report (lambda (condition stream)
(format stream "Request error: ~A" (what condition)))))

(defun process-options (&rest options &key (pathname-p nil) &allow-other-keys)
(loop for (key value)
on (alexandria:remove-from-plist options :timeout :streamp :pathname-p)
by #'cddr
when value
if pathname-p
collect (cons (kebab:to-snake-case key) value)
else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here you are breaking the previous logic or this else belong to if but having a wrong indentation. Seems there is a problem with loop indentation in emacs, because for me in this small example it also indents else on the same level as when:

CL-USER> (loop for i in (list NIL NIL 1 2 3 4 5)
               when i
                 if (< i 3)
                   collect (list :before-else i)
               else
                 collect (list :else i))
((:BEFORE-ELSE 1) (:BEFORE-ELSE 2) (:ELSE 3) (:ELSE 4) (:ELSE 5))

Let's rewrite this code to make it more readable?

Also, I don't like an explicit pathname-p argument. We already have a pathname in the options. So we can decide if we wanna to make a multipart request right in the make-request itself. I'd add a helper function (multipart-request-required-p options) which will return true if at least one of options is pathname. Also pathname-p argument to process-options should be renamed to return-alist-p - this way it will be more obvious that this function can return plist or alist depending on this mode.

collect (kebab:to-snake-case key)
and
collect value))

(defun make-request (bot name &rest options &key (streamp nil) (timeout 3) &allow-other-keys)
"Perform HTTP request to 'name API method with 'options JSON-encoded object."

(defun make-request (bot name &rest options &key (streamp nil) (timeout 3) (pathname-p nil) &allow-other-keys)
"Perform HTTP request to 'name API method with 'options JSON-encoded object or multi-part form-data."
(declare (ignore streamp))

(let ((url (concatenate 'string (get-endpoint bot) name)))
(log:debug "Posting data to"
(obfuscate url)
options)
(let* ((max-timeout (* timeout 10))
(processed-options (loop for (key value)
on (alexandria:remove-from-plist options :timeout :streamp)
by #'cddr
when value
collect (kebab:to-snake-case key)
and
collect value))
(processed-options (apply #'process-options options))
(response
(if *proxy*
(dexador:post url
:headers '(("Content-Type" . "application/json"))
:content (jonathan:to-json processed-options)
:read-timeout max-timeout
:connect-timeout max-timeout
:proxy *proxy*)
(dexador:post url
:headers '(("Content-Type" . "application/json"))
:content (jonathan:to-json processed-options)
:read-timeout max-timeout
:connect-timeout max-timeout)))
(dexador:post url
:headers (unless pathname-p '(("Content-Type" . "application/json")))
:content (if pathname-p processed-options (jonathan:to-json processed-options))
:read-timeout max-timeout
:connect-timeout max-timeout
:proxy (if *proxy* *proxy* dex:*default-proxy*)))
(data (jonathan:parse response)))
(unless (getf data :|ok|)
(log:error "Wrong data received from the server" data)
Expand Down