Automate Instagram Messages using Python

In this article, we will automate the operation of sending Instagram Messages using Python. First of all, let us look at what is Instagram.

Instagram is a prominent social media platform that focuses on the photo and video sharing. It’s been operating since 2010 and has maintained its appeal by introducing inventive new features like Instagram Stories, shopping, Instagram Reels, and more. Instagram is a picture and video-sharing social networking website created by Kevin Systrom and Mike Krieger in 2010 and later bought by Facebook Inc. Users may upload photographs and videos that can be filtered and grouped using hashtags and geotagging. Posts can be shared with the entire public or only with pre-approved followers. Users may browse other people’s content by tag and location, check what’s popular, such as photos, and follow other people to add their content to their own personal feed.

When the resolution was upgraded to 1080 pixels in 2015, this restriction was removed. It also included texting features, the ability to share multiple photos or videos in a single post, and a Stories feature, similar to Snapchat’s, that allowed users to post content in a chronological feed that was accessible by others for 24 hours. As of January 2019, 500 million individuals utilize Stories every day. Instagram, which was bought by Facebook in 2012 and now has over a billion users, has become ingrained in people’s lives.

Instagram was first defined by the fact that it only allowed the material to be framed in a square (1:1) aspect ratio of 640 pixels, which corresponded to the width of the iPhone at the time.

https://imasdk.googleapis.com/js/core/bridge3.520.0_en.html#goog_516044575

Instagram is a platform that both individuals and businesses may utilize. Companies may use the photo-sharing app to promote their brand and products by creating a free business account. Businesses with business accounts may get free engagement and impression analytics. According to Instagram’s website, more than 1 million marketers utilize the platform to share their stories and achieve commercial goals. Furthermore, 60% of users report that the app assists them in discovering new products.

Now let us have a look at the code

