This guide will walk you through the process of setting up a chatbot that uses the OpenAI GPT-3 model to respond to messages on Facebook Messenger.
If you prefer to use a script instead of running the command, you can download the run_app.sh
script from this repository and run it using ./run_app.sh
.
wget https://raw.githubusercontent.com/kentemman/MessengerGPT/main/run_app.sh
chmod +x run_app.sh
./run_app.sh
Before getting started, you'll need:
- A Facebook Developer account
- A Facebook page for your chatbot
- An OpenAI API key
- Python 3 installed on your computer
- Flask and requests Python packages installed
- Go to the Facebook Developer portal and create a new app.
- Follow the steps to set up your app, including adding a Messenger product and linking it to your Facebook page.
- Generate a Page Access Token and keep it handy, you'll need it later.
- If you don't already have one, sign up for an account on the OpenAI website.
- Generate an API key for the GPT-3 model, and keep it handy.
- Create a new directory for your project and navigate to it in the terminal.
- Create a new Python file and call it
app.py
. - Paste the code from the original post into this file.
- Replace the OpenAI API key and Facebook Page Access Token with your own tokens.
- Install the Flask and requests Python packages by running pip install flask requests in the terminal.
- Start the Flask server by running python app.py in the terminal.
- Open a new terminal tab or window and navigate to the directory where you installed ngrok.
- Start ngrok by running the command:
./ngrok http 5000
- Note the "Forwarding" URL that is displayed in the ngrok console. This is the URL that you will use as your callback URL in the Facebook Developer portal.
- Go back to your Facebook Developer portal and navigate to your app's Messenger settings.
- Under the Webhooks section, click on the "Setup Webhooks" button.
- Enter
- Go to your Facebook page and send a message to your chatbot.
- Check the console of your Flask server to see the 3. input message and the response from OpenAI GPT-3.
- Check the Facebook Messenger conversation to see 5. the chatbot's response.
- Once you're happy with the chatbot's functionality, you can deploy it to a server so that it can run 24/7.
- There are many ways to deploy a Flask server, including using services like Heroku, AWS Elastic Beanstalk, or Google Cloud Run.
- Follow the instructions for your chosen deployment method to upload your Flask app and run it on a server.
- Change the
OPEN_AI_API
to your api - Change the
PAGE_TOKEN
to your page token
That's it! You now have a Facebook Messenger chatbot that uses OpenAI GPT-3 to generate responses. You can customize the chatbot's behavior by modifying the code in app.py.