DEV Community

Cover image for Facebook ChatBot Using Flask
notHanii
notHanii

Posted on • Updated on

Facebook ChatBot Using Flask

first of all go here and login with your fb account
Also Install ngrok zip file

https://developers.facebook.com/
Enter fullscreen mode Exit fullscreen mode
https://ngrok.com/download
Enter fullscreen mode Exit fullscreen mode

Image description
Fill the instructions According to you
Image description

Image description
That's it Now We Do Some code
create a folder and using pip install these Modules

pip install pymessenger

pip install requests

pip install flask
Enter fullscreen mode Exit fullscreen mode

Open your Folder And make a file name
fbBot.py

from flask import Flask,request
import requests
from pymessenger import Bot

app =  Flask(__name__)

VERIFY_TOKEN = ''
PAGE_ACCESS_TOKEN =''
bot = Bot(PAGE_ACCESS_TOKEN)


def handling_message(text):
    adjusted_msg = text
    if adjusted_msg == 'hi' or adjusted_msg == 'Hi':
        response = 'hey'
    elif adjusted_msg == "what's up" or adjusted_msg == "what's up":
        response = "i'm Great"
    else:
        response = "it's pleasure to talk with you,Thank you."
    return response

@app.route('/', methods = ["POST","GET"])

def web_hook():
    if request.method == "GET":
        if request.args.get('hub.verify.token') == VERIFY_TOKEN:
            return request.args.get('hub.challenge')

        else:
                return 'Unable To Connect Meta'
    elif request.method == 'POST':
        data = request.json
        process = data['entry'][0]['messaging']
        for msg in process:
            text = msg['message']['text']
            sender_id = msg['sender']['id']
            response = handling_message(text)
            bot.send_text_message(sender_id,response)
        return 'Message Delivered'

    else:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8q5t43y3zd7kvdzdpfdp.png)


if  __name__ == '__main__':
    app.run()

Enter fullscreen mode Exit fullscreen mode

name anything about verify token
Generate page access token and paste above

Now Extract ngrok and paste it in your Workspace

Image description

run fbBot.py
and ngrok As An Administrator
put this cmd
and Hit Enter

ngrok.exe http 5000
Enter fullscreen mode Exit fullscreen mode

now you are connected with it
from here copy https that end with .io
and paste it here with token name and save

Image description
Restart your app Build again And You Are Good To Go

Top comments (0)