imseldrith commited on
Commit
67793ba
·
verified ·
1 Parent(s): adf4e97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -21
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
- # Debugging: Print the loaded environment variables
16
- print(f"VERIFY_TOKEN: {VERIFY_TOKEN}")
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)
 
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)