Thereupon set up, let’s get started. Install the Telegram application for pc
Telegram is mostly a cellular software, but also for developing needs you’re gonna want it installed on exactly the same machine you are really utilizing for composing signal. Because of this, you can easily quickly test that without unlocking your own mobile each time. And, you’ll have the ability to replicate and paste your own Telegram bot’s API key into your own book editor.
Time and energy to seize that API trick!
Step two: Chat with the BotFather to have their API secret
Telegram bot creation try an unique proce because most of truly based around your own relationships with a Telegram bot. That bot may be the BotFather. As part of your Telegram desktop app, you ought to browse his username and begin a discussion with your.
Allow the start demand to BotFather by entering /start .
You’ll read a summary of directions that help you make, revise, and manage their spiders. As it’s the first energy, you’ll desire /newbot .
Compared to the proce for building a-twitter bot, a Mastodon bot, or any other sort of bot I’ve attempted, Telegram makes the initial set-up super easy. We gue that shows exactly what a great device Telegram bots are!
After giving the /newbot command, you’re able to pick a reputation and login name for the robot. The name is exactly what their consumers will discover the bot as with their own contacts checklist, additionally the login name try how they’ll believe it is. Consider the login name like a Twitter handle; it has to end up being distinctive, plus it’s most useful when it’s quick and remarkable.
With this complete, you’ll be provided with your own bot’s API secret. The API secret try exactly how Telegram knows the code you create is aociated using this bot. Every bot features its own API key, and you need ton’t promote it with individuals or they may hijack the bot and possess they carry out their particular bad deeds.
That concludes all of our talk to BotFather for the present time — on the rule!
Step three: creating the bot’s treasures http://hookupwebsites.org/ldsplanet-review/ and index
Open terminal, and do mkdir telegram-bot . This brings a directory site for the bot’s files. It’s better to keep them confined to one folder therefore it’s an easy task to manage. Demand directory with cd telegram-bot after which do the following:
touch Gemfile touch bot.rb atom .
This creates two empty data, one for specifying the gems you’ll want plus one where the bot’s code will stay. The very last demand starts both these data in Atom.
Click on the Gemfile in Atom’s sidebar, and paste these in:
This informs Bundler to seize the Ruby interface to your Telegram API from rubygems.
To perform the gem setup, go back to their terminal and type package . Here’s what you need to see:
That’s it for the treasure build, now we’re at long last obtaining onto the signal.
Coding the first Telegram robot
The rule that will become run continuously regarding the machine is actually inside bot.rb. It’s vacant immediately, but here we’re browsing link inside the Telegram gem we just bundled and create a bot.
It’s not much rule to publish. Once you are really done, it’s this that you’ll has:
In Telegram, this is exactly what the rule above do:
Let’s view what every part of the signal really does and create it we run.
(exchange the TOKEN using the API token you duplicated from BotFather)
Ruby makes it simple to gue what signal perform. The 3 traces above put the Telegram bot features to your document then generate a new robot object through the Telegram robot cla, authenticated with your token therefore, the plan knows the best places to deliver the data.
Next role is but one huge circle. It looks complicated to start with, it’s an easy task to choose aside.
Initial range says to the bot keeping paying attention for commands. And, whenever it receives a command to pa it with the meage changeable. The places line logs the command to your terminal so you’re able to discover what’s taking place even though the robot works.
The bot’s reaction actions were kept in a situation statement. The outcome declaration’s feedback try fed through from meage changeable after it’s already been cleaned out right up from the gem’s get_command_for strategy. The bot’s answer text is placed according to order kept after which at long last delivered with send_with ahead of the loop restarts.
With this build, anybody can come across the robot on Telegram and deliver the /start and /greet commands, and watch it for action.
To work on this, save yourself the changes in Atom and operate ruby bot.rb for the terminal. Provided that terminal try available and working, their robot will send responses!