Last active
August 27, 2019 07:21
-
-
Save ollpu/a279c61fd9f240e04a96ceaecddc7362 to your computer and use it in GitHub Desktop.
Lightweight Passive Telegram Bot API (Ruby)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'net/http' | |
module TgAPI | |
BOT_TOKEN = "[...]" | |
def self.api_do action, **params | |
uri = URI.parse "https://api.telegram.org/bot#{BOT_TOKEN}/#{action.to_s}" | |
response = Net::HTTP.post uri, params.to_json, "Content-Type" => "application/json" | |
unless response.is_a? Net::HTTPSuccess | |
m = JSON.parse(response.body)&.[]("description") rescue nil | |
m ||= response.code | |
raise ArgumentError, "API request failed: #{m}" | |
end | |
JSON.parse(response.body) | |
end | |
def self.send_message chat_id, text, **params | |
params[:chat_id] = chat_id | |
params[:text] = text | |
api_do :sendMessage, **params | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment