From 259bed98232333302ce61723ad6045f6cceeb6a7 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 16 Apr 2018 19:47:35 -0700 Subject: [PATCH] Move some of the sending code outside of the bot. --- src/telepathy/bot.cr | 66 +++++----------------------------- src/telepathy/utils.cr | 81 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 58 deletions(-) create mode 100644 src/telepathy/utils.cr diff --git a/src/telepathy/bot.cr b/src/telepathy/bot.cr index 53a2593..89950ce 100644 --- a/src/telepathy/bot.cr +++ b/src/telepathy/bot.cr @@ -1,4 +1,5 @@ require "http" +require "./utils.cr" module Telepathy class Bot @@ -6,10 +7,6 @@ module Telepathy Done end - enum ParseMode - Normal, Markdown, HTML - end - def initialize(@api_token : String) @request_base = "https://api.telegram.org/bot#{@api_token}" @this_user = uninitialized User? @@ -26,74 +23,27 @@ module Telepathy end def get_me - response = HTTP::Client.get(@request_base + "/getMe", - headers: HTTP::Headers{"User-agent" => "Telepathy"}) - return Response(User).from_json(response.body).result + Utils.get_me(@api_token) end def get_updates(timeout = 0) - update_data = {} of String => Int64 | Int32 | String - update_data["timeout"] = timeout - @last_update_id.try { |id| update_data["offset"] = id } - response = HTTP::Client.get(@request_base + "/getUpdates", - headers: HTTP::Headers{"User-agent" => "Telepathy", "Content-type" => "application/json" }, - body: update_data.to_json) - return Response(Array(Update)).from_json(response.body).result + Utils.get_updates(@api_token, timeout, @last_update_id) end def send_message(chat_id : String | Int64, text : String, - parse_mode : ParseMode = ParseMode::Normal, + parse_mode : Utils::ParseMode = Utils::ParseMode::Normal, disable_web_preview : Bool = false, disable_notification : Bool = false, reply_to_message_id : Int64? = nil) - message_data = { "chat_id" => chat_id, "text" => text } of String => Int64 | String | Bool - message_data["disable_web_preview"] = true if disable_web_preview - message_data["disable_notification"] = true if disable_notification - if parse_mode == ParseMode::Markdown - message_data["parse_mode"] = "Markdown" - elsif parse_mode == ParseMode::HTML - message_data["parse_mode"] = "HTML" - end - reply_to_message_id.try { |id| message_data["reply_to_message_id"] = id } - HTTP::Client.get(@request_base + "/sendMessage", - headers: HTTP::Headers{"User-agent" => "Telepathy", "Content-type" => "application/json" }, - body: message_data.to_json) + Utils.send_message(@api_token, chat_id, text, + parse_mode, disable_web_preview, + disable_notification, reply_to_message_id) 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 + Utils.send_photo(@api_token, chat_id, photo, caption, disable_notification, reply_to_message_id) end def command(command_name, &block: Update, Array(String) -> Void) diff --git a/src/telepathy/utils.cr b/src/telepathy/utils.cr new file mode 100644 index 0000000..cb67039 --- /dev/null +++ b/src/telepathy/utils.cr @@ -0,0 +1,81 @@ +module Telepathy::Utils + extend self + API_URL = "https://api.telegram.org/bot" + + enum ParseMode + Normal, Markdown, HTML + end + + def get_me(api_key : String) + response = HTTP::Client.get(API_URL + api_key + "/getMe", + headers: HTTP::Headers{"User-agent" => "Telepathy"}) + return Response(User).from_json(response.body).result + end + + def get_updates(api_key : String, timeout = 0, last_update_id : Int? = nil) + update_data = {} of String => Int64 | Int32 | String + update_data["timeout"] = timeout + last_update_id.try { |id| update_data["offset"] = id } + response = HTTP::Client.get(API_URL + api_key + "/getUpdates", + headers: HTTP::Headers{"User-agent" => "Telepathy", + "Content-type" => "application/json" }, + body: update_data.to_json) + return Response(Array(Update)).from_json(response.body).result + end + + def send_message(api_key : String, chat_id : String | Int64, text : String, + parse_mode : ParseMode = ParseMode::Normal, + disable_web_preview : Bool = false, + disable_notification : Bool = false, + reply_to_message_id : Int64? = nil) + message_data = { "chat_id" => chat_id, "text" => text } of String => Int64 | String | Bool + message_data["disable_web_preview"] = true if disable_web_preview + message_data["disable_notification"] = true if disable_notification + if parse_mode == ParseMode::Markdown + message_data["parse_mode"] = "Markdown" + elsif parse_mode == ParseMode::HTML + message_data["parse_mode"] = "HTML" + end + reply_to_message_id.try { |id| message_data["reply_to_message_id"] = id } + HTTP::Client.get(API_URL + api_key + "/sendMessage", + headers: HTTP::Headers{"User-agent" => "Telepathy", "Content-type" => "application/json" }, + body: message_data.to_json) + end + + def send_photo(api_key : String, 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(API_URL + api_key + "/sendPhoto", + headers: HTTP::Headers{"User-agent" => "Telepathy", + "Content-type" => channel.receive }, + body: reader) + end + end +end