In this article we'll show how to achieve functionality shown in the above image.
You will need:
- Raspberry Pi connected to the internet.
- Dataplicity installed. If you need some help to get up to speed, see Getting started with Dataplicity.
Above is a block diagram illustrating how we set up a web application running on a Raspberry Pi, made available to a Facebook application webhook via dataplicity Wormhole.
Step 1: CREATE a FACEBOOK ACCOUNT
This will be the administrator account for the application that we'll implement in the next steps. If you would like to use your own account or already have a separate account then you can skip this step.
To register simply fill in the registration page with your details.
Step 2: CREATE a FACEBOOK PAGE
A Facebook "Page" is not the same as your personal Facebook timeline - it's a page normally created to represent a group, company, or other more organised presence on Facebook. This will be the front end for our application.
To create a page click on "Create Page" in Pages section in the menu on the left.
In the next window you'll be asked what type of page you would like to create. Choose Brand or Product.
When you click on Get started you will be asked to fill in some info about the page. You can either fill it in or skip most of it.
Step 3: CREATE a FACEBOOK APP AND SET UP a WEBHOOK
Go to https://developers.facebook.com/ and log in using the administrator account you created in the first step of this article.
Click on My apps field beside your thumbnail in the top right corner. This will direct you to the page containing your apps. Click on the green button Create a new app. Following that a window will pop up asking to fill in some details. Fill all the details and click on Create App ID.
On the next page click on + Add Product in the menu on the left. Click on Get Started with Webhooks then go back to a previous page and do the same for Messanger.
As you click on get started with Webhooks you will be asked to add a new subscription on the next page.
Step 4: DOWNLOAD THE CODE
The Raspberry Pi code is available on github.
To download it onto your Pi please use the command below:
To unpack the code from zip archive.
It is important to download the code at this step in the article because the next steps require a few minor modifications to parts of this code.
Step 5: SET UP THE SERVER
After unpacking the code files please open your terminal emulator in the same folder. Run the server using command below.
sudo python server.py TermPassword
TermPassword - This is the password we use to prevent other people on Facebook from controlling your Pi. You will need it when you start terminal mode inside the Facebook chat window. We recommend you pick your own password instead of the one we use in this example.
At this point it is important that you already have dataplicity installed on your device and Wormhole enabled. See Getting Started with Dataplicity for help to get to this point.
When you run the server, you should get a message indicating that the server is starting and then that it's running. To check that it's working correctly please go to your wormhole address inside the browser and you should see "Hello world".
This indicates that the server is functioning correctly. You can stop the server for now by pressing CTRL + C.
The next step is to edit facebook.py file. Open it with your favourite editor such as nano.
Inside that file modify the SUBSCRIPTION_TOKEN variable at the top by setting it to any string you want. We'll set it to:
SUBSCRIPTION_TOKEN = "tok_dp_fb_term"
save the file and close it.
Run the server again.
Step 6: SUBSCRIBE YOUR WEBHOOK TO THE SERVER
Return to your Facebook Developer page, and in the menu on the left side go to PRODUCTS section and click on Webhooks. Click the green button in top right corner that says New Subscription and from the drop-down menu select page. Next a form will pop up - fill it in as follows:
(Has to be the same as your SUBSCRIPTION_TOKEN inside the facebook.py file)
Click on Verify and Save. On the terminal emulator you should see your server receive a GET request. If everything was done correctly you should now see the following subscription on your developers page.
Step 7: GET STARTED WITH FACEBOOK MESSENGER
Back on your developers page in the menu on the left side go to PRODUCTS section and click on Messanger. Click the blue Get Started button.
You will now see a couple of sections on the next page. The ones that we're interested in are Token Generation and Webhooks.
In Token Generation sections click on Select a page button and from the drop-down menu select your page. You will get a pop up window asking for permissions and whether you want to continue as current user. Click on OK for everything.
In Webhooks sections click on Select a page button and from the drop-down menu select your page and click the blue Subscribe button.
Copy the Page Access Token.
You can stop the server for now by pressing CTRL + C. The next step is to edit facebook.py file. Open it with your favourite editor such as nano.
Inside that file modify the ACCESS_TOKEN variable at the top by setting it to the string you've just copied from Page Access Token field.
ACCESS_TOKEN = "EAAI3ZCiM ... lbNbj4pDrL6XQ1u9 ... VvQgm0"
save the file and close it.
Run the server again and proceed to the next step.
sudo python server.py TermPassword
Step 8: MAKE YOUR APPLICATION PUBLIC
Return to the Facebook Developer site, and in the menu on the left side go to the App review section.
Click on the sliding button to make the app public. After you do this, you will be able to send messages to your Facebook Page (ie Raspberry Pi) from accounts other than the administrator account.
Step 9: RUN IT!
Try sending some messages to your Pi from the Facebook page you have created! Let's send a help command to see other commands that we can use in our messages.
Other than help we have 3 other commands available:
- date - Returns current date on your Raspberry Pi.
- time - Returns current time on your Raspberry Pi.
- term <password> - Enters into terminal mode. (The date and time commands above are used in normal mode)
Below is an example of a response to running date and time commands. We can also see that in normal mode when you send a command that's not recognized it will be echoed back with appropriate response.
The terminal is started by sending:
(replace TermPassword with the password you set when you started the server on your Pi)
It then enters into the terminal mode that starts a shell on the Raspberry Pi and runs it until exit command is received. The exit command is part of the shell rather than our program, hence it's not on the above list of commands.
Step 10: A WORD ON SECURITY
Please note the security of your Pi terminal in this project is down to the complexity of the password you choose. It is also a project we just built for fun and almost certainly has bugs so please use with caution.
If you find any issues please comment them here or if you want you can contribute to our github. Any further suggestions how to improve this project will be much appreciated.
Step 11: EXPANDING THE PROJECT
There are lots of things we could add at this point such as:
- Create a GPIO Mode to switch on/off GPIO pins and switch on/off LEDs.
- Use Facebook Messenger to send files directly to the Pi.
- Add some AI to the chat while in normal mode.