tg_error_notifier
Gem for Rails error notifications to Telegram.
What it catches
- Unhandled errors in Rack/Rails request cycle.
- Failed ActiveJob executions.
Quick usage
# Gemfile
gem "tg_error_notifier"# config/initializers/telegram_error_notifier.rb
Rails.application.configure do
config.telegram_error_notifier.bot_token = ENV["TELEGRAM_BOT_TOKEN"]
config.telegram_error_notifier.chat_id = ENV["TELEGRAM_ERRORS_CHAT_ID"]
config.telegram_error_notifier.app_name = "my_app"
endEnvironment variables
TELEGRAM_BOT_TOKENTELEGRAM_ERRORS_CHAT_ID-
TELEGRAM_ERRORS_APP_NAME(optional)
Telegram setup (bot, private channel/group, chat_id)
1. Create a bot and get token
- Open Telegram and start chat with
@BotFather. - Send
/newbotand follow prompts (bot name + username ending withbot). - Copy the HTTP API token (looks like
123456:ABC...) and save it asTELEGRAM_BOT_TOKEN.
2. Create a private channel or private group
Private channel
- Telegram -> New Channel -> set as Private.
- Open channel settings -> Administrators -> add your bot as admin.
- Grant at least permission to post messages.
Private group (or supergroup)
- Telegram -> New Group -> add at least one member.
- Add your bot to the group.
- Promote bot to admin if needed (recommended for reliability).
- Keep group private (no public username).
3. Get chat_id
Method A (easy): @userinfobot / @RawDataBot
- Add bot
@userinfobot(or@RawDataBot) into your target channel/group. - Send any message there.
- Open the helper bot dialog and read chat id.
- For channels/supergroups it is usually negative and starts with
-100.... - Delete this bot from your channel.
Method B (official API): getUpdates
- Send at least one message in target chat after adding your bot.
- Open in browser:
https://api.telegram.org/bot<TELEGRAM_BOT_TOKEN>/getUpdates - Find
chatobject and copychat.id.
Examples:
- Private channel/supergroup:
-1001234567890 - Private group (old style):
-123456789
4. Validate before using in Rails
- Ensure bot is present in the target chat.
- Ensure bot has permission to send messages.
- Put value into
TELEGRAM_ERRORS_CHAT_IDand run a smoke test.
Manual notification
begin
do_work
rescue => e
pp TgErrorNotifier.capture_exception(e)
# or with extra context:
TgErrorNotifier.capture_exception(
e,
source: "custom",
context: { feature: "sync", user_id: current_user&.id }
)
raise
endcapture_exception returns a diagnostic hash, e.g.:
{ sent: true, status: :sent, code: 200 }{ sent: false, status: :skipped, reason: "missing_chat_id" }{ sent: false, status: :failed, reason: "telegram_api_error", code: 400, body: "..." }