From caca0b13981623b72f2803282a63b1186a6b02c6 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 12 Jan 2018 22:07:20 -0800 Subject: [PATCH] Add a way to send messages. --- src/telepathy/bot.cr | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/telepathy/bot.cr b/src/telepathy/bot.cr index 12239a7..1a955c8 100644 --- a/src/telepathy/bot.cr +++ b/src/telepathy/bot.cr @@ -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