Spaces:
Sleeping
Sleeping
imseldrith
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,8 @@
|
|
1 |
-
from flask import Flask, request
|
|
|
2 |
from dotenv import load_dotenv
|
3 |
import os
|
4 |
-
from helper.openai_api import chat_completion
|
5 |
-
from helper.messenger_api import send_message, set_typing_on, set_typing_off
|
6 |
|
7 |
-
# Load environment variables
|
8 |
load_dotenv()
|
9 |
|
10 |
app = Flask(__name__)
|
@@ -19,54 +17,35 @@ print(f"VERIFY_TOKEN: {VERIFY_TOKEN}")
|
|
19 |
print(f"PAGE_ACCESS_TOKEN: {PAGE_ACCESS_TOKEN}")
|
20 |
print(f"OPENAI_API_KEY: {OPENAI_API_KEY}")
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
@app.route('/webhook', methods=['POST'])
|
38 |
-
def handle_messages():
|
39 |
-
body = request.get_json()
|
40 |
-
print('Incoming request body:', body) # Log the entire request body
|
41 |
-
|
42 |
-
if not body.get('entry') or not body['entry'][0].get('messaging'):
|
43 |
-
return 'Invalid payload', 400
|
44 |
-
|
45 |
-
messaging_event = body['entry'][0]['messaging'][0]
|
46 |
-
sender_id = messaging_event['sender']['id']
|
47 |
-
message_text = messaging_event['message'].get('text')
|
48 |
-
|
49 |
-
if not sender_id or not message_text:
|
50 |
-
return 'Invalid message', 400
|
51 |
-
|
52 |
-
set_typing_on(sender_id)
|
53 |
-
|
54 |
-
result = chat_completion(message_text)
|
55 |
-
response_text = result.get('response', '')
|
56 |
-
|
57 |
try:
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
70 |
|
71 |
if __name__ == '__main__':
|
72 |
-
app.run(host="0.0.0.0", port=7860, debug=True)
|
|
|
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__)
|
|
|
17 |
print(f"PAGE_ACCESS_TOKEN: {PAGE_ACCESS_TOKEN}")
|
18 |
print(f"OPENAI_API_KEY: {OPENAI_API_KEY}")
|
19 |
|
20 |
+
# This is API key for facebook messenger.
|
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 |
+
if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"):
|
26 |
+
if not request.args.get("hub.verify_token")== "imseldrith":
|
27 |
+
return "Verification token missmatch", 403
|
28 |
+
return request.args['hub.challenge'], 200
|
29 |
+
return "Hello world", 200
|
30 |
+
@app.route("/", methods=['POST'])
|
31 |
+
def fbwebhook():
|
32 |
+
data = request.get_json()
|
33 |
+
print(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
try:
|
35 |
+
# Read messages from facebook messanger.
|
36 |
+
message = data['entry'][0]['messaging'][0]['message']
|
37 |
+
sender_id = data['entry'][0]['messaging'][0]['sender']['id']
|
38 |
+
if message['text'] == "hi":
|
39 |
+
request_body = {
|
40 |
+
"recipient": {
|
41 |
+
"id": sender_id
|
42 |
+
},
|
43 |
+
"message": {
|
44 |
+
"text": "hello, world!"
|
45 |
+
}
|
46 |
+
}
|
47 |
+
response = requests.post(API, json=request_body).json()
|
48 |
+
return response
|
49 |
|
50 |
if __name__ == '__main__':
|
51 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|