Skip to content

Instantly share code, notes, and snippets.

@ollpu
Last active August 27, 2019 07:21
Show Gist options
  • Save ollpu/a279c61fd9f240e04a96ceaecddc7362 to your computer and use it in GitHub Desktop.
Save ollpu/a279c61fd9f240e04a96ceaecddc7362 to your computer and use it in GitHub Desktop.
Lightweight Passive Telegram Bot API (Ruby)
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