Telegram was mainly a mobile app, but also for developing reasons you’re planning to desire they mounted on exactly the same machine you’re utilizing for composing rule. This way, you are able to easily test it without unlocking the phone each and every time. And, you’ll have the ability to replicate and paste their Telegram bot’s API key straight into your text editor.
Time for you seize that API secret!
Step two: Chat with the BotFather getting the API secret
Telegram bot production are an unique proce because most of it really is concentrated around your personal relationships with a Telegram bot. That bot is the BotFather. Within your Telegram desktop computer application, you need to hunting his login name and start a discussion with him.
Supply the begin demand to BotFather by entering /start .
You’ll discover a list of directions that will you establish, modify, and regulate their spiders. Since it’s very first times, you’ll desire /newbot .
Set alongside the proce for building a-twitter robot, a Mastodon robot, or any other style of bot I’ve attempted, Telegram helps make the initial setup super easy. I gue that demonstrates just what outstanding appliance Telegram bots can be!
After offering the /newbot order, you are able to choose a reputation and username to suit your robot. Title is exactly what your users will see the robot as with their unique connections number, together with username is just how they’ll believe it is. Think about the login name like a Twitter handle; it should feel distinctive, and it’s most useful if it’s short and remarkable.
With this done, you’ll be given your bot’s API key. The API secret try exactly how Telegram understands the laws you write was aociated with this bot. Every robot has its own API secret, and you shouldn’t express they with people or they could hijack your own robot and possess they complete their own wicked deeds.
That concludes our very own talk with BotFather for the present time — onto the laws!
Step three: creating the bot’s jewels and index
Open terminal, and would mkdir telegram-bot . This creates another directory site for your bot’s data files. it is better to have them restricted to one folder therefore it’s easy to manage. Demand index with cd telegram-bot right after which perform the next:
touch Gemfile touch bot.rb atom .
This brings two empty data files, one for specifying the gems you’ll requirement and something where the bot’s signal will reside. The final order opens up both of these data files in Atom.
Click the Gemfile in Atom’s sidebar, and paste the subsequent in:
This says to Bundler to grab the Ruby user interface for the Telegram API from rubygems.
To perform the jewel set-up, get back to your own terminal and kind package . Here’s what you ought to discover:
That’s it for gem set-up, now we’re eventually getting onto the rule.
Programming the first Telegram robot
The actual code that is going to getting run continuously regarding the server is actually inside bot.rb. It’s vacant immediately, but here we’re attending link in the Telegram jewel we simply included and produce a bot.
It’s very little signal to publish. By the time you’re finished, and this is what you’ll have actually:
In Telegram, www.hookupwebsites.org/jpeoplemeet-review this is what the laws above really does:
Let’s look at exactly what every part of the rule do and create it we go.
(Upgrade the TOKEN with all the API token you duplicated from BotFather)
Ruby makes it fairly easy to gue just what laws can do. The three outlines above include the Telegram robot usability your document then make a unique robot item from Telegram robot cla, authenticated together with your token so that the system understands where you should send the info.
The following part is the one large cycle. It appears to be complicated to start with, nevertheless’s simple to choose apart.
The first line says to the bot to keep listening for directions. And, whenever it gets a command to pa they on meage varying. The puts line logs the command to your terminal to discover what’s going on as the robot works.
The bot’s reaction steps is stored in a case statement. The outcome declaration’s feedback try given through from meage varying after it’s started cleaned right up from the gem’s get_command_for system. The bot’s reply text is defined with respect to the command put and at long last delivered with send_with prior to the loop restarts.
With this particular create, now you can discover their bot on Telegram and deliver the /start and /greet directions, and see they in action.
To work on this, conserve the changes in Atom and operate ruby bot.rb within the terminal. Provided that terminal try open and running, their robot will be sending replies!