InstaBot how to send multiple messages to same user Python?

Viewed 27

So ive tried this:

with open("rawtext.txt", encoding="utf8") as f:
lines = f.readlines()
for line in lines:
    bot.send_messages(line, ["REDACTED_USER"])

But the bot only sends the first line as a message (I'm using the instabot library). This is what is shown in the console log while running:

2022-09-23 16:54:00,259 - INFO - Going to send 1 messages. 100%|██████████| 1/1 [00:01<00:00, 1.52s/it] 2022-09-23 16:54:03,789 - INFO - Going to send 1 messages. 0%| | 0/1 [00:00<?, ?it/s]

1 Answers

There is a default delay in the bot if you send messages to users. This is to prevent a ban from Instagram. I think the creator of the bot has done this on purpose, so I would advise not to change this. To summarize:

  • The delay between messages is 5-10 minutes.
  • The maximum amount of messages per day is 50-100 minutes.

Below you'll find code references to support these findings.

  1. The random delay (source):

    message_delay=random.randint(300, 600)
    
  2. Maximum messages per day (source):

    max_messages_per_day=random.randint(50, 100),
    
  3. The delay calculation (source):

    def delay(self, key):
        """
        Sleep only if elapsed time since
        `self.last[key]` < `self.delay[key]`.
        """
        last_action, target_delay = self.last[key], self.delays[key]
        elapsed_time = time.time() - last_action
        if elapsed_time < target_delay:
            t_remaining = target_delay - elapsed_time
            time.sleep(t_remaining * random.uniform(0.25, 1.25))
        self.last[key] = time.time()
    
  1. The delay usage (source)

    def send_message(self, text, user_ids, thread_id=None):
        """
        :param self: bot
        :param text: text of message
        :param user_ids: list of user_ids for creating group or
        one user_id for send to one person
        :param thread_id: thread_id
        """
        user_ids = _get_user_ids(self, user_ids)
        if not isinstance(text, str) and isinstance(user_ids, (list, str)):
            self.logger.error("Text must be an string, user_ids must be an list or string")
            return False
    
        if self.reached_limit("messages"):
            self.logger.info("Out of messages for today.")
            return False
    
        self.delay("message")
        urls = self.extract_urls(text)
        item_type = "link" if urls else "text"
        if self.api.send_direct_item(
            item_type, user_ids, text=text, thread=thread_id, urls=urls
        ):
            self.total["messages"] += 1
            return True
    
        self.logger.info("Message to {user_ids} wasn't sent".format(user_ids=user_ids))
        return False
    
Related