All Projects → vdthatte → How-To-Build-A-Chatbot

vdthatte / How-To-Build-A-Chatbot

Licence: other
Learn to build a facebook chatbot using Python and Flask

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to How-To-Build-A-Chatbot

Chatblocks
Declarative Messenger chatbot framework
Stars: ✭ 48 (+220%)
Mutual labels:  facebook, chatbot, messenger, messenger-bot
Messengerbot
Python client for Facebook Messenger Platform Bot
Stars: ✭ 140 (+833.33%)
Mutual labels:  facebook, messenger, messenger-bot
facebook-send-api-emulator
Facebook Messenger Emulator & Facebook Send API Emulator functionality allowing you to test web hooks on developer's machine.
Stars: ✭ 24 (+60%)
Mutual labels:  facebook, chatbot, messenger
Messenger Bot Rails
Ruby on Rails Gem for the Facebook Messenger Bot Platform
Stars: ✭ 64 (+326.67%)
Mutual labels:  facebook, chatbot, messenger-bot
messenger
💬 A PHP library for Facebook Messenger
Stars: ✭ 53 (+253.33%)
Mutual labels:  facebook, messenger, messenger-bot
Pymessager
Python API to develop chatbot on Facebook Messenger Platform
Stars: ✭ 580 (+3766.67%)
Mutual labels:  facebook, chatbot, messenger
Magento Chatbot
Magento Chatbot Integration with Telegram, Messenger, Whatsapp, WeChat, Skype and wit.ai.
Stars: ✭ 149 (+893.33%)
Mutual labels:  facebook, chatbot, messenger
React Messenger Customer Chat
React component for messenger customer chat plugin
Stars: ✭ 221 (+1373.33%)
Mutual labels:  facebook, messenger, messenger-bot
Fbmessenger Node
FB messenger for node
Stars: ✭ 52 (+246.67%)
Mutual labels:  facebook, chatbot, messenger
Swiftybot
How to create a Telegram, Facebook Messenger, and Google Assistant bot with Swift using Vapor on Ubuntu / macOS.
Stars: ✭ 247 (+1546.67%)
Mutual labels:  facebook, messenger, messenger-bot
facebook-messenger
Go (GoLang) package for Facebook Messenger API and Chat bot
Stars: ✭ 62 (+313.33%)
Mutual labels:  facebook, chatbot
Tkkeyboardcontrol
TKKeyboardControl adds keyboard awareness and scrolling dismissal (like iMessages app) to any view with only 1 line of code for Swift.
Stars: ✭ 110 (+633.33%)
Mutual labels:  facebook, messenger
Fb Botmill
A Java framework for building bots on Facebook's Messenger Platform.
Stars: ✭ 67 (+346.67%)
Mutual labels:  facebook, chatbot
Jbot
Make Slack and Facebook Bots in Java.
Stars: ✭ 1,148 (+7553.33%)
Mutual labels:  facebook, messenger-bot
Botkit
Botkit is an open source developer tool for building chat bots, apps and custom integrations for major messaging platforms.
Stars: ✭ 10,555 (+70266.67%)
Mutual labels:  facebook, chatbot
React Messenger
Chat UX components built with React, inspired by Facebook Messenger
Stars: ✭ 167 (+1013.33%)
Mutual labels:  facebook, messenger
Rubotnik
"Bot-end" Ruby framework to quickly build and launch a Facebook Messenger chatbot
Stars: ✭ 191 (+1173.33%)
Mutual labels:  facebook, messenger
Messenger Platform Postman Collection
A delicious Postman collection for all your Messenger Platform needs.
Stars: ✭ 150 (+900%)
Mutual labels:  facebook, messenger-bot
Messenger For Desktop
This is not an official Facebook product, and is not affiliated with, or sponsored or endorsed by, Facebook.
Stars: ✭ 2,180 (+14433.33%)
Mutual labels:  facebook, messenger
Node Fb Messenger
✉️ Facebook Messenger Platform Node.js API Wrapper
Stars: ✭ 206 (+1273.33%)
Mutual labels:  facebook, messenger

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].