With this in place, let’s get started. Grab the Telegram software for pc

With this in place, let’s get started. Grab the Telegram software for pc

Telegram is mostly a mobile software, but for development uses you’re probably want to have it mounted on equivalent machine you are really using for writing rule. This way, you can easily rapidly test that without unlocking the mobile anytime. And, you’ll have the ability to copy and paste their Telegram bot’s API key into the text editor.

Time to grab that API key!

Step 2: talk with the BotFather in order to get your API key

Telegram robot design is actually a novel militarycupid proce because a lot of it really is focused around your own connections with a Telegram robot. That bot is the BotFather. Inside your Telegram desktop computer app, you’ll want to search his username and start a discussion with your.

Supply the beginning order to BotFather by entering /start .

You’ll read a listing of directions which help you produce, revise, and control their spiders. Since it’s the first opportunity, you’ll want /newbot .

When compared to proce for developing a-twitter robot, a Mastodon bot, or any other method of robot I’ve tried, Telegram helps make the preliminary create a breeze. I gue that shows just what the means Telegram bots is generally!

After offering the /newbot demand, you’re able to pick a reputation and username for your robot. Title is exactly what your consumers will see the bot as with their unique contacts list, and login name was how they’ll believe it is. Think of the username like a-twitter handle; it has to getting special, therefore’s most useful if this’s brief and remarkable.

Thereupon done, you’ll get your own bot’s API trick. The API key was how Telegram understands the signal you compose try aociated using this robot. Every robot possesses its own API trick, and you ought ton’t show they with people or they might hijack your robot and then have they perform their unique bad deeds.

That concludes our very own speak to BotFather for the present time — onto the code!

Step three: installing the bot’s treasures and directory

Open terminal, and manage mkdir telegram-bot . This brings a fresh service to suit your bot’s data files. It’s far better have them restricted to 1 folder so it’s easy to regulate. Navigate to the directory site with cd telegram-bot and perform some following:

touch Gemfile touch bot.rb atom .

This produces two blank data, one for specifying the treasures you’ll want plus one where in actuality the bot’s signal will living. The past demand opens up these two files in Atom.

Click the Gemfile in Atom’s sidebar, and paste this amazing in:

This tells Bundler to seize the Ruby user interface on the Telegram API from rubygems.

To complete the gem build, go back to the terminal and kind bundle . Here’s what you should read:

That’s it when it comes down to gem create, today we’re at long last acquiring onto the rule.

Coding the first Telegram bot

The actual code which will become run continuously on the servers try inside bot.rb. It’s vacant at this time, but right here we’re going to link inside the Telegram treasure we just bundled and develop a bot.

It’s not much rule to create. By the time you’re completed, this is just what you’ll bring:

In Telegram, this is what the rule above really does:

Let’s see just what every part of the code does and create it as we get.

(substitute the TOKEN aided by the API token you copied from BotFather)

Ruby makes it rather easy to gue what code is going to do. The 3 traces above incorporate the Telegram bot features your document after which develop a unique bot object from Telegram robot cla, authenticated along with your token so the program knows locations to deliver the info.

The next component is certainly one large cycle. It appears to be complicated initially, nonetheless it’s very easy to pick aside.

The first line informs the robot to keep hearing for commands. And, whenever it obtains a command to pa they to the meage adjustable. The sets range logs the order your terminal so you can see what’s taking place whilst robot operates.

The bot’s impulse steps were stored in an instance statement. Your situation statement’s input is fed through from the meage adjustable after it’s become cleansed up by gem’s get_command_for process. The bot’s answer text is defined with respect to the demand retained and at long last delivered with send_with before the loop restarts.

With this particular build, you can now pick their bot on Telegram and deliver the /start and /greet commands, and watch it in action.

For this, rescue the alterations in Atom and operated ruby bot.rb inside terminal. Provided that terminal is actually available and operating, your robot will be sending responses!

Customizing their shiny latest bot

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 항목은 *(으)로 표시합니다