Quickly Create a Telegram Bot in Ruby
Telegram might be a new name to you, but it’s gaining popularity the in the last few years, in recent news Telegram reported 100,000,000 monthly active users. So what is it all about ?
Telegram is a cloud based messaging app which is well known for a variety of features and it’s secure messaging being one of key features. It supports a variety of platforms like Windows, OSX, GNU/Linux, Android, iOS and Windows Phone. It is also worth mentioning that the client side of Telegram is open source, so we cold be using any other messaging app like Pidgin to connect with Telegram.
Some of the features of Telegram:
- Super Groups (1000 members or more)
- File/Photos transfer
- Open source client side
- Synchronizes your messages through all your devices
- File size sharing limit 1.5 GB
We are going to talk about the last one in particular. Telegram offers two APIs: The developer API and the bot API
First, we need to sign up for an account. Either download one of the clients or go directly to the web version of Telegram. From there, you’ll be asked to give a phone number and Telegram will send a confirmation SMS.
Meet The BotFather
Bots are special Telegram accounts designed to handle messages automatically. Users can interact with bots by sending them commands in private or group chats. We control our bots using HTTPS requests to the bot API.
What can we do with bots ?
- Take notes in Telegram groups
- Get weather updates
- Play Games
- Interact with other services such as social networks, IRC, etc.
We can do a lot of stuff with bots. The first step in creating our bot is to talk to the BotFather.
Type in the search form BotFather.
Click on the BotFather and a chat container will be opened so we can chat with him. Hit the start button which will display a list of commands that we can use to interact with the BotFather.
Let’s create our bot using the command
/newbot. It is going to ask for a name for our bot, so we’ll call our bot sitepbot.
Now we have to register for a username, note: it must end in bot, If our bot is named TetrisBot the username must be tetrisbot. Let it be sitepointbot.
BotFather will congratulate us, and give a link to our bot telegram.me/sitepoint_bot.
We can add a description and also add a profile picture to our bot. The
/help command will give us a list of commands we can interact with BotFather.
/newbot - create a new bot /token - generate authorization token /revoke - revoke bot access token /setname - change a bot's name /setdescription - change bot description /setabouttext - change bot about info /setuserpic - change bot profile photo /setinline - change inline settings /setinlinefeedback - change inline feedback settings /setcommands - change bot commands list /setjoingroups - can your bot be added to groups? /setprivacy - what messages does your bot see in groups? /deletebot - delete a bot /cancel - cancel the current operation
Using one of the commands above, we can make various changes to our bot. But, right now, the most important command is
/token, as it will give us an API token to access our bot through HTTPS. Go ahead and type that now:
The return value will look something like this:
After creating our bot and having it listen to us, it’s time for some Ruby power. There are several gems to interact with the bot API. We will be using telegram-bot-ruby.
We can install it directly from your terminal:
gem install telegram-bot-ruby
Or add it to our Gemfile:
If we are using it with the Gemfile we need to enter
bundle install in our project directory. This will install the gem and the dependencies it needs.
Hello SitePoint Example
Create a file named sitepointbot.rb, we’ll build out the content of this file here.
First, we need to import our gem:
Next, we need to add our token, which we generated earlier:
token = 'YOUR API TOKEN'
bot.api object, we can use all the methods from Bot API:
Telegram::Bot::Client.run(token) do |bot| bot.listen do |message| case message.text when '/sitepoint' bot.api.send_message(chat_id: message.chat.id, text: "Welcome to https://www.sitepoint.com") end end end
send_message is one of the methods from the Bot API and it sends a message to the current
chat_id is the unique identifier for the target chat, or user name of the target channel. Of course,
text is message to be sent to the chat. Whenever the bot sees
/sitepoint in our chat message, it will respond with the “Welcome to https://www.sitepoint.com!”
Note: It is strongly suggested that we use a slash (/) before our commands, we don’t want our bots to interfere with our conversations.
require 'telegram/bot' token = 'YOUR API TOKEN' Telegram::Bot::Client.run(token) do |bot| bot.listen do |message| case message.text when '/sitepoint' bot.api.send_message(chat_id: message.chat.id, text: "Welcome to https://www.sitepoint.com") end end end
Now it is time to run our bot:
We have to search for the bot. It will appear in the search result area.
As you can see, there is a “Start” button. If we click the button a
/start command will execute. This should always contain a welcome message from our bot, it is the best place to add general information about the bot, like what it does and what commands it understands.
This is the effect of the Start button or the
/start command. If you wanted to start the bot inside a chat, you can also type
which is necessary if you have more than one bot in a chat.
Now let’s try
The result is a welcome message with a link to SitePoint’s website.
One More Example
Let’s use the
sendLocation method. This method will make a point on the map. We will be using the same object
bot.api, but this time we want the
bot.api.send_location(chat_id: message.chat.id, latitude: -37.807416, longitude: 144.985339)
require 'telegram/bot' token = 'YOUR API KEY' Telegram::Bot::Client.run(token) do |bot| bot.listen do |message| case message.text when '/start' bot.api.send_message(chat_id: message.chat.id, text: "I am the SitePoint bot, My commands are /sitepoint /map") when '/sitepoint' bot.api.send_message(chat_id: message.chat.id, text: "Welcome to https://www.sitepoint.com") when '/map' bot.api.send_location(chat_id: message.chat.id, latitude: -37.807416, longitude: 144.985339) end end end
Note: We need to exit the bot and start again before seeing the changes.
Telegram offers a solid, secure messaging service. Like Slack, Telegram offers the ability to create bots that can interact with users. We have seen some basic features that bots can do, but we can make them smarter and more functional, take a look at the API of Telegram and be creative. If you make something great share it with us.