We are writing a Telegram bot with an application! (Python) / Hebrew

We are writing a Telegram bot with an application! (Python) / Hebrew

Today, we will write a Telegram Bot with an application. For example: Users fill out questionnaires and then send them to the Administrator.

For this we will use the pyTelegramBotAPI (telebot) library.

pyTelegramBotAPI is an easy and good library for writing Telegram Bot!

Installing the library

To install on Windows of the telebot library itself, we will have to open cmd or the terminal and write:

pip install pyTelegramBotAPI

To install on macOS you need to write:

pip3 install pyTelegramBotAPI

Start of code

First you need to get our robot token. To do this, go to @BotFather and create a bot. After you will receive a token and you need to insert it into a variable TOKEN which we will now create, must be inserted instead YOUR_TOKEN .

So, we first import the library itself:

import telebot

TOKEN = 'YOUR_TOKEN'
ADMIN_ID = 'YOUR_ID'

In variable TOKEN we will store our token received from BotFather

In variable ADMIN_ID we need to paste our id from telegram account. You can find out your ID through the robot @getmyid_bot

Now let’s write the bot’s greeting:

import telebot

TOKEN = 'YOUR_TOKEN'
ADMIN_ID = 'YOUR_ID'

bot = telebot.TeleBot(TOKEN)

name = None
username = None
user1_mess = None
user2_mess = None
user3_mess = None

@bot.message_handler(commands=['start'])
def start_message(message):
    global name, username
    name = message.from_user.first_name
    username = message.from_user.username
    bot.send_message(message.chat.id, "Привет! Это бот для заявок. Чтобы подать заявку, заполните анкеты")

bot.polling()

We have 4 variables. Variables name and username make global and in them you need to store the username of the user from the telegram and the name. Now let’s check if our bot is working. To do this, you first need to write at the end of the code:

bot.polling()

As we can see, the work is working!

Now let’s get down to the questionnaires:

import telebot

TOKEN = 'YOUR_TOKEN'
ADMIN_ID = 'YOUR_ID'

bot = telebot.TeleBot(TOKEN)

name = None
username = None
user1_mess = None
user2_mess = None
user3_mess = None


@bot.message_handler(commands=['start'])
def start_message(message):
    global name, username
    name = message.from_user.first_name
    username = message.from_user.username
    bot.send_message(message.chat.id, "Привет! Это бот для заявок. Чтобы подать заявку, заполните анкеты")
    form1(message)

And yes, in the function start_message at the end do not forget to write form1(message) which, after the greeting, takes us to the next function that we will create now.

Let’s write the first question:

def form1(message):
    bot.send_message(message.chat.id, "1. Как вас зовут?")
    bot.register_next_step_handler(message, form2)

Thanks to the code bot.register_next_step_handler(message, form2) the bot waits for the user to send a message, and after sending it, moves on to the next function form2, now we will create it

We make the second function:

def form2(message):
    global user1_mess
    user1_mess = message.text
    bot.send_message(message.chat.id, "2. Сколько вам лет?")
    bot.register_next_step_handler(message, form3)

We make a variable user1_mess global and change its value to message.text. It will store the user’s text. This variable stores the text entered in the previous function.

We also do it with the third function:

def form3(message):
    global user2_mess
    user2_mess = message.text
    bot.send_message(message.chat.id, "3. Укажите номер телефона:")
    bot.register_next_step_handler(message, result)

You probably noticed that in the code bot.register_next_step_handler(message, result) this time points to a function result. Let’s create it

We make a function results:

def result(message):
    global user3_mess
    user3_mess = message.text
    bot.send_message(message.chat.id, "Спасибо за заявку! Скоро наш менеджер свяжется с вами. Всего доброго!")
    admin_app(ADMIN_ID)

Here we just write the message and the code admin_app(ADMIN_ID) which casts to a function admin_app.

Let me remind you that Variables user3_mess, user2_mess and user1_mess store the texts entered in the previous function.

And yes, we create a function to send the entered user data to the Administrator/Manager

We make a function admin_app to send data to the Admin:

def admin_app(ADMIN_ID):
    ankets = f'''Новая заявка от {name}!
Имя: {user1_mess}
Возраст: {user2_mess}
Номер телефона: {user3_mess}

Telegram:
Name: {name}
Username: @{username}'''

    bot.send_message(ADMIN_ID, ankets)

We create a variable surveys where user questionnaires will be filled out and sent to the Admin. You remember that we created a variable ADMIN_ID? So, user applications will be sent to this account with this ID.

Do not forget to write at the end of the code:

bot.polling()

Full code:

import telebot

TOKEN = 'YOUR_TOKEN'
ADMIN_ID = 'YOUR_ID'

bot = telebot.TeleBot(TOKEN)

name = None
username = None
user1_mess = None
user2_mess = None
user3_mess = None


@bot.message_handler(commands=['start'])
def start_message(message):
    global name, username
    name = message.from_user.first_name
    username = message.from_user.username
    bot.send_message(message.chat.id, "Привет! Это бот для заявок. Чтобы подать заявку, заполните анкеты")
    form1(message)


def form1(message):
    bot.send_message(message.chat.id, "1. Как вас зовут?")
    bot.register_next_step_handler(message, form2)

def form2(message):
    global user1_mess
    user1_mess = message.text
    bot.send_message(message.chat.id, "2. Сколько вам лет?")
    bot.register_next_step_handler(message, form3)

def form3(message):
    global user2_mess
    user2_mess = message.text
    bot.send_message(message.chat.id, "3. Укажите номер телефона:")
    bot.register_next_step_handler(message, result)

def result(message):
    global user3_mess
    user3_mess = message.text
    bot.send_message(message.chat.id, "Спасибо за заявку! Скоро наш менеджер свяжется с вами. Всего доброго!")
    admin_app(ADMIN_ID)

def admin_app(ADMIN_ID):
    ankets = f'''Новая заявка от {name}!
Имя: {user1_mess}
Возраст: {user2_mess}
Номер телефона: {user3_mess}

Telegram:
Name: {name}
Username: @{username}'''

    bot.send_message(ADMIN_ID, ankets)

    
bot.polling()

We launch and check:

Hooray! Everything works great!

Also you can in a variable ADMIN_ID specify the id of your group, where you need to send messages about new applications (don’t forget to add the bot to the group).

Related posts