Chatting on Facebook Using Python
Tags: python, devops, tutorial, discuss
Published: 2020-12-30 23:48:21

## How to send message to FB friend using Python.
In one of my articles I discussed how to login to facebook using python with the only help of selenium and Chromedriver.
Today, we will see how to send message to any of our Facebook friend using Python with the power of a library called selenium.
Should you have not read my previous article I will recommend you to read it [here](https://dev.to/maxwizardth/login-facebook-using-python-3bgk). Otherwise let's go into the lesson. 

firstly we need to install some libraries below :
 1. selenium
 2. webDriver_manager for chrome or geckodriver for firefox.
 
 if you already have the two libraries above then you are good to go.
 After installation of the two libraries above then we can start the code.
But before we move to the code let me try to explain some function we are going to use in the program.
### Some other requirements for the task
 * We need the  selenium here to login to Facebook by providing email and password
 * we will need the id of the freind or group that we want to send the message to.
 * the text to be send as message.

### How will you get your friend or group Id Number
 It might be a little difficult to get your friend facebook Id, calm down it is very simple take the following step
* go to [www.facebook.com](www.facebook.com) on your laptop and login to your facebook account.
* click `see more` at the leftside of the middle of your screen.
* Now stroll down and click message/messenger icon by the left and click on the friend you want to chat with.
* Now check the url of you browser you will see 15 digits with splash ending it, that is your friend Id number. like the following:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/q6b6iy1yr0120gs01umt.JPG)
so the 15 digits with splash will be input when you were ask to input your friend id e.g `100016740546536/`.

### Some functions needed 
* `find_element_by_id()` : A function provided by selenium module to find the element with the help of their id.
* `find_element_by_name()` : A function provided by selenium module 
 to find the element with the help of their name.
* `driver.find_element_by_xpath()` : A function provided by selenium module to find the element with their css selector.
*  `send_keys()` : A function provided by selenium module also to write data or text into the box we use
* `webdriver.Chrome()`: A function that will open new window of chrome
* `get()` : A function we will used to open up the Facebook website
* `quit()`: A function to close the browser when we are done.
* `sleep()` : A function to delay the running of the script for some seconds.

### Necessary modules to be import and important data
In our code we need to import the following from their lib.
* webdriver from selenium 
* sleep from time 
* ChromeDriverManager from webdriver_manager.chrome
* Options from selenium.webdriver.chrome.options 

Also you will need to provide your  username and password.
 
#### HERE IS THE CODE
```python
from selenium import webdriver
from time import sleep
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
usernam=str(input('Enter your username: '))
password=str(input('Enter your Password: '))
frndId = str(input('Enter your friend or Group Id: '))
message = str(input('Enter your text message here: '))
#this is where i open a new window
driver = webdriver.Chrome(ChromeDriverManager().install())
#this is where i open facebook
driver.get ('https://www.facebook.com/')
# this is where i start entering my username and password.
driver.find_element_by_id("email").send_keys(usernam)
driver.find_element_by_id('pass').send_keys(password)
driver.find_element_by_name("login").click() # I click login button
# login process done I am now on facebook
sleep(1)
# i start navigating to message and click on the friend i wanna messsage
mesgAdd='https://www.facebook.com/messages/t/'
mesgLink=mesgAdd+frndId
driver.get(mesgLink)
sleep (1)
#This is Where I clicked the Send Message '
driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').send_keys(message, Keys.ENTER)

# This is where I entered the keys and Did Enter
'''
this is how it should look like

Enter your username abdahi.oladejo.10
Enter your Password 9ad3e22
Enter your friend or Group Id100016740336536/
Enter your text message here how are you doing
'''
```
wait for some minutes and see your message being sent. Did you like it ? well that is not all. the amazing part of it is that you can manipulate your message anyway you like.
for example, let say we want to send the same message to a friend 50 times then we can multiply the message by 50 or we write a for loop for the message.

#### Example
```python
from selenium import webdriver
from time import sleep
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
usernam=str(input('Enter your username: '))
password=str(input('Enter your Password: '))
frndId = str(input('Enter your friend or Group Id: '))
message = str(input('Enter your text message here: '))
#this is where i open a new window
driver = webdriver.Chrome(ChromeDriverManager().install())
#this is where i open facebook
driver.get ('https://www.facebook.com/')
# this is where i start entering my username and password.
driver.find_element_by_id("email").send_keys(usernam)
driver.find_element_by_id('pass').send_keys(password)
driver.find_element_by_name("login").click() # I click login button
# login process done I am now on facebook
sleep(1)
# i start navigating to message and click on the friend i wanna messsage
mesgAdd='https://www.facebook.com/messages/t/'
mesgLink=mesgAdd+frndId
driver.get(mesgLink)
sleep (1)
#This is Where I clicked the Send Message '
msg=message*50
  driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').send_keys(msg, Keys.ENTER)
'''
you can use this for loop to do another fun
for msg in message:
    driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').send_keys(msg, Keys.ENTER)
# This is where I entered the keys and Did Enter
'''
#this is how it should look like

'''Enter your username abdahi.oladejo.10
Enter your Password 9ad3e22
Enter your friend or Group Id100016740336536/
Enter your text message here how are you doing
'''
```
did you see the message ? try to write for loop for the message and manipulate your message the way you want.
enjoy coding! see you in the next lesson where we will be discussing whatsapp chat with python. remember to like and comment below.