imseldrith commited on
Commit
38b2ac5
·
verified ·
1 Parent(s): 97d51f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -57
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
- # Retrieve environment variables
12
- VERIFY_TOKEN = os.getenv('VERIFY_TOKEN')
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
- 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("/", methods=['POST'])
35
- def fbwebhook():
36
  data = request.get_json()
37
- print("Received data:", data)
38
 
39
- try:
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
- print(f"Sender ID: {sender_id}, Message: {message}")
 
 
46
 
47
- # Check if the received message is "hi"
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
- except KeyError as e:
63
- print(f"KeyError: {e}")
64
- return "Invalid payload structure", 400
65
- except Exception as e:
66
- print(f"Exception: {e}")
67
- return "Internal Server Error", 500
 
 
 
68
 
69
  if __name__ == '__main__':
70
- app.run(host="0.0.0.0", port=7860, debug=True)
 
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)