Mr-Help commited on
Commit
f1b8b54
·
verified ·
1 Parent(s): b2a7019

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +35 -15
main.py CHANGED
@@ -14,9 +14,12 @@ GREENAPI_TOKEN = "805b69f6c85d4e6caea0edaba692b889abecc9e6bb8b457e8f"
14
 
15
  GREENAPI_BASE = f"https://7105.api.greenapi.com/waInstance{GREENAPI_INSTANCE_ID}"
16
 
17
- # Interactive buttons endpoint (زي المثال بتاعك)
18
  GREENAPI_SEND_BUTTONS_URL = f"{GREENAPI_BASE}/sendInteractiveButtons/{GREENAPI_TOKEN}"
19
 
 
 
 
20
  # Plain text endpoint (لو عندك endpoint مختلف في GreenAPI غيّره هنا)
21
  GREENAPI_SEND_TEXT_URL = (
22
  f"https://7105.api.greenapi.com/"
@@ -70,12 +73,29 @@ def detect_intent_simple(text: str) -> str:
70
  # GreenAPI send helpers
71
  # =========================
72
 
73
- def send_interactive_buttons(chat_id: str, header: str, body: str, footer: str, buttons: list):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  """
75
  buttons example:
76
  [{"buttonId":"1","buttonText":"..."}, ...]
77
- OR url button:
78
- {"type":"url","buttonId":"1","buttonText":"...","url":"..."}
79
  """
80
  payload = {
81
  "chatId": chat_id,
@@ -84,7 +104,7 @@ def send_interactive_buttons(chat_id: str, header: str, body: str, footer: str,
84
  "footer": footer,
85
  "buttons": buttons
86
  }
87
- resp = requests.post(GREENAPI_SEND_BUTTONS_URL, json=payload, timeout=TIMEOUT_SEC)
88
  return resp.status_code, (resp.text or "")[:1500]
89
 
90
  def send_text_message(chat_id: str, message: str):
@@ -223,7 +243,8 @@ async def webhook_receiver(req: Request):
223
  {"buttonId": "3", "buttonText": "تواصل معنا"},
224
  ]
225
 
226
- status, txt = send_interactive_buttons(chat_id, header, body_txt, footer, buttons)
 
227
  print(">>> sent GREETING buttons:", status, txt)
228
  return {"ok": True, "intent": intent}
229
 
@@ -232,16 +253,15 @@ async def webhook_receiver(req: Request):
232
  body_txt = "يسعدنا تواصلك. سجل الشكوى من الرابط ده، وفريقنا هيتواصل معاك في أقرب وقت."
233
  footer = "شكراً لاختيار ÄDK"
234
 
235
- buttons = [
236
- {
237
- "type": "url",
238
- "buttonId": "1",
239
- "buttonText": "تسجيل الشكوى",
240
- "url": COMPLAINT_FORM_URL
241
- }
242
- ]
243
 
244
- status, txt = send_interactive_buttons(chat_id, header, body_txt, footer, buttons)
245
  print(">>> sent COMPLAINT url button:", status, txt)
246
  return {"ok": True, "intent": intent}
247
 
 
14
 
15
  GREENAPI_BASE = f"https://7105.api.greenapi.com/waInstance{GREENAPI_INSTANCE_ID}"
16
 
17
+ # Interactive buttons endpoint
18
  GREENAPI_SEND_BUTTONS_URL = f"{GREENAPI_BASE}/sendInteractiveButtons/{GREENAPI_TOKEN}"
19
 
20
+ # Interactive REPLY buttons endpoint
21
+ GREENAPI_SEND_REPLY_BUTTONS_URL = f"{GREENAPI_BASE}/sendInteractiveButtonsReply/{GREENAPI_TOKEN}"
22
+
23
  # Plain text endpoint (لو عندك endpoint مختلف في GreenAPI غيّره هنا)
24
  GREENAPI_SEND_TEXT_URL = (
25
  f"https://7105.api.greenapi.com/"
 
73
  # GreenAPI send helpers
74
  # =========================
75
 
76
+ def send_url_button(chat_id: str, header: str, body: str, footer: str, url: str, button_text: str = "تسجيل الشكوى"):
77
+ payload = {
78
+ "chatId": chat_id,
79
+ "body": body,
80
+ "header": header,
81
+ "footer": footer,
82
+ "buttons": [
83
+ {
84
+ "type": "url",
85
+ "buttonId": "1",
86
+ "buttonText": button_text,
87
+ "url": url
88
+ }
89
+ ]
90
+ }
91
+ resp = requests.post(GREENAPI_SEND_URL_BUTTON_URL, json=payload, timeout=TIMEOUT_SEC)
92
+ return resp.status_code, (resp.text or "")[:1500]
93
+
94
+
95
+ def send_reply_buttons(chat_id: str, header: str, body: str, footer: str, buttons: list):
96
  """
97
  buttons example:
98
  [{"buttonId":"1","buttonText":"..."}, ...]
 
 
99
  """
100
  payload = {
101
  "chatId": chat_id,
 
104
  "footer": footer,
105
  "buttons": buttons
106
  }
107
+ resp = requests.post(GREENAPI_SEND_REPLY_BUTTONS_URL, json=payload, timeout=TIMEOUT_SEC)
108
  return resp.status_code, (resp.text or "")[:1500]
109
 
110
  def send_text_message(chat_id: str, message: str):
 
243
  {"buttonId": "3", "buttonText": "تواصل معنا"},
244
  ]
245
 
246
+ status, txt = send_reply_buttons(chat_id, header, body_txt, footer, buttons)
247
+
248
  print(">>> sent GREETING buttons:", status, txt)
249
  return {"ok": True, "intent": intent}
250
 
 
253
  body_txt = "يسعدنا تواصلك. سجل الشكوى من الرابط ده، وفريقنا هيتواصل معاك في أقرب وقت."
254
  footer = "شكراً لاختيار ÄDK"
255
 
256
+ status, txt = send_url_button(
257
+ chat_id=chat_id,
258
+ header=header,
259
+ body=body_txt,
260
+ footer=footer,
261
+ url=COMPLAINT_FORM_URL,
262
+ button_text="تسجيل الشكوى"
263
+ )
264
 
 
265
  print(">>> sent COMPLAINT url button:", status, txt)
266
  return {"ok": True, "intent": intent}
267