Compare commits
4 Commits
81eee54695
...
bfda135c19
Author | SHA1 | Date |
---|---|---|
Danila Fedorin | bfda135c19 | |
Danila Fedorin | 806d8db44c | |
Danila Fedorin | 60835022fb | |
Danila Fedorin | cebc91d13a |
|
@ -18,6 +18,8 @@ module Telepathy
|
|||
@last_update_id = nil
|
||||
@command_hooks = {} of String => Update, Array(String) -> Void
|
||||
@message_hooks = [] of Update -> Void
|
||||
@poll_start_hooks = [] of -> Void
|
||||
@poll_end_hooks = [] of -> Void
|
||||
@poll_channel = Channel(Int64?|Control).new
|
||||
@update_channel = Channel(Array(Update)|Control).new
|
||||
@poll_running = false
|
||||
|
@ -58,6 +60,42 @@ 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
|
||||
photo_file = photo.as(File)
|
||||
formdata.file("photo", photo_file,
|
||||
HTTP::FormData::FileMetadata.new(filename: File.basename(photo_file.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
|
||||
|
@ -66,6 +104,14 @@ module Telepathy
|
|||
@message_hooks.push(block)
|
||||
end
|
||||
|
||||
def poll_start(&block: -> Void)
|
||||
@poll_start_hooks.push(block);
|
||||
end
|
||||
|
||||
def poll_end(&block: -> Void)
|
||||
@poll_end_hooks.push(block);
|
||||
end
|
||||
|
||||
private def process_updates(updates)
|
||||
updates.each do |update|
|
||||
if message = update.message
|
||||
|
@ -94,7 +140,6 @@ module Telepathy
|
|||
@last_update_id = action.nil? ? @last_update_id : action + 1
|
||||
@update_channel.send get_updates 10
|
||||
when Control
|
||||
@update_channel.send action
|
||||
break
|
||||
end
|
||||
end
|
||||
|
@ -107,6 +152,7 @@ module Telepathy
|
|||
process_updates(item)
|
||||
@poll_channel.send item.last?.try &.update_id
|
||||
when Control
|
||||
@poll_channel.send item
|
||||
break
|
||||
end
|
||||
end
|
||||
|
@ -118,6 +164,7 @@ module Telepathy
|
|||
@poll_running = true
|
||||
spawn_workers
|
||||
@poll_channel.send nil
|
||||
@poll_start_hooks.each &.call
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -125,6 +172,7 @@ module Telepathy
|
|||
if @poll_running
|
||||
@poll_running = false
|
||||
@poll_channel.send Control::Done
|
||||
@poll_end_hooks.each &.call
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue