M
M
mirindanda2021-10-11 18:26:30
Bots
mirindanda, 2021-10-11 18:26:30

How to make the bot respond only when it is called?

How to make a telegram bot answer only when it is accessed by a keyword, but view all correspondence at the same time?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pozner88, 2021-10-11
@pozner88

What is meant by viewing all correspondence?
If you need to do by keyword, then you can add either a handler to a command or a condition and to which it should respond.
Handler can be done like this

@dp.message_handler(commands=['reg'])
    async def enter_reg(message: types.Message):
        await message.answer('Вы начали регистрацию в боте.\n'
                             'Укажите ваше имя')

On the mandrel / reg, he will write that registration has begun.
Or for a specific word
@dp.message_handler(content_types='text')
    async def new_message(message: types.Message):
        print(message.text)
        if message.text == 'привет':
            await bot.send_message(message.from_user.id, 'введено ключевое слово')

the code is written for aiogram, for other libraries it will be possible to redo it

R
Ruslan Mandzyuk, 2021-10-11
@Ryslan_13

Just write hello to the bot and he will answer you,
but don't forget to enter the token

from aiogram import types, Dispatcher, executor, Bot

TOKEN = " "

bot = Bot(TOKEN)

dp = Dispatcher(bot)

@dp.message_handler(text='hello') # text='hello' Здесь укажи на какой текст должен отвечать бот
async def start(message: types.Message):
    await bot.send_message(message.chat.id,  'jojo')

if __name__ == '__main__':
    executor.start_polling(dp)

The only thing I did not understand is what it means to view the entire correspondence

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question