Spaces:
Sleeping
Sleeping
File size: 1,606 Bytes
67793ba 7364bee 92b3530 7364bee 38b2ac5 67793ba 7364bee 38b2ac5 7364bee 67793ba 7364bee 67793ba 7364bee 97d51f3 7364bee 67793ba 38b2ac5 7364bee 38b2ac5 92b3530 7364bee |
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
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)
|