Updating Telegram Bot to Message Multiple Instagram Users in Sequence

Hey everyone, I need some help with my Telegram bot project. Right now, it sends a message to one Instagram user at a time using Selenium, but I’d like to update it.

I’m aiming to:

  • Allow users to enter several Instagram usernames separated by commas in Telegram.
  • Have the bot send messages to each username one after the other.
  • Keep the browser open until every message has been sent.

Currently, the browser shuts down after each message. I’m not sure how to modify the script to maintain an active session for all users, and I’m curious if there’s an effective method to return to the direct messaging screen between messages.

Any ideas or suggestions would be very helpful!

As someone who’s worked extensively with Instagram bots, I can offer some practical advice. First, definitely keep that WebDriver instance outside your messaging loop - it’ll save you a ton of headaches with session management. For handling multiple usernames, I’d suggest using a queue data structure. It’s perfect for processing items sequentially and allows easy error handling if a message fails to send.

One trick I’ve found useful is implementing a small delay between messages. This helps avoid triggering Instagram’s anti-spam measures. Also, consider adding a try-except block around your messaging function to catch and log any errors without crashing the entire bot.

Lastly, if you’re frequently returning to the DM screen, it might be worth creating a separate function for that navigation. You could even cache certain page elements to speed up the process. Just remember to periodically refresh these cached elements to ensure they’re still valid.

I’ve tackled a similar project before, and I can offer some insights. To keep the browser open for multiple messages, you’ll want to restructure your code to create a single WebDriver instance outside of your messaging loop. This way, you can reuse the same session for all users.

For returning to the DM screen between messages, you can store the URL of the Instagram direct message page and use driver.get() to navigate back after each message is sent. Alternatively, you could locate and click on the DM icon element to return to the inbox.

To handle multiple usernames, split the input string by commas and iterate through the resulting list. Make sure to implement proper error handling and timeouts to deal with potential issues like rate limiting or unresponsive pages.

Remember to respect Instagram’s terms of service and avoid spamming users, as this could lead to your bot being blocked.

hey there! i’ve done smthing similar before. u could try using a for loop to iterate thru the usernames. keep the webdriver instance outside the loop so it stays open. use driver.get() to go back to DM page after each msg. just watch out for insta’s rate limits, they can be a pain sometimes lol