Last active
December 6, 2023 17:17
-
-
Save rilexz/29602cbefb59099fa81ebf80b2704764 to your computer and use it in GitHub Desktop.
Facebook bot website integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
run command in terminal | |
// | |
pip install -r requirements.txt | |
/* | |
Setting up first flask server | |
*/ | |
from flask import Flask | |
app = Flask(__name__) | |
@app.route("/") | |
def hello(): | |
return "Hello World!" | |
if __name__ == "__main__": | |
app.run() | |
/* | |
go-to root folder and run command | |
*/ | |
./ngrok http 5000 | |
/* | |
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 bots", 200 | |
/* | |
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 | |
/* | |
Method 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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment