Tutorial: a Facebook Bot for beginners

WARNING!: the usage of this bot could lead to a Facebook profile permanent banning and end up in the Facebook Jail. Only use this tutorial for learning purposes, and never try to use it for spamming or web scrapping with your real account or any other account.

Photo by Alex Knight on Unsplash

- Step 1: Installing Ruby

Before starting our attempt to write code, we have to install our language. In this case, as we have said, we are going to use Ruby. Installing it is a pretty easy-to-follow task, but make sure you follow each step carefully and copy and paste every command line to prevent typos.

Ruby on Windows 10 Tutorial

- Step 2: Installing a Text Editor

As you might know, code is text ordered in a way the computer can understand it, and the way you arrange your code (syntax) is going to depend on the programming language you are using. However, the text editors we use commonly like Microsoft Word and Libre-Office-Writer are great for writing documents, but these won’t be useful for writing code. We need a completely different set of features in our editors such as plugins, syntax highlighting, auto-closing of brackets and braces, and linting. At this step, we are going to install an excellent free code editor VSCode (Visual Studio Code).

  • Open VSCode and access your folder: File > Open Folder > -Select your folder- > OK.
    As your folder is empty, you won’t see anything in your VSCode UI but the folder’s name.
Your folder’s name is on the left side of the screen.
Save your bot.rb file
Save your bot.rb file
Saving my file as bot.rb (the .rb extension is important)
File extension check .rb with the icon

- Step 3: using Ruby gems to access Facebook

  • Mozilla Firefox
    At this step, we are going to use the Firefox web browser. If you haven't installed it yet, you can access this link and follow the instructions.
  • What’s a gem?
    In general terms, a gem is a collection of Ruby methods and functionalities that we can extract into a base code.
Terminal displayed at the bottom of the screen.
gem install selenium-webdriver
Command-line pasted into the terminal with the correct path.
Terminal after installing the selenium-webdriver gem.
require “selenium-webdriver”
driver = Selenium::WebDriver.for :firefox
url = ‘https://www.facebook.com/'
driver.get(url)
Code saved in my ruby file.
ruby bot.rb
Command-line to run the ruby file.
Facebook page after running our bot’s file.

- Step 4: Facebook login

After having automatized the process of accessing Facebook, it’s time to teach our bot how to log in. For this subject, it’s better to understand a crucial term so you can use it for future projects.

Copy Xpath of an element demonstration.
Xpath tutorial.
email = ‘yourname@email.com’
password = ‘yourpassword’
def login(email, password, driver)
email_element = driver.find_element(xpath: ‘//*[@id=”email”]’)
password_element = driver.find_element(xpath: ‘//*[@id=”pass”]’)
email_element.clear()
email_element.send_keys(email)
password_element.send_keys(password)
password_element.submit
sleep(5)
endlogin(email, password, driver)
Facebook login code without real email and password.
ruby bot.rb
Put the element Xpath inside the single-quotes, replacing its content.

- Step 5: Access to Facebook groups

IMPORTANT: make sure you are already a member of every group you want the bot to post. Otherwise, your bot could not find any field to write on.

[‘https://www.facebook.com/groups/275366519230814’, ‘https://www.facebook.com/groups/rordevelopers’, ‘https://www.facebook.com/groups/javascript.morioh’]
The array of links.
def openGroups(groups, driver)
groups.each do | group |
driver.get(group)
sleep(3)
end
end
openGroups(groups, driver)
OpenGroups function created.

- Step 6: Publish a post on every Facebook group

  • We need to find the Xpath of the element where we want the bot to click in order to start writing on every group (a text field). At the moment I am writing this tutorial, the group's text field before click for posting looks like:
Facebook text field for posting in a group.
field = driver.find_element(xpath: ‘your element xpath here’)
Field Xpath inside the OpenGroups function.
Get the Xpath of the post button.
def WriteAndPost(field, text, driver)
begin
field.click
sleep(3)
driver.action.send_keys(text).perform
sleep(2)
driver.find_element(xpath: ‘/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[4]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[3]/div[2]/div[1]/div[1]’).click
rescue
sleep(2)
end
end
text = ‘write the content of your posts here’
Text variable below groups.

Congratulations!

You can run your bot now and see how it automatically posts on every Facebook group you have pasted inside the array!
It is the way a bot works. Of course, there are a lot more functionalities you can implement to your bot; however, with the topics we have reviewed in this tutorial, I bet you can build your bot to automatize an astonishing process on the internet.

I'm a full-stack web developer and visual creator. I'm always trying to do my best at the things I'm passionate about. I love you all!

Get the Medium app