---
title: "Telegram Part 2. Notification for CRUD actions"
author: "Yaroslav Shmarov"
date: "2021-04-12"
canonical_url: "https://blog.superails.com/ruby-on-rails-telegram-notifications"
markdown_url: "https://blog.superails.com/ruby-on-rails-telegram-notifications.md"
description: "prerequisites: Telegram Part 1{:target=\"blank\"} console: app/mailers/telegram_mailer.rb - action for bot to send message to the chat api_secret_key - access key…"
tags: ["ruby","rails","ruby-on-rails","telegram","bots","httparty"]
license: "GNU AGPL v3. Credit Yaroslav Shmarov and link the canonical URL."
video_url: "https://www.youtube.com/watch?v=t0U0CPZH8ZU"
---

# Telegram Part 2. Notification for CRUD actions

### TODO: Telegram bot notifies group chat whenever a new post is created.

prerequisites:
* [Telegram Part 1](https://blog.superails.com/ruby-on-rails-telegram-api-send-message){:target="blank"}

console:
```
rails g mailer TelegramMailer
```
app/mailers/telegram_mailer.rb - action for bot to send message to the chat
```
class TelegramMailer < ApplicationMailer
  def group_message(text)
    api_secret_key = "1629298034:AAGMejWo9WFeZ-XP51f4Tpbb_L_0t8nO4xM"
    chat_id = "-574253305"

    HTTParty.post("https://api.telegram.org/bot#{api_secret_key}/sendMessage",
      headers: {
        'Content-Type' => 'application/json'
      },
      body: {
        chat_id: chat_id,
        text: text
      }.to_json
    )
  end
end
```
`api_secret_key` - access key of the bot. 

`chat_id` - ID of the group chat.

app/controllers/posts_controller.rb
```
def create
  @post = Post.new(post_params)
  if @post.save
    text = "#{current_user} created post: #{@post.title}"
    TelegramMailer.group_message(text).deliver_now
    redirect_to @post, notice: 'Post was successfully created.'
  else
    render :new
  end
end
```

`text` - customizable text that will be send via telegram.

`TelegramMailer.group_message(text).deliver_now` - action to send the message from `telegram_mailer.rb`

### That's it!
