narendrasinghd commited on
Commit
47c8cd5
·
verified ·
1 Parent(s): 841d157

Upload 20 files

Browse files
app/services/__pycache__/realtor_service.cpython-312.pyc CHANGED
Binary files a/app/services/__pycache__/realtor_service.cpython-312.pyc and b/app/services/__pycache__/realtor_service.cpython-312.pyc differ
 
app/services/agent_service.py CHANGED
@@ -5,4 +5,4 @@ def verify_agent_by_agent_id_and_broker_name(agent_id, broker_name):
5
  return True
6
  elif agent_id == "king_agent" and broker_name == "king_broker":
7
  return True
8
- return False
 
5
  return True
6
  elif agent_id == "king_agent" and broker_name == "king_broker":
7
  return True
8
+ return False
app/services/realtor_service.py CHANGED
@@ -1,8 +1,10 @@
1
  from app.db import crud
 
 
 
 
2
  from app.services.agent_service import verify_agent_by_agent_id_and_broker_name
3
  from app.services.properties_service import get_property_by_address_or_mls_number
4
- from app.utils.log_utils import setup_logger
5
- import requests, os, json, uuid
6
  from dotenv import load_dotenv
7
  load_dotenv()
8
 
@@ -13,7 +15,7 @@ n8n_webhook_url = os.getenv("N8N_WEBHOOK_URL")
13
 
14
  logger = setup_logger("realtor_service")
15
 
