telepathy/src/telepathy/bot.cr

31 lines
1.2 KiB
Crystal
Raw Normal View History

2018-01-12 17:22:05 -08:00
require "http"
module Telepathy
class Bot
def initialize(@api_token : String)
@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
2018-01-12 17:22:05 -08:00
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
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
2018-01-12 17:22:05 -08:00
end
end