From cebc91d13a499659ee43d40eeb19434304b14b48 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 13 Jan 2018 22:41:07 -0800 Subject: [PATCH] Add a method to send a photo. --- src/telepathy/bot.cr | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/telepathy/bot.cr b/src/telepathy/bot.cr index 684078b..fad0088 100644 --- a/src/telepathy/bot.cr +++ b/src/telepathy/bot.cr @@ -58,6 +58,41 @@ module Telepathy body: message_data.to_json) end + def send_photo(chat_id : String | Int64, photo : String | File, caption : String? = nil, + disable_notification : Bool = false, + reply_to_message_id : Int64? = nil) + IO.pipe do |reader, writer| + channel = Channel(String).new(1) + spawn do + HTTP::FormData.build(writer) do |formdata| + channel.send(formdata.content_type) + case chat_id + when String + formdata.field("chat_id", chat_id.as(String)) + when Int64 + formdata.field("chat_id", chat_id.as(Int64).to_s) + end + formdata.field("disable_notification", disable_notification.to_s) + caption.try { |caption| formdata.field("caption", caption) } + reply_to_message_id.try { |id| formdata.field("reply_to_message_id", id) } + case photo + when String + formdata.field("photo", photo.as(String)) + when File + formdata.file("photo", photo.as(IO), + HTTP::FormData::FileMetadata.new(filename: File.basename(photo.path))) + end + end + + writer.close + end + + response = HTTP::Client.get(@request_base + "/sendPhoto", + headers: HTTP::Headers{"User-agent" => "Telepathy", "Content-type" => channel.receive }, + body: reader) + end + end + def command(command_name, &block: Update, Array(String) -> Void) @command_hooks[command_name] = block end