Add a way to send messages.

This commit is contained in:
Danila Fedorin 2018-01-12 22:07:20 -08:00
parent 4eaaaa28b7
commit caca0b1398
1 changed files with 23 additions and 0 deletions

View File

@ -6,6 +6,10 @@ 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?
@ -35,6 +39,25 @@ module Telepathy
return Response(Array(Update)).from_json(response.body).result
end
def send_message(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(@request_base + "/sendMessage",
headers: HTTP::Headers{"User-agent" => "Telepathy", "Content-type" => "application/json" },
body: message_data.to_json)
end
def command(command_name, &block: Update, Array(String) -> Void)
@command_hooks[command_name] = block
end