Not a subscriber?

Join thousands of others who are building self-directed lives through creativity, grit, and digital strategy—breaking free from the 9–5.
Receive one free message a week

Ruby Script for Telegram Notifications

In a recent podcast with Pietr Levels he mentioned that he uses Telegram for all his messaging and system notifications instead of paying for expensive third party services for this. This post shows you how to do this with ruby.

In short, he writes simple PHP scripts to check for certain uptime, events, errors, etc on his server. If anything is out of the ordinary he has the script send him a message on telegram notifying him of the issue. This is simple, and it works, especially for a single founder approach.

I don’t use PHP for my web apps, I use Ruby (Ruby on Rails) so I wanted to build something similar for myself as I’m tired of having Slack as being a entry point for all of these kinds of things.

So, I whipped up a quick script that allows me to send a message to a telegram bot. This script is below. However, before you can use the script you’ll need a telegram bot you can send a message to.

Here’s a quick video demo:

Let’s dive into it ..

Setting up a Telegram Bot

Setting a up a telegram bot is as simple as sending a message to @botfather using the /newbot command on Telegram.

Here’s a detailed set of instructionst that show you how.

Once complete, this will give you a Bot Token. You’ll want to save this somewhere secure like in you a credential manager/password manager of some sort. Don’t post this publicly or share it.

After you have a bot set up. You’ll want to send a message to it in Telegram so there is some recent chat history. You’ll need this so you can obtain a chat_id below.

Ruby Telegram Bot Script

Here’s the script:

require 'telegram/bot' # RE: gem install telegram-bot-ruby

class TelegramMessenger
  def initialize(bot_token, chat_id)
    @bot_token = bot_token
    @chat_id = chat_id
  end

  def get_updates
    Telegram::Bot::Client.run(@bot_token) do |bot|
      puts "Bot is running"
      updates = bot.api.getUpdates
      puts updates.to_json
      puts "Bot is done running"
    end
  end

  def escape_markdown(text)
    # Replace each special character with the escaped version
    text.gsub(/([\\\_\*\[\]\(\)\~\>\#\+\-\=\|\{\}\.\!])/) { |match| "\\#{match}" }
  end

  def send_message(message, parse_mode = 'MarkdownV2', chat_id = nil)
    puts "Sending message ... #{message}"
    chat_id ||= @chat_id
    Telegram::Bot::Client.run(@bot_token) do |bot|
      bot.api.send_message(chat_id: , text: escape_markdown(message), parse_mode: )
    end
    puts "Message sent!"
  rescue => e
    puts "Failed to send message: #{e.message}"
  end
end

In order for this script to work you’ll need a chat_id and the bot_token.

Obtaining the chat_id

To get the chat_id you’ll need to send the bot a message in Telegram so that there’s some chat history. Then you can run this command:

bot_token = 'your-bot-token'
telegramer = TelegramMessenger.new(bot_token, nil)
telegramer.get_updates

This will return some json. You’ll want to look for chat: { id: <some-chat-id> } in the resulting json, that is your chat_id.

Save your chat_id – probably the best to just store it is in the same place as your bot token so its easier to find.

Sending a Message to the Bot

Ok, you’ve got the bot_token and the chat_id, your script is ready.

Here’s how you send a message to your bot:

# Replace 'YOUR_BOT_API_TOKEN' and 'CHAT_ID' with your actual bot token and chat ID
bot_token = 'your-bot-token'
chat_id = 'your-chat-id'

# Create an instance of the TelegramMessenger class
telegramer = TelegramMessenger.new(bot_token, chat_id)

# Create a multi-line string for the message
message = <<~MESSAGE
    Check out *this* ruby method:

    ```ruby
    def foo 
        puts "bar"
    end    
    ```

    Now you can see the code in a code block.

    This is a test message.
MESSAGE

telegramer.send_message(message)

This will send formatted markdown to your bot and the bot will format it with code blocks/etc.

Thats it. Enjoy.

View this script on Github here.