From ac0c8823a3bcf1a26845d4a2687cf981991cb2d9 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 12 Jan 2018 18:03:55 -0800 Subject: [PATCH] Implement method to get updates from Telegram. --- src/telepathy/bot.cr | 13 +++++++++++++ src/telepathy/model/update.cr | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/telepathy/model/update.cr diff --git a/src/telepathy/bot.cr b/src/telepathy/bot.cr index 1c37e45..cf1fa67 100644 --- a/src/telepathy/bot.cr +++ b/src/telepathy/bot.cr @@ -6,6 +6,8 @@ module Telepathy @request_base = "https://api.telegram.org/bot#{@api_token}" @this_user = uninitialized User? @this_user = get_me + @last_update_id = uninitialized Int64? + @last_update_id = nil end def get_me @@ -13,5 +15,16 @@ module Telepathy headers: HTTP::Headers{"User-agent" => "Telepathy"}) return Response(User).from_json(response.body).result end + + def get_updates + update_data = {} of String => Int64 | String + @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) + updates = Response(Array(Update)).from_json(response.body).result + updates.last?.try { |update| @last_update_id = update.update_id + 1 } + return updates + end end end diff --git a/src/telepathy/model/update.cr b/src/telepathy/model/update.cr new file mode 100644 index 0000000..2a1e6fd --- /dev/null +++ b/src/telepathy/model/update.cr @@ -0,0 +1,18 @@ +require "json" + +module Telepathy + class Update + JSON.mapping( + update_id: Int64, + message: Message?, + edited_message: Message?, + channel_post: Message?, + edited_channel_post: Message? + # TODO inline_query + # TODO chosen_inline_result + # TODO callback_query + # shipping_query + # pre_checkout_query + ) + end +end