This blog shows how to utilize Facebook Messenger Chatbot, Flask, and ChatGPT together to provide text messages with detailed responses.

Step 1: Create a Facebook Chatbot

We must create the Facebook Messenger Chatbot in order to automate the messages with ChatGPT. Please follow the steps from step 1 to step 8 in our blog post on the Facebook Messenger Bot. Before continuing, please make sure you followed the blog’s instructions.

Step 2: ChatGPT API 

You can check out our blog WhatsApp Chatbot With ChatGPT Step 2 “ChatGPT API” which has instructions for setting up and using ChatGPT.

Step 3: Integrate ChatGPT API with Flask Application

It’s time to integrate our ChatGPT API with the Flask application now that it has been successfully installed.

The flask application that we have created in step 1 has to be modified now. To receive ChatGPT’s response to user messages, replace the existing code in your Flask app with the following code.

import requests
import os, signal
from flask import Flask, request
import openai
app = Flask(__name__)

#This is API key for OpenAI
openai.api_key = '<YOUR OPENAI API KEY>'
# This is page access token that you get from facebook developer console.
PAGE_ACCESS_TOKEN = '<Your PAGE ACCESS TOKEN>'
# This is API key for facebook messenger.
API="https://graph.facebook.com/LATEST-API-VERSION/me/messages?access_token="+PAGE_ACCESS_TOKEN


@app.route("/", methods=['POST'])
def fbwebhook():
    data = request.get_json()
    try:
        if data['entry'][0]['messaging'][0]['sender']['id']:
            message = data['entry'][0]['messaging'][0]['message']
            sender_id = data['entry'][0]['messaging'][0]['sender']['id']
            chat_gpt_input=message['text']
            print(chat_gpt_input)
            completion = openai.ChatCompletion.create(
                            model="gpt-3.5-turbo",
                            messages=[{"role": "user", "content": chat_gpt_input}])          
            response = completion['choices'][0]['message']['content']
            print("ChatGPT Response=>",chatbot_res)
            return chatbot_res
    except Exception as e:
        print(e)
        pass
    return '200 OK HTTPS.'
if __name__ =='__main__':
    app.run()

Note:

Run the flask application in the terminal: python SCRIPT_NAME.py
Run the ngrok on terminal: ngrok http 5000

Step 4: Test Facebook Messenger Chatbot 

If you visit your Facebook page, you’ll be able to test the bot by sending it a message. If you have everything correctly set up, you should get a message back from your page.

After sending a message on a bot you will receive a response from ChatGPT on the server as shown in below

When a user sends a message, the created page is successfully delivering them a ChatGPT response.

Do let us know if you face any issues in setting up or using the script. We would be happy to help! Contact us or post your query in the comments.

Also, check out our other tutorials to learn how to build a ChatGPT chatbot on different platforms.

WhatsApp with ChatGPT: Build An Automated, AI-Powered WhatsApp Chatbot With ChatGPT Using Flask

Telegram Bot with ChatGPT: Build An Automated, AI-Powered Telegram Chatbot With ChatGPT Using Flask

Slack Chatbot with ChatGPT: Build An Automated, AI-Powered Slack Chatbot With ChatGPT Using Flask

We have already created a blog post series where we have provided a tutorial on how to create a chatbot for different platforms. You can explore these blogs and learn how you can set different kinds of rich responses which can increase user engagement on your chatbot.

Telegram: Create Telegram Bot Using Python Tutorial With Examples

Facebook Messanger: Create Facebook Messenger Bot Using Python Tutorial With Examples

Slack: Create Slack Bot Using Python Tutorial With Examples

Discord: Create Discord Bot Using Python Tutorial With Examples

Dialogflow ES API: How To Integrate Dialogflow ES API To Add NLP Capabilities In Your Chatbot?

Dialogflow CX API: How to integrate Dialogflow CX API to add NLP capabilities in your Chatbot?

Dialogflow ES with Twilio: Setup Twilio Integration On Your Server To Connect Google Dialogflow ES

Categories: Chatbots Development ChatGPT Facebook Messenger Natural Language Processing NLP Python

Leave a Reply

Your email address will not be published.

You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*