Telegram Bot is your new assistant


Create a bot in telegram.

Create a bot in telegram.

Good afternoon everyone, in today’s column I want to touch on a rather interesting topic in terms of programming, which will be of interest even to ordinary users. These are Telegram bots. I have been creating them for about three years now, and today I would like to tell you how useful it can be, and if this material becomes interesting to you, then in the future I could delve into this topic.

Let’s start with the basics and terminology. A Telegram bot is a set of code that, during activation, acts as a user and its functionality depends only on the abilities and needs of its creator. Everyone can use the Telegram bot. The main thing is to know its name for the search engine.

To date, there are countless Telegram bots. Creating your own is not a problem. Development can be either through special services that, according to the given parameters, will create the necessary technological solution for you to work with, or with the help of self-written code, but for this you really need to know the basics of programming languages ​​such as Python, Java or C ++.

I want to dwell on the second option in more detail, because for me it is always more interesting to create my own. If you wish, there is a huge amount of material on the Internet that will help you make it more workable, and in this article I will only show the basics.

So, from long lyrics to business! In order to create your own bot, you need a Telegram account. Next, in the search bar, you need to find the main bot @BotFather. This is your personal manager and the main general of your bots. Its functionality includes the registration of bots, their removal, the creation of an avatar and the name of the bot. Write the /start command to this bot, after that you will activate the “Father” Bot.

(Bot Father)

(Bot Father)

The next important step in creating your bot is choosing a programming language.

In my case it will be Python. It is on it that I will demonstrate to you how easy and simple it is to create the future. To write the code, I will use the PyCharm developer tool.

We create a project inside the tool. In a file with the “.py” extension, we will write the code.

This article now moves on to the code demo and my comments on the case. Let’s define the functionality of the bot in the same way. Let’s say this bot will be able to answer simple phrases (Hi, record, bye) and moreover, if you write the word “record”, he will tease you and write “record” in response.

Step 1 Libraries

import telebot

# The library is needed in order to use the capabilities and functions of the Telegram bot.

Step 2. Key

bot = telebot.TeleBot(‘your unique key to Father Bot’)

#In the bot variable, we enter the key that @Botfather gave us. This is necessary to register it on the network, and also so that any user can use it.

Step 3. First function

@bot.message_handler(commands=[‘start’])

def start_message(message):

# Functions allow you to structure the work of your code and divide the possibilities into blocks.

Step 4. Reply to the user

bot.send_message(message.chat.id, ‘Hello’) # instead of hello, you can write any word or even a sentence.

! Important Telegram can send up to 4000 characters in one message. If your text exceeds the limit, then PyCharm will give you an error.

If in the future you need to send a large text, then it can, as an option, be divided into several components: bot.send_message(message.chat.id, ‘TEXT’).

Step 5. Dialogue

@bot.message_handler(content_types=[‘text’]) # Denote what we will write to the bot now

def send_text(message): #Function of accepting text from the user.

if message.text.lower() == ‘hello’: # If your text is “hello”

bot.send_message(message.chat.id, ‘Hello friend’) # the bot will send you “hello friend”

elif message.text.lower() == ‘record’: # If your text is “record”

bot.send_message(message.chat.id, message.text) #message text will copy your message and forward it to you.

elif message.text.lower() == ‘bye’: # If your text is “Bye”

bot.send_message(message.chat.id, ‘Be happy friend!’) # the bot will send you “’Be happy friend!”

else: # Applies if the entered variant does not match the words: hello, entry, bye.

bot.send_message(message.chat.id, ‘I’m stupid I don’t understand you!’)

Step 6. Finishing

bot.polling() #this part of the code must be at the end of your Telegram bot code. Formally, this is like a dot at the end of a sentence, which clearly states: the functionality of the bot is over.

Now that all the code is in the project, let’s run it. After activation, if you did everything correctly, then when you write the / start command to your bot (do not forget to find it in the list of bots), it will write you “Hello” (the line that is responsible for this 🙂 then try writing different commands and words. There is a chance that your bot may stop working and in order for it to resume in PyCharm, it needs to be restarted. The rule works here: the simpler your bot, the faster it will break.

Conclusion.

This was only an introductory article in the study of bots. If you like this material, please distribute it among your friends and relatives, it may be of interest to them. If you are interested in continuing to create a bot in telegram, write comments, I will be happy to write a continuation. I hope that this material will be known to as many people as possible.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *