imseldrith commited on
Commit
80da867
·
verified ·
1 Parent(s): 1e4a631

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -3
app.py CHANGED
@@ -4,6 +4,7 @@ import requests
4
  app = Flask(__name__)
5
 
6
  VERIFY_TOKEN = 'imseldrith' # Replace with your verification token
 
7
 
8
  @app.route('/', methods=['GET'])
9
  def home():
@@ -39,6 +40,9 @@ def webhook():
39
 
40
  print(f"Received message from {sender_id}: {message_text}")
41
 
 
 
 
42
  if message_text.lower() == 'hi':
43
  response_text = 'hello'
44
  print(f"Sending response to {sender_id}: {response_text}")
@@ -46,11 +50,13 @@ def webhook():
46
  else:
47
  print(f"No action taken for message: {message_text}")
48
 
 
 
 
49
  return jsonify({'status': 'ok'})
50
 
51
  def send_message(recipient_id, message_text):
52
- access_token = 'EAAHQjpHkOQQBO8d03L5jRIdplH7dOuYOFpTCYqVMU4QZA7HJbzP6pGICnCam6RF2zhfR7Hu8s7p5eKDH1Bjn84tm0ZBEA2CJCzu4UujsUUkGIZAuP7wNQVLs9OY34CsOixt5nkgi9zLPPFCGpAEEpo8dXZBXifQPOwGtma7FfWmP2AMpukODNfsfbv8SeA4K' # Replace with your Facebook Page Access Token
53
- url = f'https://graph.facebook.com/v12.0/me/messages?access_token={access_token}'
54
  headers = {'Content-Type': 'application/json'}
55
  payload = {
56
  'recipient': {'id': recipient_id},
@@ -60,5 +66,27 @@ def send_message(recipient_id, message_text):
60
  response = requests.post(url, headers=headers, json=payload)
61
  print(f"Message send response: {response.status_code}, {response.text}")
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  if __name__ == '__main__':
64
- app.run(host="0.0.0.0", port=7860, debug=True)
 
4
  app = Flask(__name__)
5
 
6
  VERIFY_TOKEN = 'imseldrith' # Replace with your verification token
7
+ ACCESS_TOKEN = 'EAAHQjpHkOQQBO8d03L5jRIdplH7dOuYOFpTCYqVMU4QZA7HJbzP6pGICnCam6RF2zhfR7Hu8s7p5eKDH1Bjn84tm0ZBEA2CJCzu4UujsUUkGIZAuP7wNQVLs9OY34CsOixt5nkgi9zLPPFCGpAEEpo8dXZBXifQPOwGtma7FfWmP2AMpukODNfsfbv8SeA4K' # Replace with your Facebook Page Access Token
8
 
9
  @app.route('/', methods=['GET'])
10
  def home():
 
40
 
41
  print(f"Received message from {sender_id}: {message_text}")
42
 
43
+ # Set typing on
44
+ set_typing_on(sender_id)
45
+
46
  if message_text.lower() == 'hi':
47
  response_text = 'hello'
48
  print(f"Sending response to {sender_id}: {response_text}")
 
50
  else:
51
  print(f"No action taken for message: {message_text}")
52
 
53
+ # Set typing off after responding
54
+ set_typing_off(sender_id)
55
+
56
  return jsonify({'status': 'ok'})
57
 
58
  def send_message(recipient_id, message_text):
59
+ url = f'https://graph.facebook.com/v12.0/me/messages?access_token={ACCESS_TOKEN}'
 
60
  headers = {'Content-Type': 'application/json'}
61
  payload = {
62
  'recipient': {'id': recipient_id},
 
66
  response = requests.post(url, headers=headers, json=payload)
67
  print(f"Message send response: {response.status_code}, {response.text}")
68
 
69
+ def set_typing_on(recipient_id):
70
+ url = f'https://graph.facebook.com/v12.0/me/messages?access_token={ACCESS_TOKEN}'
71
+ headers = {'Content-Type': 'application/json'}
72
+ payload = {
73
+ 'recipient': {'id': recipient_id},
74
+ 'sender_action': 'typing_on'
75
+ }
76
+ print(f"Sending typing on request: {payload}")
77
+ response = requests.post(url, headers=headers, json=payload)
78
+ print(f"Typing on response: {response.status_code}, {response.text}")
79
+
80
+ def set_typing_off(recipient_id):
81
+ url = f'https://graph.facebook.com/v12.0/me/messages?access_token={ACCESS_TOKEN}'
82
+ headers = {'Content-Type': 'application/json'}
83
+ payload = {
84
+ 'recipient': {'id': recipient_id},
85
+ 'sender_action': 'typing_off'
86
+ }
87
+ print(f"Sending typing off request: {payload}")
88
+ response = requests.post(url, headers=headers, json=payload)
89
+ print(f"Typing off response: {response.status_code}, {response.text}")
90
+
91
  if __name__ == '__main__':
92
+ app.run(host="0.0.0.0", port=7860, debug=True)