Home>

I have a question/problem, i have a bot with command 'hug' But when I use this command on someone in the chat, he does not see it and ignores it, it only works if they answer the message of the bot itself. Help what to do? Here is the code:

import logging
from aiogram import Bot, Dispatcher, executor, types
API_TOKEN= 'token'
# Configure logging
logging.basicConfig(level=logging.INFO)
# Initialize bot and dispatcher
bot= Bot(token=API_TOKEN)
dp= Dispatcher(bot)
@dp.message_handler(commands=['help'])
async def send_welcome(message: types.Message):
  await message.reply("Hi!\nI'm a test bot!\nI was made for hugs!")
@dp.message_handler(commands=['hug'], commands_prefix='!?./')
async def hug(message):
  await message.reply(f'@{message.from_user.username} hugged @{message.reply_to_message.from_user.username}')
if __name__== '__main__':
  executor.start_polling(dp, skip_updates=True)
  • Answer # 1
    @dp.message_handler(commands=['hug'], commands_prefix='!?./')
    async def hug(message: types.Message):
      await message.reply(f'@{message.from_user.username} hugged @{message.from_user.username}')
    

    First of all, you made a mistake in creating the function:

    async def hug(message):
    

    Without specifying the type of parameter with which they later wanted to work:

    async def hug(message: types.Message):
    

    After you usedreply_to_messageactually in order for the bot to "hug" only after you answer the bot's message (it seems logical)

    await message.reply(f'@{message.from_user.username} hugged @{message.reply_to_message.from_user.username}')
    

    Of course this will not work as you want, the solution is:

    await message.reply(f'@{message.from_user.username} hugged @{message.from_user.username}')
    

    You can use different names (I'm talking about those that in your case will "hug" the user), in my fix, the user hugs the user. Correct in the name of the bot, I think it will not be difficult for you.