Spaces:
Sleeping
Sleeping
imseldrith
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
-
from flask import Flask, request
|
2 |
import requests
|
3 |
from dotenv import load_dotenv
|
4 |
import os
|
5 |
|
|
|
6 |
load_dotenv()
|
7 |
|
8 |
app = Flask(__name__)
|
@@ -12,40 +13,52 @@ VERIFY_TOKEN = os.getenv('VERIFY_TOKEN')
|
|
12 |
PAGE_ACCESS_TOKEN = os.getenv('PAGE_ACCESS_TOKEN')
|
13 |
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
print(f"PAGE_ACCESS_TOKEN: {PAGE_ACCESS_TOKEN}")
|
18 |
-
print(f"OPENAI_API_KEY: {OPENAI_API_KEY}")
|
19 |
|
20 |
-
|
21 |
-
API = "https://graph.facebook.com/LATEST-API-VERSION/me/messages?access_token="+PAGE_ACCESS_TOKEN
|
22 |
-
|
23 |
-
@app.route("/", methods=['GET'])
|
24 |
def fbverify():
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
29 |
return "Hello world", 200
|
|
|
30 |
@app.route("/", methods=['POST'])
|
31 |
def fbwebhook():
|
32 |
data = request.get_json()
|
33 |
print(data)
|
|
|
34 |
try:
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
39 |
request_body = {
|
40 |
"recipient": {
|
41 |
"id": sender_id
|
42 |
},
|
43 |
"message": {
|
44 |
-
"text": "
|
45 |
}
|
46 |
}
|
47 |
-
response = requests.post(
|
48 |
-
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
if __name__ == '__main__':
|
51 |
-
app.run(host="0.0.0.0", port=7860, debug=True)
|
|
|
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__)
|
|
|
13 |
PAGE_ACCESS_TOKEN = os.getenv('PAGE_ACCESS_TOKEN')
|
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 |
+
if mode == "subscribe" and challenge:
|
26 |
+
if token == VERIFY_TOKEN:
|
27 |
+
return challenge, 200
|
28 |
+
else:
|
29 |
+
return "Verification token mismatch", 403
|
30 |
return "Hello world", 200
|
31 |
+
|
32 |
@app.route("/", methods=['POST'])
|
33 |
def fbwebhook():
|
34 |
data = request.get_json()
|
35 |
print(data)
|
36 |
+
|
37 |
try:
|
38 |
+
# Extract the message and sender ID from the request
|
39 |
+
messaging_event = data['entry'][0]['messaging'][0]
|
40 |
+
message = messaging_event.get('message', {})
|
41 |
+
sender_id = messaging_event['sender']['id']
|
42 |
+
|
43 |
+
# Check if the received message is "hi"
|
44 |
+
if message.get('text') == "hi":
|
45 |
request_body = {
|
46 |
"recipient": {
|
47 |
"id": sender_id
|
48 |
},
|
49 |
"message": {
|
50 |
+
"text": "Hello, world!"
|
51 |
}
|
52 |
}
|
53 |
+
response = requests.post(API_URL, json=request_body)
|
54 |
+
return jsonify(response.json()), response.status_code
|
55 |
+
|
56 |
+
except KeyError as e:
|
57 |
+
print(f"KeyError: {e}")
|
58 |
+
return "Invalid payload structure", 400
|
59 |
+
except Exception as e:
|
60 |
+
print(f"Exception: {e}")
|
61 |
+
return "Internal Server Error", 500
|
62 |
|
63 |
if __name__ == '__main__':
|
64 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|