TODO: Telegram bot that sends messages to a telegram group when an event in your Rails app happens #


Step 1. Telegram Setup. API calls via the browser #

create a telegram bot: botfather.png save the API key that you were given to access the app:


Next - create a telegram group, invite the bot bot to group, make the bot a group admin

Next - get the group id via an API call in the browser:

result: groupid.png from the request we will see the group id:


Now we can post a message to the group via the browser:

result: tgresult.png

Step 2. Rails link_to post a bot message - view (elementary) #

any view:

<%= link_to "Send message via View", "", method: :post %>

Step 3. Rails link_to post a bot message - controller (advanced) #

We will need to send HTTP requests from a controller action. For this we will use gem "httparty".


gem "httparty", "~> 0.18" # Makes http fun! Also, makes consuming restful web services dead easy


post "bots/say_hello", to: "bots#say_hello", as: :say_hello

bots_controller.rb - displays a few different ways of sending an HTTParty requests.

class BotsController < ApplicationController

  def say_hello
    api_secret_key = "1629298034:AAGMejWo9WFeZ-XP51f4Tpbb_L_0t8nO4xM"
    chat_id = "-574253305"

    text = "#{current_user} @yarotheslav #{request.url} sorry for spam"

    # body = {text: "#{current_user} is alive", chat_id: chat_id}
    #"{api_secret_key}/sendMessage", body: body)"{api_secret_key}/sendMessage",
      headers: {
        'Content-Type' => 'application/json'
      body: {
        chat_id: chat_id,
        text: text
    redirect_to root_path, notice: "message sent"


any view - invoke this controller action:

<%= link_to "Send message via Controller", say_hello_path, method: :post %>

or a button that has method: :post included by default:

<%= button_to "Send message via Controller", say_hello_path %>

Useful links & future readings: