Last active
October 30, 2017 15:35
-
-
Save ollpu/ed20764deafb015772be to your computer and use it in GitHub Desktop.
Telegram Bot API in Rails
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
# Set ENV["TELEGRAM_BOT_API_KEY"] before usage! | |
# Use telegram#repair to connect to API | |
require "net/http" | |
require "uri" | |
include ActionView::Helpers::TextHelper | |
class TelegramController < ApplicationController | |
protect_from_forgery with: :null_session | |
# Reconnects to Telegram Bot API. Avoid permitting access from unknown entities. | |
def repair | |
# Clear webhook so that getUpdates can be used | |
uri = URI.parse("https://api.telegram.org/bot#{ENV["TELEGRAM_BOT_API_KEY"]}/setWebhook") | |
Net::HTTP.post_form(uri,{}) | |
# Mark all messages as read | |
uri = URI.parse("https://api.telegram.org/bot#{ENV["TELEGRAM_BOT_API_KEY"]}/getUpdates") | |
Net::HTTP.post_form(uri,{ | |
:offset => 2147483647 | |
}) | |
# Reconfigure webhook | |
uri = URI.parse("https://api.telegram.org/bot#{ENV["TELEGRAM_BOT_API_KEY"]}/setWebhook") | |
Net::HTTP.post_form(uri,{ | |
:url => telegram_url | |
}) | |
Rails.cache.delete('bot_command') | |
render :json => {:ok => true} | |
end | |
def index | |
update_id = telegram_params[:update_id] | |
last_update_id = Rails.cache.read("bot_command") | |
# If last_update_id == nil then true, otherwise check validity | |
if last_update_id.present? ? last_update_id + 1 == update_id : true | |
Rails.cache.write("bot_command", update_id) | |
message = telegram_params[:message] | |
if message.present? | |
# Prepare response | |
response_msg = parse_command(message[:text], message[:from][:id]) | |
# Determine id of reply | |
reply_id = response_msg[:reply] ? message[:message_id] : nil | |
# Send message | |
successful = send_message message[:chat][:id], response_msg[:text], true, reply_id | |
else successful = false end | |
else not_privileged end | |
render :json => {:ok => successful} | |
end | |
private | |
def send_message (chat_id, text = "", web_preview = true, reply_id = nil, reply_markup = nil) | |
if chat_id.present? | |
uri = URI.parse("https://api.telegram.org/bot#{ENV["TELEGRAM_BOT_API_KEY"]}/sendMessage") | |
response = Net::HTTP.post_form(uri, | |
{ | |
:chat_id => chat_id, | |
:text => text, | |
:disable_web_page_preview => (not web_preview), | |
:reply_to_message_id => reply_id, | |
:reply_markup => reply_markup | |
}) | |
return JSON.parse(response.body)["ok"] | |
else | |
return false | |
end | |
end | |
def parse_command (text, sender) | |
to_return = {:response => false} | |
params = text[/^\/[\w]+ (.+)$/, 1] | |
params_arr = text.scan(/ ([^ ]+)/) | |
case text[/^\/([\w]+)/, 1] | |
when "echo" | |
to_return[:text] = params | |
end | |
to_return | |
end | |
def telegram_params | |
params.require(:telegram) | |
.permit( | |
:update_id, | |
message: [ | |
:message_id, | |
:text, | |
:chat => [:id], | |
:from => [:id] | |
] | |
) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the Gist, could you please also share how your router is configured. Thanks!