Fb-chatbot / app.py
imseldrith's picture
Update app.py
7364bee verified
raw
history blame
1.61 kB
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
VERIFY_TOKEN = 'imseldrith' # Replace with your verification token
@app.route('/', methods=['GET'])
def home():
return "Welcome to the chatbot!"
@app.route('/webhook', methods=['GET', 'POST'])
def webhook():
if request.method == 'GET':
# Webhook verification
verify_token = request.args.get('hub.verify_token')
challenge = request.args.get('hub.challenge')
if verify_token == VERIFY_TOKEN:
return challenge
else:
return 'Error, wrong validation token'
elif request.method == 'POST':
data = request.get_json()
messaging_event = data['entry'][0]['messaging'][0]
if 'message' in messaging_event:
sender_id = messaging_event['sender']['id']
message_text = messaging_event['message'].get('text', '')
if message_text.lower() == 'hi':
response_text = 'hello'
send_message(sender_id, response_text)
return jsonify({'status': 'ok'})
def send_message(recipient_id, message_text):
access_token = 'YOUR_PAGE_ACCESS_TOKEN' # Replace with your Facebook Page Access Token
url = f'https://graph.facebook.com/v12.0/me/messages?access_token={access_token}'
headers = {'Content-Type': 'application/json'}
payload = {
'recipient': {'id': recipient_id},
'message': {'text': message_text}
}
requests.post(url, headers=headers, json=payload)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=7860, debug=True)