Table of Contents

  1. Prerequisites
  2. Deploy Your Facebook Bot with Python
  3. main.py script breakdown

Deploy Facebook Bot in Python

Let’s love every word (hmm) in the English language.

Here’s a small bash script to run your own Every Word Bot variant on Facebook. Instead of “fu*k+ word”, we’ll stick to “love+word.”

The script will install Python and necessary modules required to run the bot. You may bookmark this page as well. I will write more Facebook automation scripts in the near future.

Prerequisites

  1. Create a Facebook page
  2. Sign up with DigitalOcean or a cloud-provider of your choice, and set up a low-tier Ubuntu 20.04 instance.

You’ll get 100USD credit on DigitalOcean if you register with the link I shared.

Deploy Your Facebook Bot with Python

Proceed if you meet the requirements listed in prerequisites.

  1. Generate a non-expiring access token.

    maxbots.ddns.net/token is a tool designed to let you get an access token to allow a Facebook bot to autopost. You use this access token in your API requests for authentication purposes. The access token you receive never expires, unless your account or page gets zucced.

  2. Download fb-bot-starter v1.0 Bash script.

    fbs.sh is a Bash script I created to help you install Python, supporting modules, and set up the bot in action.

    Login to the server, and run wget https://verfasor.com/fbs.sh

  3. Make fbs.sh executable.

    In order to run a file directly, we’ll need to change the permissions to allow the script to be executable for the user. chmod is a command that changes permissions on a file, and +x will add execute rights to the script.

    Run chmod +x fbs.sh to make fbs.sh executable.

  4. Execute .fbs.sh.

    Type in ./fbs.sh and press enter.

  5. Enter the access token.

    Enter the access token when the script prompts you to do so.

Yep, that’s it. The script will create a tmux session named lewbot, which will run in the background and automate the page.

Why tmux?

tmux is a terminal multiplexer. The tool allows you to have multiple terminal commands and applications running visually next to each other without the need to open multiple terminal emulator windows.

nohup is another option, but I feel tmux is nice and sleek enough to run FB bots.

main.py script breakdown

Let’s have a look at the main script that powers the Facebook page.

The modules

import random
import facebook
import schedule
import time

random: to cut a line randomly from the words.txt and so on.

k = 1
filename = 'words.txt'
with open(filename) as file:
    lines = file.read().splitlines()

if len(lines) > k:
    random_lines = random.sample(lines, k)
    chosenline = "\n".join(random_lines)
    chosenlinestr = str(chosenline)
    message = "Love " + chosenlinestr 
    print(message)

    with open(filename, 'w') as output_file:
        output_file.writelines(line + "\n"
                               for line in lines if line not in random_lines)

facebook: to interact with the Facebook Graph API
schedule and time: No Shit, Sherlock.

Access Token

#FB Access Token = 'acesstoken'

Post to Facebook

#let's post it
def post():
    #Facebook stuff
    graph = facebook.GraphAPI(accesstoken)
    post_id = graph.put_object(parent_object='me', connection_name='feed', message = message)
    print(f"Submitted \"{message}\" on Facebook successfully!")
    

if __name__ == '__main__':
    schedule.every().hour.do(post).run()
    while 1:
        schedule.run_pending()
        time.sleep(1)

Tip Me

Support if you’d like by referring my resume or donating through PayPal.

Written by MighilMighil is an indie musician and tinkerer with diverse work experience in technology and writing. He has had the privilege of serving in various capacities, encompassing generalist and specialist roles. He is currently based in Chengdu.

Newsletter

Opt-in to receive long-form essays in your inbox. Unsubscribe anytime. Follow me on 𝕏 if you like.

Powered by DigitalOcean, BunnyCDN, WordPress.