I’ve decided to implement Telegram notifications into one of my pet project - Evendy.ru.
After some googling I’ve found great gem https://github.com/telegram-bot-rb/telegram-bot, gem is actively maintained and have great documentation.
So, if you want to implement Telegram messages into some channel you need to follow this simple easy steps:
1) Create new bot using @BotFather bot, and get secret token.
2) Create Telegram channel from your Telegram client, and add new bot as Administrator.
3) Create config/initializers/telegram.rb
TELEGRAM_API_KEY = 'YOUR_BOT_TOKEN'
4) Just send message to your public channel:
bot = Telegram::Bot::Client.new(TELEGRAM_API_KEY)
bot.send_message(chat_id: "@#{YOUR_CHANNEL_NAME}", text: 'YOUR_TEXT')
If you want you can add more custom commands with overriding default controller with custom:
class TelegramWebhooksController < Telegram::Bot::UpdatesController
include Telegram::Bot::UpdatesController::MessageContext
context_to_action!
end
And add to your routes.rb:
telegram_webhooks TelegramWebhooksController
Some example bot action:
def send_message(message, parse_mode = :HTML)
respond_with :message, text: message, parse_mode: parse_mode
end
def events
send_message(Event.actual.all.map { |event| "<b>[#{event.id}] #{event.title}</b>" }.join("\n"))
end
More: telegram-bot gem
Example bot usage: