Telegram deploy notifications with Rake

I wanted a simple way to get a Telegram notification every time a deploy goes out from my Rails app. Here’s how to set it up:

What You’ll Need

Variable What it is
TELEGRAM_BOT_TOKEN Your bot’s API token from @BotFather
TELEGRAM_CHAT_ID The chat/group ID where notifications get sent
GITHUB_TOKEN A GitHub personal access token (needs repo read access)
GITHUB_REPO Your repo in owner/repo

1. Environment Variables

Telegram Token and Chat IDs

  1. Open Telegram and message @BotFather
  2. Send /newbot, follow the prompts, and grab the token it gives you
  3. Add your new bot to whatever chat or group you want notifications in
  4. To get your chat ID, send a message in that chat, then hit https://api.telegram.org/bot<YOUR_TOKEN>/getUpdates — you’ll see the chat.id in the response

GitHub Personal Access Token

Create a personal access token with read access to your repo’s contents and metadata. This lets the task fetch the latest push activity and commit details.

Add these environment variables to your hosting platform.

2. The Rake Task

Drop this into lib/tasks/telegram.rake:

namespace :telegram do
  desc "Send a Telegram notification after deploy with commit info"
  # ADD "bin/rails telegram:deploy_notify" to your post deploy scripts.
  task deploy_notify: :environment do
    require "net/http"
    require "json"

    bot_token = ENV["TELEGRAM_BOT_TOKEN"]
    chat_id = ENV["TELEGRAM_CHAT_ID"]
    github_token = ENV["GITHUB_TOKEN"]
    repo = ENV["GITHUB_REPO"]

    unless bot_token && chat_id && github_token && repo
      puts "Skipping deploy notification: missing environment variables."
      next
    end

    http = Net::HTTP.new("api.github.com", 443)
    http.use_ssl = true
    headers = {"Authorization" => "Bearer #{github_token}", "Accept" => "application/vnd.github+json"}

    # Get the latest push activity to detect branch
    activity_request = Net::HTTP::Get.new("/repos/#{repo}/activity?activity_type=push&direction=desc&per_page=1", headers)
    activity_response = http.request(activity_request)
    activity = JSON.parse(activity_response.body).first

    branch = activity["ref"].sub("refs/heads/", "")

    # Get the current commit on the branch (activity log can lag behind on amended commits)
    ref_request = Net::HTTP::Get.new("/repos/#{repo}/git/refs/heads/#{branch}", headers)
    ref_response = http.request(ref_request)
    ref_data = JSON.parse(ref_response.body)

    commit_sha = ref_data["object"]["sha"]
    commit_short = commit_sha[0, 7]

    # Get commit details for the message
    commit_request = Net::HTTP::Get.new("/repos/#{repo}/commits/#{commit_sha}", headers)
    commit_response = http.request(commit_request)
    commit_data = JSON.parse(commit_response.body)
    commit_message = commit_data["commit"]["message"].lines.first.strip
    commit_url = "https://github.com/#{repo}/commit/#{commit_sha}"

    text = <<~MSG
      âś… *Deployed!*

      *Branch:* `#{branch}`
      *Commit:* [#{commit_short}](#{commit_url})
      *Message:* #{commit_message}
    MSG

    uri = URI("https://api.telegram.org/bot#{bot_token}/sendMessage")
    response = Net::HTTP.post_form(uri, {
      chat_id: chat_id,
      text: text,
      parse_mode: "Markdown",
      disable_web_page_preview: true
    })

    if response.is_a?(Net::HTTPSuccess)
      puts "Telegram deploy notification sent."
    else
      puts "Telegram notification failed: #{response.body}"
    end
  end
end

3. Running It

Add this to your post-deploy script:

bin/rails telegram:deploy_notify

How It Works

The task hits the GitHub API to grab the most recent push activity on your repo. From that, it figures out:

  • Which branch was pushed
  • Which commit landed
  • What the commit message was

Then it fires off a Markdown-formatted message to Telegram that looks something like:

Deployed!

Branch: main Commit: abcdefg Message: Fix the thing that was broken


Happy deploying!