Telegram Bot with Rails App
Step 1 : Install a Rails server to handle communications with the Telegram bot
Naming the Rails app as telegram_bot:
rails new telegram_bot
Confirm the server runs correctly:
cd telegram_bot
rails s
Step 2 : Create the /bot webhook endpoint in your created Rails App
Enter the following code in your Rails App:
config/routes.rb
defaults format: :json do
resource :bot, only: %i[create]
endapp/controllers/bots_controller.rbclass BotsController < ActionController::API
def create
head :ok
end
end
Restart the server. to check it is correctly working or not.
Step 3 : Invoke Telegram’s BotFather to start building your bot
You have to install Telegram App into your system
Visit here >> https://t.me/botfather :
if you have Telegram installed on your device, it’ll open up a chat window with BotFather (@botfather)
It will look like the below-shown image :
Visit here >> https://desktop.telegram.org/ :
if you don’t have Telegram installed on your device, it’ll help you to install and set up the telegram
It will look like the below-shown image :
Step 4 : Once you have done with your installation of Telegram, you just have to hit SEND MESSAGE button.
It will navigate you to the telegram page, where BotFather will invite you over Telegram
Step 5 : /newbot — Create the new bot
Type /newbot
in the BotFather chat.
Steps to create a new bot
- Use the /newbot command to create a new bot. The BotFather will ask you for a name and username, then generate an authentication token for your new bot.
2. The name of your bot is displayed in contact details and elsewhere.
3. The Username is a short name, to be used in mentions. Usernames are 5–32 characters long and are case insensitive, but may only include Latin characters, numbers, and underscores. Your bot’s username must end in ‘bot’, e.g. ‘test_bot’ or ‘TestBot’.
4. The token is a string along the lines of 1102045543:AAHdqTcFH1vGWJxfSeofSAs0K5PALDsaw
that is required to authorize.
Token will be provided by BotFather.
It will send a message if you mistyped the name
Step 6 : You can check your Bot status like below
You just have to hit URL : https://api.telegram.org/bot5041328541:AAFdQqw6PfHQPGiMpp5ff9UIQ297yZ76TMQ/getMe
“5041328541:AAFdQqw6PfHQPGiMpp5ff9UIQ297yZ76TMQ” is your generated Token
Step 7 : You can check status of particular id like this -
Just have to hit URL : https://api.telegram.org/bot5041328541:AAFdQqw6PfHQPGiMpp5ff9UIQ297yZ76TMQ/getUpdates
“5041328541:AAFdQqw6PfHQPGiMpp5ff9UIQ297yZ76TMQ” is your token
“114209129” is your chat id
“hi” is any message you wanna send
Step 8 : Type a message to your bot in the chat
Let’s come to the coding side -
- Make a controller :
2. Set routes:
3. Create view page :
4. Minor change in application.html.erb file regarding the styling of notice
Step 9 : Based on this code, you will get the following output -
Step 10 : By hitting on “Send group message” you can send a message to your telegram group.
Step 11 : By hitting on “Send private message” you can send a private message.
Step 12 : By hitting on “Send group message via controller” you can send messages via your controller and get a success message on success.