Enhancing a Telegram Bot for Sequential Instagram DMs to Multiple Users

Hey everyone! I’ve got a Telegram bot that uses Selenium to send Instagram DMs. Right now it only handles one username at a time. I want to upgrade it so it can take multiple usernames (separated by commas) and message them one after another.

Here’s what I’m aiming for:

  • Accept a list of Instagram usernames from the Telegram user
  • Send a message to each username in order
  • Keep the browser open until all messages are sent
  • Return to the DM page between each message

I’ve got the basic script working, but I’m not sure how to modify it for this new functionality. Any tips on how to loop through usernames? Or how to keep the browser session alive? Maybe there’s a smarter way to navigate back to the DM page between messages?

Here’s a simplified version of what I’m working with:

import telebot
from selenium import webdriver

bot = telebot.TeleBot('BOT_TOKEN')

@bot.message_handler(commands=['start'])
def handle_start(message):
    bot.reply_to(message, 'Send me Instagram usernames')

@bot.message_handler(func=lambda message: True)
def handle_message(message):
    usernames = message.text.split(',')
    driver = webdriver.Chrome()
    # Login to Instagram
    for username in usernames:
        # Navigate to user's DM
        # Send message
    driver.quit()
    bot.reply_to(message, 'Messages sent!')

bot.polling()

Any suggestions would be super helpful. Thanks!

hey there! i’ve dealt with similar stuff before. for looping through usernames, you could use a for loop like you’ve got. to keep the browser open, just move the driver.quit() outside the loop. for navigating back, try driver.get(‘https://www.instagram.com/direct/inbox/’) after each message. hope this helps!

I’ve implemented something similar for a client recently. Here’s what worked well for us:

For handling multiple usernames, we used a list comprehension to clean the input:
usernames = [u.strip() for u in message.text.split(‘,’) if u.strip()]

To keep the session alive, we implemented a custom wait_for_element function using WebDriverWait. This helped avoid timing issues and made the script more robust.

For navigation, we found it more reliable to use Instagram’s direct URL structure:
driver.get(f’https://www.instagram.com/direct/t/{username}')

We also added error handling for each user, so if one failed, it wouldn’t crash the entire process:

for username in usernames:
    try:
        # Send message logic here
        successful_sends.append(username)
    except Exception as e:
        failed_sends.append((username, str(e)))

# Report results back to user

This approach worked quite well, allowing for better feedback and reliability.

I’ve worked on similar projects, and here’s what I’ve found effective: For handling multiple usernames, use a list comprehension with error checking: usernames = [u.strip() for u in message.text.split(‘,’) if u.strip() and u.isalnum()]. To maintain the browser session, implement a custom WebDriverWait function. This helps avoid timing issues and increases script reliability. For efficient navigation between users, utilize Instagram’s URL structure: driver.get(f’https://www.instagram.com/direct/t/{username}'). Implement a try-except block for each user to prevent total script failure if one user causes an error, logging successes and failures separately. Consider adding a delay between messages to avoid triggering Instagram’s anti-spam measures, using time.sleep() or a more sophisticated rate-limiting approach. These modifications should significantly improve your bot’s functionality and reliability.