Spaces:
Sleeping
Sleeping
imseldrith
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,35 @@
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
-
import requests
|
3 |
-
from dotenv import load_dotenv
|
4 |
-
import os
|
5 |
-
|
6 |
-
# Load environment variables from .env file
|
7 |
-
load_dotenv()
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
15 |
-
|
16 |
-
# Define the API endpoint for sending messages
|
17 |
-
API_URL = f"https://graph.facebook.com/v13.0/me/messages?access_token={PAGE_ACCESS_TOKEN}"
|
18 |
-
|
19 |
-
@app.route("/facebook", methods=['GET'])
|
20 |
-
def fbverify():
|
21 |
-
mode = request.args.get("hub.mode")
|
22 |
-
token = request.args.get("hub.verify_token")
|
23 |
-
challenge = request.args.get("hub.challenge")
|
24 |
-
|
25 |
-
print(f"Verification request - mode: {mode}, token: {token}, challenge: {challenge}")
|
26 |
-
|
27 |
-
if mode == "subscribe" and challenge:
|
28 |
-
if token == VERIFY_TOKEN:
|
29 |
-
return challenge, 200
|
30 |
-
else:
|
31 |
-
return "Verification token mismatch", 403
|
32 |
-
return "Hello world", 200
|
33 |
|
34 |
-
@app.route(
|
35 |
-
def
|
36 |
data = request.get_json()
|
37 |
-
|
38 |
|
39 |
-
|
40 |
-
# Extract the message and sender ID from the request
|
41 |
-
messaging_event = data['entry'][0]['messaging'][0]
|
42 |
-
message = messaging_event.get('message', {})
|
43 |
sender_id = messaging_event['sender']['id']
|
|
|
44 |
|
45 |
-
|
|
|
|
|
46 |
|
47 |
-
|
48 |
-
if message.get('text') == "hi":
|
49 |
-
request_body = {
|
50 |
-
"recipient": {
|
51 |
-
"id": sender_id
|
52 |
-
},
|
53 |
-
"message": {
|
54 |
-
"text": "Hello, world!"
|
55 |
-
}
|
56 |
-
}
|
57 |
-
response = requests.post(API_URL, json=request_body)
|
58 |
-
print(f"Response Status Code: {response.status_code}")
|
59 |
-
print(f"Response Body: {response.text}")
|
60 |
-
return jsonify(response.json()), response.status_code
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
68 |
|
69 |
if __name__ == '__main__':
|
70 |
-
app.run(
|
|
|
1 |
from flask import Flask, request, jsonify
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
app = Flask(__name__)
|
4 |
|
5 |
+
@app.route('/', methods=['GET'])
|
6 |
+
def home():
|
7 |
+
return "Welcome to the chatbot!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
@app.route('/webhook', methods=['POST'])
|
10 |
+
def webhook():
|
11 |
data = request.get_json()
|
12 |
+
messaging_event = data['entry'][0]['messaging'][0]
|
13 |
|
14 |
+
if 'message' in messaging_event:
|
|
|
|
|
|
|
15 |
sender_id = messaging_event['sender']['id']
|
16 |
+
message_text = messaging_event['message'].get('text', '')
|
17 |
|
18 |
+
if message_text.lower() == 'hi':
|
19 |
+
response_text = 'hello'
|
20 |
+
send_message(sender_id, response_text)
|
21 |
|
22 |
+
return jsonify({'status': 'ok'})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
def send_message(recipient_id, message_text):
|
25 |
+
access_token = 'EAAHQjpHkOQQBO8d03L5jRIdplH7dOuYOFpTCYqVMU4QZA7HJbzP6pGICnCam6RF2zhfR7Hu8s7p5eKDH1Bjn84tm0ZBEA2CJCzu4UujsUUkGIZAuP7wNQVLs9OY34CsOixt5nkgi9zLPPFCGpAEEpo8dXZBXifQPOwGtma7FfWmP2AMpukODNfsfbv8SeA4K' # Replace with your Facebook Page Access Token
|
26 |
+
url = f'https://graph.facebook.com/v12.0/me/messages?access_token={access_token}'
|
27 |
+
headers = {'Content-Type': 'application/json'}
|
28 |
+
payload = {
|
29 |
+
'recipient': {'id': recipient_id},
|
30 |
+
'message': {'text': message_text}
|
31 |
+
}
|
32 |
+
requests.post(url, headers=headers, json=payload)
|
33 |
|
34 |
if __name__ == '__main__':
|
35 |
+
app.run(port=7860, debug=True)
|