16
- def create_payload_for_whatsapp_message(from_number, to_number, body):
17
  payload = [
18
  {
19
  "SmsMessageSid": "SM0b07afa5f1ede39427957b4dc127cab3",
@@ -24,7 +26,7 @@ def create_payload_for_whatsapp_message(from_number, to_number, body):
24
  "WaId": from_number,
25
  "SmsStatus": "received",
26
  "Body": f"paparaphrase this: {body}",
27
- "To": f"whatsapp:{to_number}",
28
  "NumSegments": "1",
29
  "ReferralNumMedia": "0",
30
  "MessageSid": "SM0b07afa5f1ede39427957b4dc127cab3",
@@ -78,6 +80,7 @@ class RealtorService:
78
  if booking:
79
  booking.buyer_selected_date = buyer_selected_date
80
  booking.buyer_selected_time = buyer_selected_time
 
81
  db.commit()
82
  db.refresh(booking)
83
  return f"A booking already exists with this MLS number. The rescheduling request has been successfully processed. Your booking ID is {booking.id} for future reference."
@@ -86,6 +89,7 @@ class RealtorService:
86
  address=booking_address, mls_number=mls_number, buyer_selected_date=buyer_selected_date,
87
  buyer_selected_time=buyer_selected_time, listing_agent_phone_number=listing_agent_phone_number,
88
  listing_agent_session_id=new_session_id)
 
89
  else:
90
  logger.info(f"The user already exists but no active session found : {buyer_agent_phone_number}")
91
  listing_user.active_session_id = new_session_id
@@ -125,18 +129,18 @@ class RealtorService:
125
 
126
  listing_agent_body = f"Hello {listing_agent_full_name}, this is Alice assistant of {buyer_user.full_name}. We would like to schedule a showing for {booking_address} (MLS # {mls_number}) at {buyer_selected_time} on {buyer_selected_date}. The booking ID for this request is #{booking.id} for future reference. Can you make that work?."
127
  logger.info(f"Sending message to listing agent from: {ai_agnet_number}, body: {listing_agent_body}, to: {listing_agent_phone_number}")
128
- response = create_payload_for_whatsapp_message(from_number=listing_agent_phone_number, to_number=ai_agnet_number, body=listing_agent_body)
129
  if response == "Message sent successfully":
130
- return f"Do not say like its booked successfully say like: Your showing has been pending with the listing agent {listing_user.full_name}. I will notify you once the listing agent confirms the showing appointment. Your booking ID is #{booking.id} for future reference."
131
  return "Error in sending message to listing agent"
132
 
133
 
134
  def listing_realtor_confirmation(self, db, session_id, listing_selected_date, listing_selected_time, confirmation):
 
135
  logger.warning(f"Listing realtore - listing_selected_date: {listing_selected_date}, listing_selected_time: {listing_selected_time}, session_id: {session_id}, confirmation: {confirmation}.")
136
  try:
137
  booking = crud.get_booking_by_session_id(db, session_id=session_id)
138
  listing_user = crud.get_user_by_phone_number(db, phone_number=booking.listing_agent_phone_number)
139
- is_listing_user_session_queued = False
140
  except Exception as e:
141
  logger.error(f"Error in creating user: {e}")
142
  return f"booking not found with this session id, Error: {str(e)}"
@@ -154,22 +158,28 @@ class RealtorService:
154
  listing_selected_time = f"{listing_selected_time} AM"
155
  if confirmation == "confirmed":
156
  logger.warning(f"booking Schudule is confirmed by listing agent.")
157
- booking.status = "confirmed"
158
  if listing_user.queued_session_ids:
159
- logger.info(f"listing user has queued sessions Found.")
 
160
  listing_user.active_session_id = listing_user.queued_session_ids.pop(0)
 
161
  is_listing_user_session_queued = True
162
- listing_agent_body = f"Your showing at {booking.address} (MLS # {booking.mls_number}) is confirmed on {listing_selected_date} at {listing_selected_time} . I'll send you a reminder the day before to ensure you're prepared."
163
- buyer_agent_body = f"Your showing at {booking.address} (MLS # {booking.mls_number}) is confirmed by {listing_user.full_name} on {listing_selected_date} at {listing_selected_time} . I'll send you a reminder the day before to ensure you're prepared."
 
 
164
  elif confirmation == "cancelled":
165
  logger.warning(f"booking Schudule is cancelled by listing agent.")
166
  if listing_user.queued_session_ids:
167
- logger.info(f"listing user has queued sessions Found.")
 
168
  listing_user.active_session_id = listing_user.queued_session_ids.pop(0)
 
169
  is_listing_user_session_queued = True
170
  booking.status = "cancelled"
171
- listing_agent_body = f"Thank you for letting us know. We will inform the buyer that the showing has been cancelled."
172
- buyer_agent_body = f"Unfortunately, the listing agent has cancelled the showing. We apologize for any inconvenience caused."
 
173
  elif confirmation == "rescheduled":
174
  logger.warning(f"booking Schudule is rescheduled by listing agent. because of listing_selected_date: {listing_selected_date}, listing_selected_time: {listing_selected_time} and buyer_selected_date: {booking.buyer_selected_date}, buyer_selected_time: {booking.buyer_selected_time}")
175
  booking.status = "rescheduled"
@@ -179,8 +189,18 @@ class RealtorService:
179
  logger.warning(f"Invalid confirmation status")
180
  return "Invalid confirmation status"
181
 
182
- logger.info(f"Sending message to listing agent from: {ai_agnet_number}, body: {buyer_agent_body}, to: {booking.buyer_agent_phone_number}")
183
- response = create_payload_for_whatsapp_message(from_number=booking.buyer_agent_phone_number, to_number=ai_agnet_number, body=buyer_agent_body)
 
 
 
 
 
 
 
 
 
 
184
  if response != "Message sent successfully":
185
  logger.error(f"Error in sending message to listing agent")
186
  return "Error in sending message to buyer agent"
@@ -188,6 +208,7 @@ class RealtorService:
188
  return listing_agent_body
189
 
190
  def buyer_realtor_confirmation(self, db, buyer_agent_phone_number, booking_id, mls_number, buyer_selected_date, buyer_selected_time, confirmation):
 
191
  logger.warning(f"Buyer realtore - booking_id: {booking_id}, mls_number: {mls_number}, buyer_selected_date: {buyer_selected_date}, buyer_selected_time: {buyer_selected_time}, confirmation: {confirmation}.")
192
  try:
193
  if booking_id:
@@ -196,6 +217,7 @@ class RealtorService:
196
  booking = crud.get_booking_by_mls_number_and_buyer_agent_phone_number(db, mls_number=mls_number, buyer_agent_phone_number=buyer_agent_phone_number)
197
  else:
198
  return "Invalid booking id or mls number"
 
199
  except Exception as e:
200
  logger.error(f"Error in creating user: {e}")
201
  return f"booking not found with this booking id, Error: {str(e)}"
@@ -212,25 +234,50 @@ class RealtorService:
212
  buyer_selected_time = f"{buyer_selected_time} AM"
213
  if confirmation == "confirmed":
214
  logger.warning(f"booking Schudule is confirmed by buyer agent.")
 
 
 
 
 
 
215
  booking.status = "confirmed"
216
  buyer_agent_body = f"Your showing at {booking.address} (MLS # {booking.mls_number}) is confirmed on {buyer_selected_date} at {buyer_selected_time}. Your booking ID is {booking.id} for future reference. I'll send you a reminder the day before to ensure you're prepared."
217
  listing_agent_body = f"Your showing at {booking.address} (MLS # {booking.mls_number}) is confirmed on {buyer_selected_date} at {buyer_selected_time} . Your booking ID is {booking.id} for future reference. I'll send you a reminder the day before to ensure you're prepared."
 
218
  elif confirmation == "cancelled":
219
  logger.warning(f"booking Schudule is cancelled by buyer agent.")
 
 
 
 
 
 
220
  booking.status = "cancelled"
221
- buyer_agent_body = f"Thank you for letting us know. We will inform the listing Agent that the showing has been cancelled."
222
- listing_agent_body = f"Unfortunately, the Buyer agent has cancelled the showing. We apologize for any inconvenience caused."
 
223
  elif confirmation == "rescheduled":
224
  logger.warning(f"booking Schudule is rescheduled by Buyer agent. because of listing_selected_date: {buyer_selected_date}, listing_selected_time: {buyer_selected_time} and buyer_selected_date: {booking.buyer_selected_date}, buyer_selected_time: {booking.buyer_selected_time}")
225
  booking.status = "rescheduled"
226
  buyer_agent_body = f"paparaphrase this: Your reschedule request for the showing has been submitted and is waiting for confirmation from the buyer agent. I’ll update you once they confirm the showing appointment. Your booking ID is #{booking.id} for reference."
227
  listing_agent_body = f"The Buyer agent for the booking at {booking.address} (MLS # {booking.mls_number}) wants to rescheduled the showing on {buyer_selected_date} at {buyer_selected_time}. Your booking ID is #{booking.id} for future reference. Please confirm if this new time works for you."
 
228
  else:
229
  logger.warning(f"Invalid confirmation status")
230
  return "Invalid confirmation status"
231
 
 
 
 
 
 
 
 
 
 
 
232
  logger.info(f"Sending message to listing agent from: {ai_agnet_number}, body: {listing_agent_body}, to: {booking.listing_agent_phone_number}")
233
- response = create_payload_for_whatsapp_message(from_number=booking.listing_agent_phone_number, to_number=ai_agnet_number, body=listing_agent_body)
234
  if response != "Message sent successfully":
235
  logger.error(f"Error in sending message to listing agent")
236
  return "Error in sending message to Listing agent"
 
1
  from app.db import crud
2
+ import requests, os, json, uuid
3
+ from app.utils.log_utils import setup_logger
4
+ from sqlalchemy.orm.attributes import flag_modified
5
+ from app.task_scheduler.task import schedule_n8n_workflow
6
  from app.services.agent_service import verify_agent_by_agent_id_and_broker_name
7
  from app.services.properties_service import get_property_by_address_or_mls_number
 
 
8
  from dotenv import load_dotenv
9
  load_dotenv()
10
 
 
15
 
16
  logger = setup_logger("realtor_service")
17
 
18
+ def create_payload_for_whatsapp_message(from_number, body):
19
  payload = [
20
  {
21
  "SmsMessageSid": "SM0b07afa5f1ede39427957b4dc127cab3",
 
26
  "WaId": from_number,
27
  "SmsStatus": "received",
28
  "Body": f"paparaphrase this: {body}",
29
+ "To": f"whatsapp:{ai_agnet_number}",
30
  "NumSegments": "1",
31
  "ReferralNumMedia": "0",
32
  "MessageSid": "SM0b07afa5f1ede39427957b4dc127cab3",
 
80
  if booking:
81
  booking.buyer_selected_date = buyer_selected_date
82
  booking.buyer_selected_time = buyer_selected_time
83
+ booking.status = "pending"
84
  db.commit()
85
  db.refresh(booking)
86
  return f"A booking already exists with this MLS number. The rescheduling request has been successfully processed. Your booking ID is {booking.id} for future reference."
 
89
  address=booking_address, mls_number=mls_number, buyer_selected_date=buyer_selected_date,
90
  buyer_selected_time=buyer_selected_time, listing_agent_phone_number=listing_agent_phone_number,
91
  listing_agent_session_id=new_session_id)
92
+ return f"Do not say like its booked successfully say like: Your showing request is awaiting confirmation from the listing agent, {listing_user.full_name}. I will notify you once the listing agent confirms the showing appointment. Your booking ID is #{booking.id} for future reference."
93
  else:
94
  logger.info(f"The user already exists but no active session found : {buyer_agent_phone_number}")
95
  listing_user.active_session_id = new_session_id
 
129
 
130
  listing_agent_body = f"Hello {listing_agent_full_name}, this is Alice assistant of {buyer_user.full_name}. We would like to schedule a showing for {booking_address} (MLS # {mls_number}) at {buyer_selected_time} on {buyer_selected_date}. The booking ID for this request is #{booking.id} for future reference. Can you make that work?."
131
  logger.info(f"Sending message to listing agent from: {ai_agnet_number}, body: {listing_agent_body}, to: {listing_agent_phone_number}")
132
+ response = create_payload_for_whatsapp_message(from_number=listing_agent_phone_number, body=listing_agent_body)
133
  if response == "Message sent successfully":
134
+ return f"Do not say like its booked successfully say like: Your showing request is awaiting confirmation from the listing agent, {listing_user.full_name}. I will notify you once the listing agent confirms the showing appointment. Your booking ID is #{booking.id} for future reference."
135
  return "Error in sending message to listing agent"
136
 
137
 
138
  def listing_realtor_confirmation(self, db, session_id, listing_selected_date, listing_selected_time, confirmation):
139
+ is_listing_user_session_queued = False
140
  logger.warning(f"Listing realtore - listing_selected_date: {listing_selected_date}, listing_selected_time: {listing_selected_time}, session_id: {session_id}, confirmation: {confirmation}.")
141
  try:
142
  booking = crud.get_booking_by_session_id(db, session_id=session_id)
143
  listing_user = crud.get_user_by_phone_number(db, phone_number=booking.listing_agent_phone_number)
 
144
  except Exception as e:
145
  logger.error(f"Error in creating user: {e}")
146
  return f"booking not found with this session id, Error: {str(e)}"
 
158
  listing_selected_time = f"{listing_selected_time} AM"
159
  if confirmation == "confirmed":
160
  logger.warning(f"booking Schudule is confirmed by listing agent.")
 
161
  if listing_user.queued_session_ids:
162
+ logger.info(f"listing user has queued sessions Found. active session id: {listing_user.active_session_id}, queued sessions: {listing_user.queued_session_ids}")
163
+ listing_user.completed_session_ids = listing_user.completed_session_ids + [listing_user.active_session_id]
164
  listing_user.active_session_id = listing_user.queued_session_ids.pop(0)
165
+ flag_modified(listing_user, "queued_session_ids")
166
  is_listing_user_session_queued = True
167
+ booking.status = "confirmed"
168
+ listing_agent_body = f"Your showing at {booking.address} (MLS # {booking.mls_number}) is confirmed on {listing_selected_date} at {listing_selected_time}. I'll send you a reminder the day before to ensure you're prepared. The booking ID for this request is #{booking.id} for future reference."
169
+ buyer_agent_body = f"Your showing at {booking.address} (MLS # {booking.mls_number}) is confirmed by {listing_user.full_name} on {listing_selected_date} at {listing_selected_time} . I'll send you a reminder the day before to ensure you're prepared. The booking ID for this request is #{booking.id} for future reference."
170
+
171
  elif confirmation == "cancelled":
172
  logger.warning(f"booking Schudule is cancelled by listing agent.")
173
  if listing_user.queued_session_ids:
174
+ logger.info(f"listing user has queued sessions Found. active session id: {listing_user.active_session_id}, queued sessions: {listing_user.queued_session_ids}")
175
+ listing_user.completed_session_ids = listing_user.completed_session_ids + [listing_user.active_session_id]
176
  listing_user.active_session_id = listing_user.queued_session_ids.pop(0)
177
+ flag_modified(listing_user, "queued_session_ids")
178
  is_listing_user_session_queued = True
179
  booking.status = "cancelled"
180
+ listing_agent_body = f"Thank you for letting us know. I'll inform the buyer that the showing has been canceled. The booking ID for this request is #{booking.id} for future reference."
181
+ buyer_agent_body = f"Unfortunately, the listing agent, {listing_user.full_name} has canceled the showing for the property at {booking.address} (MLS # {booking.mls_number}). We apologize for any inconvenience. Your booking ID is #{booking.id} for future reference."
182
+
183
  elif confirmation == "rescheduled":
184
  logger.warning(f"booking Schudule is rescheduled by listing agent. because of listing_selected_date: {listing_selected_date}, listing_selected_time: {listing_selected_time} and buyer_selected_date: {booking.buyer_selected_date}, buyer_selected_time: {booking.buyer_selected_time}")
185
  booking.status = "rescheduled"
 
189
  logger.warning(f"Invalid confirmation status")
190
  return "Invalid confirmation status"
191
 
192
+ if is_listing_user_session_queued:
193
+ countdown_second=60
194
+ queued_booking = crud.get_booking_by_session_id(db, listing_user.active_session_id)
195
+ logger.info(f"Queued sessions id: {queued_booking.listing_agent_session_id}")
196
+ buyer_user = queued_booking.buyer_agent
197
+ listing_agent_body = f"Hello {listing_user.full_name}, this is Alice assistant of {buyer_user.full_name}. We would like to schedule a showing for {queued_booking.address} (MLS # {queued_booking.mls_number}) at {queued_booking.buyer_selected_time} on {queued_booking.buyer_selected_date}. The booking ID for this request is #{queued_booking.id} for future reference. Can you make that work?."
198
+ logger.info(f"Sending message to listing agent, body: {listing_agent_body}, to: {listing_user.phone_number}")
199
+ result = schedule_n8n_workflow.apply_async(args = [listing_user.phone_number, listing_agent_body], countdown=countdown_second)
200
+ logger.info(f"Task scheduled to run in {countdown_second} seconds.")
201
+
202
+ logger.info(f"Sending message to listing agent, body: {buyer_agent_body}, to: {booking.buyer_agent_phone_number}")
203
+ response = create_payload_for_whatsapp_message(from_number=booking.buyer_agent_phone_number, body=buyer_agent_body)
204
  if response != "Message sent successfully":
205
  logger.error(f"Error in sending message to listing agent")
206
  return "Error in sending message to buyer agent"
 
208
  return listing_agent_body
209
 
210
  def buyer_realtor_confirmation(self, db, buyer_agent_phone_number, booking_id, mls_number, buyer_selected_date, buyer_selected_time, confirmation):
211
+ is_listing_user_session_queued = False
212
  logger.warning(f"Buyer realtore - booking_id: {booking_id}, mls_number: {mls_number}, buyer_selected_date: {buyer_selected_date}, buyer_selected_time: {buyer_selected_time}, confirmation: {confirmation}.")
213
  try:
214
  if booking_id:
 
217
  booking = crud.get_booking_by_mls_number_and_buyer_agent_phone_number(db, mls_number=mls_number, buyer_agent_phone_number=buyer_agent_phone_number)
218
  else:
219
  return "Invalid booking id or mls number"
220
+ listing_user = crud.get_user_by_phone_number(db, phone_number=booking.listing_agent_phone_number)
221
  except Exception as e:
222
  logger.error(f"Error in creating user: {e}")
223
  return f"booking not found with this booking id, Error: {str(e)}"
 
234
  buyer_selected_time = f"{buyer_selected_time} AM"
235
  if confirmation == "confirmed":
236
  logger.warning(f"booking Schudule is confirmed by buyer agent.")
237
+ if listing_user.queued_session_ids:
238
+ logger.info(f"listing user has queued sessions Found. active session id: {listing_user.active_session_id}, queued sessions: {listing_user.queued_session_ids}")
239
+ listing_user.completed_session_ids = listing_user.completed_session_ids + [listing_user.active_session_id]
240
+ listing_user.active_session_id = listing_user.queued_session_ids.pop(0)
241
+ flag_modified(listing_user, "queued_session_ids")
242
+ is_listing_user_session_queued = True
243
  booking.status = "confirmed"
244
  buyer_agent_body = f"Your showing at {booking.address} (MLS # {booking.mls_number}) is confirmed on {buyer_selected_date} at {buyer_selected_time}. Your booking ID is {booking.id} for future reference. I'll send you a reminder the day before to ensure you're prepared."
245
  listing_agent_body = f"Your showing at {booking.address} (MLS # {booking.mls_number}) is confirmed on {buyer_selected_date} at {buyer_selected_time} . Your booking ID is {booking.id} for future reference. I'll send you a reminder the day before to ensure you're prepared."
246
+
247
  elif confirmation == "cancelled":
248
  logger.warning(f"booking Schudule is cancelled by buyer agent.")
249
+ if listing_user.queued_session_ids:
250
+ logger.info(f"listing user has queued sessions Found. active session id: {listing_user.active_session_id}, queued sessions: {listing_user.queued_session_ids}")
251
+ listing_user.completed_session_ids = listing_user.completed_session_ids + [listing_user.active_session_id]
252
+ listing_user.active_session_id = listing_user.queued_session_ids.pop(0)
253
+ flag_modified(listing_user, "queued_session_ids")
254
+ is_listing_user_session_queued = True
255
  booking.status = "cancelled"
256
+ buyer_agent_body = f"Thank you for letting us know. I'll inform the listing agent that the showing has been canceled. The booking ID for this request is #{booking.id} for future reference."
257
+ listing_agent_body = f"Unfortunately, the Buyer agent, {booking.buyer_agent.full_name} has canceled the showing for the property at {booking.address} (MLS # {booking.mls_number}). We apologize for any inconvenience. Your booking ID is #{booking.id} for future reference."
258
+
259
  elif confirmation == "rescheduled":
260
  logger.warning(f"booking Schudule is rescheduled by Buyer agent. because of listing_selected_date: {buyer_selected_date}, listing_selected_time: {buyer_selected_time} and buyer_selected_date: {booking.buyer_selected_date}, buyer_selected_time: {booking.buyer_selected_time}")
261
  booking.status = "rescheduled"
262
  buyer_agent_body = f"paparaphrase this: Your reschedule request for the showing has been submitted and is waiting for confirmation from the buyer agent. I’ll update you once they confirm the showing appointment. Your booking ID is #{booking.id} for reference."
263
  listing_agent_body = f"The Buyer agent for the booking at {booking.address} (MLS # {booking.mls_number}) wants to rescheduled the showing on {buyer_selected_date} at {buyer_selected_time}. Your booking ID is #{booking.id} for future reference. Please confirm if this new time works for you."
264
+
265
  else:
266
  logger.warning(f"Invalid confirmation status")
267
  return "Invalid confirmation status"
268
 
269
+ if is_listing_user_session_queued:
270
+ countdown_second=60
271
+ queued_booking = crud.get_booking_by_session_id(db, listing_user.active_session_id)
272
+ logger.info(f"Queued sessions id: {queued_booking.listing_agent_session_id}")
273
+ buyer_user = queued_booking.buyer_agent
274
+ listing_agent_body = f"Hello {listing_user.full_name}, this is Alice assistant of {buyer_user.full_name}. We would like to schedule a showing for {queued_booking.address} (MLS # {queued_booking.mls_number}) at {queued_booking.buyer_selected_time} on {queued_booking.buyer_selected_date}. The booking ID for this request is #{queued_booking.id} for future reference. Can you make that work?."
275
+ logger.info(f"Sending message to listing agent, body: {listing_agent_body}, to: {listing_user.phone_number}")
276
+ result = schedule_n8n_workflow.apply_async(args = [listing_user.phone_number, listing_agent_body], countdown=countdown_second)
277
+ logger.info(f"Task scheduled to run in {countdown_second} seconds.")
278
+
279
  logger.info(f"Sending message to listing agent from: {ai_agnet_number}, body: {listing_agent_body}, to: {booking.listing_agent_phone_number}")
280
+ response = create_payload_for_whatsapp_message(from_number=booking.listing_agent_phone_number, body=listing_agent_body)
281
  if response != "Message sent successfully":
282
  logger.error(f"Error in sending message to listing agent")
283
  return "Error in sending message to Listing agent"
app/task_scheduler/__pycache__/task.cpython-312.pyc ADDED
Binary file (2.13 kB). View file
 
app/task_scheduler/task.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests, os, json
2
+ from celery import Celery
3
+ from app.utils.log_utils import setup_logger
4
+
5
+ logger = setup_logger("task")
6
+
7
+ ai_agnet_number = os.getenv("AI_AGENT_NUMBER")
8
+ n8n_webhook_url = os.getenv("N8N_WEBHOOK_URL")
9
+
10
+ app = Celery(
11
+ "tasks",
12
+ broker="redis://localhost:6379/0",
13
+ backend="redis://localhost:6379/0",
14
+ )
15
+ app.conf.broker_connection_retry_on_startup = True
16
+
17
+ @app.task
18
+ def schedule_n8n_workflow(from_number, body):
19
+ payload = [
20
+ {
21
+ "SmsMessageSid": "SM0b07afa5f1ede39427957b4dc127cab3",
22
+ "NumMedia": "0",
23
+ "ProfileName": "",
24
+ "MessageType": "text",
25
+ "SmsSid": "SM0b07afa5f1ede39427957b4dc127cab3",
26
+ "WaId": from_number,
27
+ "SmsStatus": "received",
28
+ "Body": f"paparaphrase this: {body}",
29
+ "To": f"whatsapp:{ai_agnet_number}",
30
+ "NumSegments": "1",
31
+ "ReferralNumMedia": "0",
32
+ "MessageSid": "SM0b07afa5f1ede39427957b4dc127cab3",
33
+ "AccountSid": "",
34
+ "From": f"whatsapp:{from_number}",
35
+ "ApiVersion": "2010-04-01"
36
+ }
37
+ ]
38
+
39
+ try:
40
+ logger.info(f"Payload for whatsapp webhook: {payload}")
41
+ headers = {"Content-Type": "application/json"}
42
+ response = requests.post(n8n_webhook_url, headers=headers, data=json.dumps(payload))
43
+ logger.info(f"Response for whatsapp webhook: {response.text}")
44
+ except Exception as e:
45
+ logger.error(f"Error in sending message to agent: {e}")
46
+ return f"Error in sending message to agent: {str(e)}"
47
+ return "Message sent successfully"