Trouble with Python Telegram Bot responding to messages from another bot

I’m working on two Telegram bots using Python. One bot collects user data and videos, then sends reports to a group chat. The other bot should reply to these reports. But I’m running into a problem.

The second bot can’t seem to respond to messages from the first bot. It only replies to messages I send or its own messages. I’ve tried disabling privacy mode in BotFather, but it didn’t help.

Both bots are set up as admins in the group chat. Has anyone else faced this issue? Any ideas on how to fix it?

Here’s a simplified version of what I’m trying to do:

def bot1_send_report(chat_id, report_data):
    message = bot1.send_message(chat_id, report_data)
    return message.message_id

def bot2_reply_to_report(chat_id, message_id):
    bot2.reply_to_message(chat_id, message_id, 'Report received')

# This part doesn't work as expected
report_message_id = bot1_send_report(group_chat_id, 'New report')
bot2_reply_to_report(group_chat_id, report_message_id)

Any help would be appreciated!

I’ve dealt with similar challenges in my Telegram bot projects. One effective solution I’ve found is to use Telegram’s webhook feature instead of long polling. With webhooks, you can set up a server endpoint that receives updates from Telegram in real-time, including messages from other bots.

To implement this, you’d need to:

  1. Set up a server with a public HTTPS endpoint.
  2. Use setWebhook method to configure your bot to send updates to this endpoint.
  3. Process incoming updates on your server, checking for messages from the first bot.

This approach allows Bot2 to react immediately to Bot1’s messages without relying on direct bot-to-bot communication. It’s more responsive and efficient than polling methods.

Remember to handle potential security concerns when exposing a public endpoint. Proper authentication and input validation are crucial.

hey alex, i’ve run into this before. bots can’t usually interact with each other directly. you might wanna try having bot1 send a special keyword or command that bot2 can pick up on. then bot2 can respond to that keyword instead of trying to reply directly to bot1’s message. hope that helps!

I’ve encountered a similar issue in my projects. One workaround I found effective is to use a shared database or message queue system. Instead of having Bot1 send messages directly to the group, it could write the report data to a database. Bot2 can then periodically check this database for new entries and post them to the group chat.

This approach decouples the bots and avoids the limitations of bot-to-bot interactions in Telegram. It also gives you more control over the timing and formatting of the reports.

Here’s a basic pseudocode to illustrate:

# Bot1
 def send_report(report_data):
     database.insert(report_data)

# Bot2
 def check_and_post_reports():
     new_reports = database.get_new_reports()
     for report in new_reports:
         bot2.send_message(group_chat_id, format_report(report))

# Run check_and_post_reports() periodically

This method has worked well for me in several projects. It might require a bit more setup, but it’s more reliable and scalable in the long run.