0

Writing a bot for tech support.When I send a question, the bot forwards it to the channel and starts forwarding it too. And so it goes on endlessly.

Example(User-user who sent the bot a question, Bot1-bot who answers the user, Bot2-bot who writes in the channel)

P.S. Bot1 and Bot2 are the same bot, written to make it easier to understand

  • User: /start
  • Bot1: Hi! I am a technical support bot. Describe your problem and I will pass it on to the operators.
  • User: describe his problem
  • Bot1: reply to the message The message has been forwarded to the operators. Wait for a reply.
  • Bot2: Sends a message to the channel
  • Bot2:In the comments to the post The message has been forwarded to the operators. Wait for a reply.
  • Bot2: Sends a message to the channel
  • Bot2: In the comments to the post The message has been forwarded to the operators. Wait for a reply.

.........

What to do, I don't understand? Maybe FSM? Help me, please.

import logging
from aiogram import Bot, Dispatcher, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage

logging.basicConfig(level=logging.INFO)

bot = Bot(token='TOKEN')
storage = MemoryStorage()
dp = Dispatcher(bot, storage=storage)

@dp.message_handler(commands=['start'])
async def start_command(message: types.Message):
    await message.reply('Hi! I am a technical support bot. Describe your problem and I will pass it on to the operators.')

@dp.message_handler()
async def forward_to_operators(message: types.Message):
    bot_info = await bot.get_me()
    if message.from_user.id != bot_info.id:
        await bot.forward_message(chat_id='ID', from_chat_id=message.chat.id, message_id=message.message_id)
        await message.reply('The message has been forwarded to the operators. Wait for a reply.')

@dp.channel_post_handler()
async def handle_operators_reply(message: types.Message):
    if message.chat.id == 'ID':
        user_id = message.reply_to_message.forward_from.id
        await bot.send_message(chat_id=user_id, text=message.text)

if __name__ == '__main__':
    from aiogram import executor
    executor.start_polling(dp, skip_updates=True)

I have added a check for the message is not the bot's own

@dp.message_handler()
async def forward_to_operators(message: types.Message):
    bot_info = await bot.get_me()
    if message.from_user.id != bot_info.id:
        await bot.forward_message(chat_id='ID', from_chat_id=message.chat.id, message_id=message.message_id)
        await message.reply('The message has been forwarded to the operators. Wait for a reply.')

It didn't work, the bot kept answering endlessly and continued to do so

mousetail
  • 7,009
  • 4
  • 25
  • 45
agfn
  • 29
  • 2

0 Answers0