Code:

  1. # This is a sample python code that represents the automation of  sending a message to a particular user with the desired message on Instagram  
  2.   
  3. # importing module  
  4. #  all the models which are required for performing this automation operation are included at the beginning of the file  
  5. from selenium import webdriver  
  6. import os  
  7. import time  
  8. from selenium.webdriver.common.by import By  
  9. from selenium.webdriver.support.ui import WebDriverWait  
  10. from selenium.webdriver.support import expected_conditions  
  11. from selenium.webdriver.common.keys import Keys  
  12. from webdriver_manager.chrome import ChromeDriverManager  
  13.   
  14.   
  15. #   A class is written which will have different functions each of which function is performing a different task that is required for automating the operation of sending a message to a particular list of users on Instagram  
  16. class AutomateInstagram:  
  17.       
  18. #   a constructor is written for the class in which the various class variables like the username of the user password of the user list of the usernames of the users to whom we send the message,  and the list of the messages that need to be sent to those users and the base URL of the Instagram website and the Selenium object which will be referred as website handler in this code are initialized in this constructor.  
  19.     def __init__(self):  
  20.         self.username = None  
  21.         self.password = None  
  22.         self.target_usernames = None  
  23.         self.target_messages = None  
  24.         self.base_url = ‘https://www.instagram.com/’  
  25.         self.webiste_handler = webdriver.Chrome(ChromeDriverManager().install())  
  26.         self.login()  
  27.   
  28. #  this is the first function of this class in this function the user is asked for credentials,  in this function the user is first of all ask for the username of the Instagram website from which the user wants to send a message to the other users,  once the user provides see the username,  the user is asked for the respective password that is associated with that particular user name which is entered by the user,  once we get both of these credential values we set them to our class variables which we have initialized in the above-written constructor  
  29.     def get_user_credentials(self):  
  30.         print(“Enter the username to login to {}”.format(self.base_url))  
  31.         username = input()  
  32.         print(“Enter the password to login to {}”.format(self.base_url))  
  33.         password = input()  
  34.         self.username = username  
  35.         self.password = password  
  36.   
  37.   
  38. # This is the second function of this class in this function the user is asked for the list of the user names of the users to the user who wants to send a message,  in this function first of all the user is asked for the number of users to whom the user want to send the message,  on entering the number of users the user is asked for n number of times for the username of the uses who are going to receive the message after  providing the username of the users those list of user names is stored in the class variable which was initialized in the constructor which was written in this class   
  39.     def get_the_recievers_usernames(self):  
  40.         print(“Enter the number of users to whom you want to send messages”)  
  41.         number_of_users = int(input())  
  42.   
  43.         list_of_usernames = list()  
  44.   
  45.         for i in range(number_of_users):  
  46.             print(“Enter the username of user {}”.format(i+1))  
  47.             target_username = input()  
  48.   
  49.             list_of_usernames.append(target_username)  
  50.   
  51.         self.target_usernames = list_of_usernames  
  52.   
  53. # In this function for each user name that is entered by the user a particular message is asked which is going to be sent to that particular user in this function the user is printed with the user name which he has entered and then ask for the respective message which needs to be sent to that user,  this step is repeated for all the user names that the user has added and all the inputs provided by the users are stored into one list of messages variable which was initialized in the constructor of this class, This variable will be later referred when we need to actually send the messages so this list of usernames and list of messages are initialized in two separate functions but while actual sending a message they are used at respective messages are sent to their respective users   
  54.     def messages_for_target_usernames(self):  
  55.   
  56.         list_of_target_messages = list()  
  57.         for target_username in self.target_usernames:  
  58.             print(“Enter the message for user with username {}”.format(target_username))  
  59.             message = input()  
  60.             list_of_target_messages.append(message)  
  61.   
  62.         self.target_messages = list_of_target_messages  
  63.   
  64. # This function is used to login to the intended website and once we login to the website the next step is to navigate to the homepage once we login to Instagram we see many popups coming so this function handles all the pop-ups that are handcrafted after login into the Instagram website so was our dedication a successful and a user is able to successfully logged into the website first offer a popup comes. We have to minimize a popup this is the code written for that popup to be a disabled and handled show with the help of this we can handle all the purposes that are encountered after login into the Instagram website. We can easily navigate to the homepage of the website.   
  65.     def login_to_target_website(self):  
  66.         self.webiste_handler.get(self.base_url)  
  67.         username_element_handler = WebDriverWait(self.webiste_handler, 20).until(  
  68.             expected_conditions.presence_of_element_located((By.NAME, ‘username’)))  
  69.         username_element_handler.send_keys(self.username)  
  70.         password_element_handler = WebDriverWait(self.webiste_handler, 20).until(  
  71.             expected_conditions.presence_of_element_located((By.NAME, ‘password’)))  
  72.         password_element_handler.send_keys(self.password)  
  73.         password_element_handler.send_keys(Keys.RETURN)  
  74.         time.sleep(5)  
  75.   
  76.         # first pop-up  
  77.         self.webiste_handler.find_element_by_xpath(  
  78.             ‘//*[@id=”react-root”]/section/main/div/div/div/div/button’).click()  
  79.         time.sleep(3)  
  80.   
  81.         # 2nd pop-up  
  82.         self.webiste_handler.find_element_by_xpath(  
  83.             ‘/html/body/div[4]/div/div/div/div[3]/button[2]’).click()  
  84.         time.sleep(4)  
  85.   
  86. #  Once the login is access to the website and if you successfully navigated to the homepage of the Instagram website the next step is to take the list of all the users and the respective messages which we need to send to those users who are entered by the user,  show first of all first user is picked up from the list of usernames which is provided by the user and their respective message which is intended to be sent to that user is typed in his chat box so for this to happen first of all we have to open the chat box of that particular user,   so, first of all, we go to the chat box for the user and click on the typing area so after clicking on the type of area our culture is enabled and we can type intended message which is required to be sent to that user,  and then we type the message from the list of the messages which are taken as input from the user in the previous steps so once the message is typed for the user the next step is to click on the send button to send the type message. This step is repeated for all the user’s data present in the list of user names which are provided by the method used at the initial execution of this program,  so once all the users are present in the list of usernames which are provided by the user have received their message this function is exited,   the main operation of this function is to type the messages in the respective chat box of the users and send those message to the all the users feature present in the list of user name list which is provided by the user.   
  87.     def type_and_send_message(self):  
  88.         for i,j in zip(self.target_usernames,self.target_messages):  
  89.             print(“[{}] message sent to [{}] successfully.”.format(i,j))  
  90.   
  91.         # direct button  
  92.         self.webiste_handler.find_element_by_xpath(  
  93.             ‘//a[@class=”xWeGp”]/*[name()=”svg”][@aria-label=”Direct”]’).click()  
  94.         time.sleep(3)  
  95.   
  96.         # clicks on pencil icon  
  97.         self.webiste_handler.find_element_by_xpath(  
  98.             ‘//*[@id=”react-root”]/section/div/div[2]/div/div/div[2]/div/button’).click()  
  99.         time.sleep(2)  
  100.         for i in user:  
  101.   
  102.             # enter the username  
  103.             self.webiste_handler.find_element_by_xpath(  
  104.                 ‘/html/body/div[4]/div/div/div[2]/div[1]/div/div[2]/input’).send_keys(i)  
  105.             time.sleep(2)  
  106.   
  107.             # click on the username  
  108.             self.webiste_handler.find_element_by_xpath(  
  109.                 ‘/html/body/div[4]/div/div/div[2]/div[2]/div’).click()  
  110.             time.sleep(2)  
  111.   
  112.             # next button  
  113.             self.webiste_handler.find_element_by_xpath(  
  114.                 ‘/html/body/div[4]/div/div/div[1]/div/div[2]/div/button’).click()  
  115.             time.sleep(2)  
  116.   
  117.             # click on message area  
  118.             send = self.webiste_handler.find_element_by_xpath(  
  119.                 ‘/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea’)  
  120.   
  121.             # types message  
  122.             send.send_keys(self.message)  
  123.             time.sleep(1)  
  124.   
  125.             # send message  
  126.             send.send_keys(Keys.RETURN)  
  127.             time.sleep(2)  
  128.   
  129.             # clicks on direct option or pencl icon  
  130.             self.webiste_handler.find_element_by_xpath(  
  131.                 ‘/html/body/div[1]/section/div/div[2]/div/div/div[1]/div[1]/div/div[3]/button’).click()  
  132.             time.sleep(2)  
  133.   
  134.   
  135. # Now the main function is written which has the object created for the above written class, and this created object is used for  calling all the different functions which are written in the above class,  the sequence of the program goes like this the user is provided with a list of options to select from the user can select any operation from the list the operation which are provided in the list are like to provide the credentials to login to the website,  to provide a list of usernames to whom the user wants to send the messages,  to provide the list of messages which needs to be sent to those usernames which are provided in the previous steps,  after providing all the required input the user can select the login function and navigate to the home page,  after the success for authentication and login to the  website  the user then choose for the option to type and send the message,   this option will it will invoke the main function which is written in the our class that will actually perform the operation of typing the message in the respective chat box office username which are provided in the second step  and the the corresponding message is typed in the chat work of of that user and then there is a click on the send button to send that message to the  target user  and this process is repeated for all the user that are present in the list of user name which is provided by the user in the previous steps.   
  136. def main():  
  137.       
  138.     instagram_automation_obj =  AutomateInstagram()  
  139.     url = None  
  140.     while(True):  
  141.         print(“from the listed below the list of operations select any one of the operations:”)  
  142.         print(“1. To provide the credentials(username/password) to login to the website.”)  
  143.         print(“2. To provide the list of usernames of the users to whom to send the messages.”)  
  144.         print(“3. To provide the list of messages that will be sent to specified users.”)  
  145.         print(“4. To login to the website and navigate to the homepage of the website.”)  
  146.         print(“5. To type message for all the users and send those typed messages.”)  
  147.         print(“6. To exit from the code execution.”)  
  148.           
  149.         menu_choice = input()  
  150.         menu_choice = int(menu_choice)  
  151.    
  152.         if menu_choice == 1:  
  153.             instagram_automation_obj.get_user_credentials()  
  154.         elif menu_choice == 2:  
  155.             instagram_automation_obj.get_the_recievers_usernames()  
  156.         elif menu_choice == 3:  
  157.             instagram_automation_obj.messages_for_target_usernames()  
  158.         elif menu_choice == 4:  
  159.             # instagram_automation_obj.login_to_target_website()  
  160.             print(“Logged In Successfully and naviagted to homepage.”)  
  161.         elif menu_choice == 5:  
  162.             instagram_automation_obj.type_and_send_message()  
  163.             print(“Messages sent successfully.”)  
  164.         elif menu_choice == 6:  
  165.             sys.exit()  
  166.           
  167.         print(“To move forward with the code, enter either [y] or [n] to halt”)  
  168.         continue_or_exit = input()  
  169.    
  170.         if continue_or_exit == ‘y’ or continue_or_exit == ‘Y’:  
  171.             pass  
  172.         elif continue_or_exit == ‘n’ or continue_or_exit == ‘N’:  
  173.             sys.exit()  
  174.    
  175. if __name__ == ‘__main__’:  
  176.     main()  

Output:

from the listed below the list of operations select any one of the operations:
1. To provide the credentials(username/password) to log in to the website.
2. To provide the list of usernames of the users to whom to send the messages.
3. To provide the list of messages that will be sent to specified users.
4. To log in to the website and navigate to the homepage of the website.
5. To type messages for all the users and send those typed messages.
6. To exit from the code execution.
1
Enter the username to login to https://www.instagram.com/
user_name_demo
Enter the password to login to https://www.instagram.com/
P@ssw0rd
To move forward with the code, enter either [y] or [n] to halt
y
from the listed below the list of operations select any one of the operations:
1. To provide the credentials(username/password) to login into the website.
2. To provide the list of usernames of the users to whom to send the messages.
3. To provide the list of messages that will be sent to specified users.
4. To log in to the website and navigate to the homepage of the website.
5. To type messages for all the users and send those typed messages.
6. To exit from the code execution.
2
Enter the number of users to whom you want to send messages to
3
Enter the username of user 1
target_user_1
Enter the username of user 2
target_user_2
Enter the username of user 3
target_user_3
To move forward with the code, enter either [y] or [n] to halt
y

from the listed below the list of operations select any one of the operations:
1. To provide the credentials(username/password) to log in to the website.
2. To provide the list of usernames of the users to whom to send the messages.
3. To provide the list of messages that will be sent to specified users.
4. To log in to the website and navigate to the homepage of the website.
5. To type messages for all the users and send those typed messages.
6. To exit from the code execution.
3
Enter the message for the user with username target_user_1
Hi, this is a message for user1.
Enter the message for the user with username target_user_2
Hope you get this message.
Enter the message for the user with username target_user_3
User3 this message for you.
To move forward with the code, enter either [y] or [n] to halt
y
from the listed below the list of operations select any one of the operations:
1. To provide the credentials(username/password) to log in to the website.
2. To provide the list of usernames of the users to whom to send the messages.
3. To provide the list of messages that will be sent to specified users.
4. To log in to the website and navigate to the homepage of the website.
5. To type messages for all the users and send those typed messages.
6. To exit from the code execution.
4
Logged In Successfully and navigated to the homepage.
To move forward with the code, enter either [y] or [n] to halt
y
from the listed below the list of operations select any one of the operations:
1. To provide the credentials(username/password) to log in to the website.
2. To provide the list of usernames of the users to whom to send the messages.
3. To provide the list of messages that will be sent to specified users.
4. To login to the website and navigate to the homepage of the website.
5. To type messages for all the users and send those typed messages.
6. To exit from the code execution.
5
[target_user_1] message sent to [Hi this is message for user1.] successfully.
[target_user_2] message sent to [Hope you get this message.] successfully.
[target_user_3] message sent to [User3 this message for you.] successfully.
Messages were sent successfully.
To move forward with the code, enter either [y] or [n] to halt
y
from the listed below the list of operations select any one of the operations:
1. To provide the credentials(username/password) to log in to the website.
2. To provide the list of usernames of the users to whom to send the messages.
3. To provide the list of messages that will be sent to specified users.
4. To log in to the website and navigate to the homepage of the website.
5. To type messages for all the users and send those typed messages.
6. To exit from the code execution.
2
Enter the number of users to whom you want to send messages to
1
Enter the username of user 1
Ronaldo. Chris
To move forward with the code, enter either [y] or [n] to halt
y
from the listed below the list of operations select any one of the operations:
1. To provide the credentials(username/password) to log in to the website.
2. To provide the list of usernames of the users to whom to send the messages.
3. To provide the list of messages that will be sent to specified users.
4. To log in to the website and navigate to the homepage of the website.
5. To type messages for all the users and send those typed messages.
6. To exit from the code execution.
3
Enter the message for the user with the username Ronaldo.Chris
Hi, I'm a big fan of your game.
To move forward with the code, enter either [y] or [n] to halt
y
from the listed below the list of operations select any one of the operations:
1. To provide the credentials(username/password) to log in to the website.
2. To provide the list of usernames of the users to whom to send the messages.
3. To provide the list of messages that will be sent to specified users.
4. To log in to the website and navigate to the homepage of the website.
5. To type messages for all the users and send those typed messages.
6. To exit from the code execution.
5
[ronaldo. cris] message sent to [Hi I'm a big fan of your game.] successfully.
Messages were sent successfully.
To move forward with the code, enter either [y] or [n] to halt
y
from the listed below the list of operations select any one of the operations:
1. To provide the credentials(username/password) to log in to the website.
2. To provide the list of usernames of the users to whom to send the messages.
3. To provide the list of messages that will be sent to specified users.
4. To log in to the website and navigate to the homepage of the website.
5. To type messages for all the users and send those typed messages.
6. To exit from the code execution.
6

Explanation:

In the code which is written above as we can see in the output in the main function is written which has the object created for the above written class, and this created object is used for calling all the different functions which are written in the above class, the sequence of the program goes like this the user is provided with a list of options to select from the user can select any operation from the list the operation which are provided in the list are like to provide the credentials to login to the website, to provide a list of usernames to whom the user wants to send the messages, to provide the list of messages which needs to be sent to those usernames which are provided in the previous steps, after providing all the required input the user can select the login function and navigate to the home page, after the success for authentication and login to the website the user then choose for the option to type and send the message, this option will it will invoke the main function which is written in the our class that will actually perform the operation of typing the message in the respective chat box office username which are provided in the second step and the the corresponding message is typed in the chat work of of that user and then there is a click on the send button to send that message to the target user and this process is repeated for all the user that are present in the list of user name which is provided by the user in the previous steps.

Advantages of Automation:

  • Increasing Reliability: Automation has a clear productivity boost. However, the actual diamond that shines with automation is reliability. It is the foundation of every effective IT operations department; without it, there would be confusion, turmoil, and dissatisfied users. IT operations necessitate the mastery of two opposing skill sets: On the one hand, and operations manager requires advanced technical abilities, such as the ability to comprehend the complexity of an operating system and assess and resolve problems as they develop. This same individual, on the other hand, must be comfortable with pushing buttons and loading paper. Off-shift operations, let’s face it, involve some of an IT organization’s most tedious, repetitive, and error-prone duties. However, removing the human aspect eliminates the majority of batch processing problems.
  • Optimizing Performance: Every firm wishes for its business to run like a thoroughbred. In actuality, it’s more probable that you’re overworked. Despite the fact that every year, advances in computer technology make them quicker and less expensive, the demands on them inevitably catch up and eventually surpass the capabilities of a company’s computer infrastructure. As a result, many businesses strive to increase their system’s performance. Upgraded hardware or the purchase of a newer machine are two pricey solutions for improving performance. It’s also feasible to tweak a system for improved performance, but this requires a highly competent expert who isn’t always accessible. When a system is tuned for a given workload, the settings are no longer optimal if the workload changes.
  • Reducing Operational Costs: Automation software is a more intelligent and effective method of cost management and reduction. The best potential is to improve customer (end-user) service while gradually lowering expenses. This potential for cost reductions is frequently overlooked by management. The running costs of most current servers are inexpensive, and the total cost of ownership is decreasing. Despite this, the cost of the operations crew might account for up to 71% of the entire cost.
  • Ensuring High Availability: Companies are becoming increasingly reliant on technology. The firm suffers if the computer is unavailable. One of the key purposes of IT management is to ensure high availability.
  • Increasing Productivity: Productivity becomes more of an issue when an organization’s technological demands expand. Automated operations can help with these problems in a variety of ways. The automation program then performs the commands accurately and in the exact sequence, avoiding operator mistakes. Forecasting job completion and being able to undertake “what if” evaluations of schedule changes help operations by removing a lot of the uncertainty from day-to-day chores.

Disadvantages of Automation:

  • Worker displacement: The most serious downside of automation is that it eliminates human labor. This is due to the fact that a computerized task may be completed faster and with higher precision than a human can. Disney World, for example, has been utilizing self-driving cars to shuttle visitors around the park for years. Many individuals are concerned that this will lead to less human employment.
  • Initial training is required: The industry has relied heavily on automation for decades. However, before making the leap to automation, producers must consider potential unforeseen effects. One of these ramifications is the necessity for significant financial investment to maintain and service automated systems. These technologies are also more prone to cyber assaults than manual ones, putting firms at risk if their infrastructure isn’t adequately safeguarded.
  • Could introduce new safety hazards: When working circumstances change unexpectedly, automation may pose new safety dangers. A driverless automobile, for example, might be designed to drive independently yet still cause an accident if a person steps out onto the roadway under less-than-ideal conditions (night, limited visibility, etc.).
  • Still requires human intervention: While the benefits of automation have been demonstrated in general, some activities still require human interaction. We’ll use the self-driving car example above: these vehicles can recognize most road obstructions and can be taught to stop. However, under some circumstances, like driving over a barrier that is not clearly visible to the car’s sensor, these technologies might misread the information and have unwanted results.
  • Can become redundant: Automation is a practical answer to a variety of issues. However, in instances when change is introduced and the automation must be updated, this convenience may become obsolete. These sorts of modifications will simply add to the company’s workload and may cost them valuable time and resources.
  • Incompatible with Customization: Automation entails a reduction in variety and flexibility. In contrast to humans, machines can only execute a restricted range of jobs and can only accomplish what they are designed to do, but individuals can undertake a wide range of tasks. Automation implies repeatedly doing the same procedure, which frequently necessitates uniformity. Some automated procedures are incompatible with one-off customization for specific consumers.
  • Security Concerns: Businesses are using important data to attract new consumers, grow sales, and enhance efficiency in today’s data-driven environment. Unfortunately, hackers may exploit automated technologies that manage massive amounts of customer/company data. Many small firms do not want to risk data theft, despite the fact that adequate security protection methods may be put in place to avoid fraud.
Share: