How to Set up a Facebook Messenger Bot to Deep Fry Images on Your Group Chats

It’s free and surprisingly easy

Ethan Roberts
The Startup

--

Photo by Wine Dharma on Unsplash

I’m in a meme group-chat on Facebook Messenger, and sometimes a situation arises (naturally) where an image just needs to be deep fried. In this circumstance we have to use an online service, taking valuable minutes out of the day. It’s this exact type of minor inconvenience that bots are perfect for solving.

Wouldn’t it be great, I thought to myself, if saying the phrase “Can I request a deep fry” triggers a bot to automatically deep fry the last image sent to the chat? Yes, yes it would.

(If you don’t know what deep frying refers to, please see this source)

This tutorial will walk you through the steps to get this project up and running from the code on my GitHub repo. I suggest pulling it down to your local machine to make this as easy as possible.

If you want more information about how the code actually works, see this partner article.

Getting the Tools

This project is written in Python, so you’ll need a way of running Python code locally.

We’ll be hosting this project on an AWS EC2 instance (for free!), and using a Docker container hosted on AWS ECR for easy deployment. You’ll need an AWS account for this, which is free, as well as the AWS CLI and Docker installed locally.

Obviously you’ll need a Facebook account too. I chose to create a new one as my main account has 2-factor authentication which slightly complicates the log in process.

The Code

The whole code is shown below. It’s surprisingly short isn’t it?

Let’s get started.

The first thing you’ll need to do is install the Python package dependencies. This is as simple as running the following command in your terminal/ command prompt from the project directory. This installs every package listed in the requirements file.

pip install -r src/requirements.txt

The next job is to find the thread ID for your group chat. In Facebook Messenger, all chats have a unique ID associated with them, called the thread ID. You’ll use this to let the bot know which group chat it should be operating on.

I’ve included a script in the repo that helps you find the ID for a given group chat name. Running that with your details should return the ID you’ll need for the next few parts.

Script for finding the thread ID of a group chat

Now that you have the ID for the target group chat, you can assign it to the t_id variable of main.py . While you’re there, fill in your Facebook login details. Please be careful with this script when you have your login information in it — make sure you’re not sharing the folder or uploading it anywhere public.

And that’s it! Running main.py from your IDE should just work! Feel free to have a play. You can change the trigger message and error messages to whatever you want.

Dockerizing

The problem with how it’s set up now is that it’ll only work while your PC is on and the script is running. You don’t want to have to have your PC on all the time do you?

To solve this, we can run it on an AWS EC2 instance. Think of it as your PC but running thousands of miles away, all the time. Before you can run it though, we need to get the code in a Docker container.

Run the following commands from your terminal (or command prompt) in the project directory:

docker build -t deep-fry-bot .

(that uses the Dockerfile to create a template (or image) of the code)

docker run deep-fry-bot

(that uses the Docker image just created and runs it)

You should see the exact same output as from running main.py directly, which means it’s time to move to AWS console.

Running on AWS

The first thing to do is to log into AWS console and go to ECR. This is where Amazon stores your Docker image and makes it easy to get your code into the EC2 instance.

Click ‘Create Repository’, call it deep-fry-bot and click ‘Create Repository’ at the bottom. You should now have one repository in ECR:

If this is your first time using the AWS CLI, you’ll need to get your credentials. Go to IAM -> Users and select your user. Then go to the Security Credentials tab.

You’re going to create an Access Key, which is a public-private key pair that Amazon can use to make sure you are allowed to access your account. So press that ‘Access Key’ button. You should get two long codes. Save them somewhere SECURE, you’ll need them later.

Now go back to your terminal and run the command aws configure . Copy and paste both keys as prompted, then just press enter for the other two values. You should now be set up to run AWS commands for that account.

It’s time to push your container image from your PC to ECR. Go back to ECR in the console, select deep-fry-bot and press ‘View Push Commands’. Run through steps 1, 3 and 4 (we’ve already done step 2 to verify it’s working).

This might take a while, so while it’s uploading we can create the virtual machine that’ll run this container.

Go to EC2 in the console and click ‘Launch Instance’.

  • Choose ‘Amazon Linux 2 AMI (HVM)’, which should be a free-tier eligible machine.
  • On the next page, choose ‘t2.micro’ (which should also be free-tier eligible)
  • You don’t need to configure anything else, so select ‘Review and Launch’ then ‘Launch’

AWS is now spinning up a virtual machine that you can use for, well, anything. But we’re gonna use it to run this project.

Once that’s booted up, we can get the Docker image onto the machine and run it. Go to ‘Running Instances’ in the EC2 console, select the instance and press ‘Connect’. Choose the ‘EC2 Instance Connect’ tab on the following screen and press connect again.

This should take you to a Linux command line. What’s crazy is that command line is running on a machine potentially thousands of miles away, and you can control it with your keyboard!

We need to get docker installed on the virtual machine, so copy and paste the following commands one at a time into the command line and let it do it’s thing — it might take a few minutes. (Credit)

sudo yum update -y
sudo amazon-linux-extras install docker
sudo service docker start
sudo usermod -a -G docker ec2-user

Now that that’s set up, we can just pull and run the container from ECR (it should be finished uploading from your PC by now).

You’ll need to run through the aws configure steps again on this command line, and then run step 1 from the push commands (aws ecr get-login-password… etc). You should now be authenticated to pull the image from ECR.

Find the URI of the image from ECR and copy/paste it into the following command.

docker pull <URI>

That should start downloading the image from ECR. Once that’s over with, it’s time for the moment of truth — running the container. That’s as simple as:

docker run <URI>

You should start to see the login messages appear, then say the print the word ‘listening’. All that’s left to do now is send the message ‘Can I request a deep fry’ to the right chat and it should send you a beautifully deep-fried image!

The deep fry bot in action

Rejoice!

Hopefully at this point you have a working bot! If not, please leave a comment or message me directly and I’ll be happy to help out as best as I can.

Check out my other project, where I used Google Cloud and Machine Learning to run a Twitter streaming Sentiment Analyser: https://towardsdatascience.com/tracking-dysons-public-image-using-46-166-tweets-68af509923a6

--

--