Spaces:
Sleeping
Sleeping
File size: 1,284 Bytes
67793ba 92b3530 38b2ac5 67793ba 38b2ac5 adf4e97 38b2ac5 67793ba 38b2ac5 67793ba 38b2ac5 67793ba 38b2ac5 97d51f3 38b2ac5 67793ba 38b2ac5 92b3530 38b2ac5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/', methods=['GET'])
def home():
return "Welcome to the chatbot!"
@app.route('/webhook', methods=['POST'])
def webhook():
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 = 'EAAHQjpHkOQQBO8d03L5jRIdplH7dOuYOFpTCYqVMU4QZA7HJbzP6pGICnCam6RF2zhfR7Hu8s7p5eKDH1Bjn84tm0ZBEA2CJCzu4UujsUUkGIZAuP7wNQVLs9OY34CsOixt5nkgi9zLPPFCGpAEEpo8dXZBXifQPOwGtma7FfWmP2AMpukODNfsfbv8SeA4K' # 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(port=7860, debug=True)
|