Code Monkey home page Code Monkey logo

how-to-build-a-chatbot's Introduction

How To Build A Chatbot - Part 1 ๐Ÿ”ฅ

Learn to build a facebook chatbot using Python and Flask

Part 2

Table of Contents

Create a Facebook Page

After you've created a page, click on this Link to create a Facebook App to access FB's developer's console.

Stuff to install and setup

Prereq: Make sure you have python installed on your laptop

Download Project Files

You can download the files here

Install Dependencies

Navigate into the downloaded folder using cd and once you reach the root folder run the following command in your terminal.

pip install -r requirements.txt

Setting up your first flask server

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

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

Run the following command, python hello_world.py

Open localhost:5000 in your browser. And you'll see "Hello World"

Now, go to your root folder and run the command below.

./ngrok http 5000

Now, the whole world can see "Hello World" if they use the link ;)

Code Breakdown

verify() method

This method is used only once. It's for Facebook to check if the link you've given it is valid or not.

@app.route('/', methods=['GET'])
def verify():

    if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"):
        if not request.args.get("hub.verify_token") == "test_token":
            return "Verification token mismatch", 403
        return request.args["hub.challenge"], 200

    return "Hello world", 200

webhook() method

This is the main method that recieves user's messages.

@app.route('/', methods=['POST'])
def webhook():

    # Recieve your package from facebook

    data = request.get_json()
    
    # print(data)
    
    if data["object"] == "page":

        for entry in data["entry"]:
            for messaging_event in entry["messaging"]:

                if messaging_event.get("message"):  # someone sent us a message

                    sender_id = messaging_event["sender"]["id"]        # the facebook ID of the person sending you the message
                    recipient_id = messaging_event["recipient"]["id"]  # the recipient's ID, which should be your page's facebook ID
                    message_text = messaging_event["message"]["text"]  # the message's text

                    print(sender_id)
                    print(message_text)

                    # responding to your user
                    send_message(sender_id, message_text)

                if messaging_event.get("delivery"):  # delivery confirmation
                    pass

                if messaging_event.get("optin"):  # optin confirmation
                    pass

                if messaging_event.get("postback"):  # user clicked/tapped "postback" button in earlier message
                    pass

    return "ok", 200

send_message() method

This is the method that sends messages back to Facebook.

def send_message(recipient_id, message_text):

    # Prepare your package

    params = {
        "access_token": "ADD_YOUR_PAGE_ACCESS_TOKEN"
    }
    headers = {
        "Content-Type": "application/json"
    }
    data = json.dumps({
        "recipient": {
            "id": recipient_id
        },
        "message": {
        	# add the text you want to send here
            "text": message_text
        }
    })

    # Send the package to facebook with the help of a POST request
    r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)

    # do this to check for errors
    if r.status_code != 200:
    	print("something went wrong")

Useful References

Here's another tutorial
Flask Documentation
read more abour ngrok
Read about get and most requests

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.