Dooratre commited on
Commit
dee826f
·
verified ·
1 Parent(s): 79509c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -3
app.py CHANGED
@@ -1,11 +1,19 @@
1
  from flask import Flask, request
2
  import requests
 
3
  from qwen import get_qwen_response # Import the Qwen response function
4
 
5
  app = Flask(__name__)
6
 
7
- # Configuration
8
- AI_SERVICE_URL = "https://aoamrnuwara.pythonanywhere.com/send-message"
 
 
 
 
 
 
 
9
  AI_SERVICE_TOKEN = "123400"
10
 
11
  @app.route("/webhook", methods=["GET"])
@@ -24,6 +32,13 @@ def handle_message():
24
  body = request.get_json()
25
 
26
  for entry in body["entry"]:
 
 
 
 
 
 
 
27
  for messaging_event in entry["messaging"]:
28
  if "message" in messaging_event and "text" in messaging_event["message"]:
29
  sender_id = messaging_event["sender"]["id"]
@@ -39,7 +54,8 @@ def handle_message():
39
  }
40
  payload = {
41
  "recipient_id": sender_id,
42
- "message_text": ai_response
 
43
  }
44
  requests.post(AI_SERVICE_URL, json=payload, headers=headers)
45
 
 
1
  from flask import Flask, request
2
  import requests
3
+ import json
4
  from qwen import get_qwen_response # Import the Qwen response function
5
 
6
  app = Flask(__name__)
7
 
8
+ # Load page credentials from token.json
9
+ try:
10
+ with open('token.json', 'r') as f:
11
+ PAGE_CREDENTIALS = {page['app_id']: page['page_token'] for page in json.load(f)['pages']}
12
+ except FileNotFoundError:
13
+ print("Error: 'token.json' file not found. Please create it and add page credentials.")
14
+ PAGE_CREDENTIALS = {}
15
+
16
+ AI_SERVICE_URL = "https://your-pythonanywhere-domain.com/send-message"
17
  AI_SERVICE_TOKEN = "123400"
18
 
19
  @app.route("/webhook", methods=["GET"])
 
32
  body = request.get_json()
33
 
34
  for entry in body["entry"]:
35
+ page_id = entry["id"]
36
+ page_token = PAGE_CREDENTIALS.get(page_id)
37
+
38
+ if not page_token:
39
+ print(f"Page ID {page_id} not found in credentials. Skipping...")
40
+ continue
41
+
42
  for messaging_event in entry["messaging"]:
43
  if "message" in messaging_event and "text" in messaging_event["message"]:
44
  sender_id = messaging_event["sender"]["id"]
 
54
  }
55
  payload = {
56
  "recipient_id": sender_id,
57
+ "message_text": ai_response,
58
+ "page_access_token": page_token # Include page-specific token
59
  }
60
  requests.post(AI_SERVICE_URL, json=payload, headers=headers)
61