Skip to content

Commit

Permalink
Broadcast template fix (#1595)
Browse files Browse the repository at this point in the history
* Broadcast template issue handled and added test cases related to the same.

* Broadcast template issue handled and added test cases related to the same.

* fixed failing test cases

* Broadcast template issue handled and added test cases related to the same.
  • Loading branch information
maheshsattala authored Nov 13, 2024
1 parent c7fd7a5 commit d271fed
Show file tree
Hide file tree
Showing 5 changed files with 668 additions and 26 deletions.
3 changes: 2 additions & 1 deletion kairon/events/definitions/message_broadcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def execute(self, event_id: Text, **kwargs):
"""
config = None
reference_id = None
status = EVENT_STATUS.FAIL.value
status = EVENT_STATUS.INITIATED.value
exception = None
is_resend = kwargs.get('is_resend', "False") == "True"
try:
Expand All @@ -72,6 +72,7 @@ def execute(self, event_id: Text, **kwargs):
except Exception as e:
logger.exception(e)
exception = str(e)
status = EVENT_STATUS.FAIL.value
finally:
time.sleep(5)
MessageBroadcastProcessor.insert_status_received_on_channel_webhook(reference_id, config["name"],
Expand Down
33 changes: 24 additions & 9 deletions kairon/shared/channels/broadcast/whatsapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ def log(**kwargs):
template_name = self.config['template_name']
language_code = self.config['language_code']

raw_template = self.__get_template(template_name, language_code)
raw_template, template_exception = self.__get_template(template_name, language_code)
raw_template = raw_template if raw_template else []
MessageBroadcastProcessor.update_broadcast_logs_with_template(
self.reference_id, self.event_id, raw_template=raw_template,
log_type=MessageBroadcastLogType.send.value, retry_count=0
log_type=MessageBroadcastLogType.send.value, retry_count=0,
template_exception=template_exception
)

def __send_using_configuration(self, recipients: List):
Expand All @@ -105,7 +107,7 @@ def __send_using_configuration(self, recipients: List):
namespace = template_config.get("namespace")
lang = template_config["language"]
template_params = self._get_template_parameters(template_config)
raw_template = self.__get_template(template_id, lang)
raw_template, template_exception = self.__get_template(template_id, lang)

# if there's no template body, pass params as None for all recipients
template_params = template_params * len(recipients) if template_params else [template_params] * len(recipients)
Expand All @@ -128,7 +130,7 @@ def __send_using_configuration(self, recipients: List):
self.bot, MessageBroadcastLogType.send.value, self.reference_id, api_response=response,
status=status, recipient=recipient, template_params=t_params, template=raw_template,
event_id=self.event_id, template_name=template_id, language_code=lang, namespace=namespace,
retry_count=0
retry_count=0, template_exception=template_exception
)
MessageBroadcastProcessor.add_event_log(
self.bot, MessageBroadcastLogType.common.value, self.reference_id, failure_cnt=failure_cnt, total=total,
Expand All @@ -151,14 +153,17 @@ def resend_broadcast(self):
failure_cnt = 0
total = len(required_logs)
skipped_count = len(broadcast_logs) - total
template_name = required_logs[0]["template_name"]
language_code = required_logs[0]["language_code"]
template, template_exception = self.__get_template(template_name, language_code)

for log in required_logs:
template_id = log["template_name"]
namespace = log["namespace"]
language_code = log["language_code"]
components = log["template_params"]
recipient = log["recipient"]
template = log["template"]
template = log.template if log.template else template
response = channel_client.send_template_message(template_id, recipient, language_code, components,
namespace)
status = "Failed" if response.get("error") else "Success"
Expand All @@ -169,7 +174,7 @@ def resend_broadcast(self):
self.bot, MessageBroadcastLogType.resend.value, self.reference_id, api_response=response,
status=status, recipient=recipient, template_params=components, template=template,
event_id=self.event_id, template_name=template_id, language_code=language_code, namespace=namespace,
retry_count=retry_count,
retry_count=retry_count, template_exception=template_exception
)
kwargs = {
f"resend_count_{retry_count}": total,
Expand Down Expand Up @@ -197,6 +202,16 @@ def __get_client(self):
raise AppException(f"Whatsapp channel config not found!")

def __get_template(self, name: Text, language: Text):
for template in BSP360Dialog(self.bot, self.user).list_templates(**{"business_templates.name": name}):
if template.get("language") == language:
return template.get("components")
template_exception = None
template = []
try:
for template in BSP360Dialog(self.bot, self.user).list_templates(**{"business_templates.name": name}):
if template.get("language") == language:
template = template.get("components")
break
return template, template_exception
except Exception as e:
logger.exception(e)
template_exception = str(e)
return template, template_exception

3 changes: 3 additions & 0 deletions kairon/shared/channels/whatsapp/bsp/dialog360.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ def list_templates(self, **kwargs):
except DoesNotExist as e:
logger.exception(e)
raise AppException("Channel not found!")
except Exception as e:
logger.exception(e)
raise AppException(str(e))

@staticmethod
def get_partner_auth_token():
Expand Down
4 changes: 2 additions & 2 deletions kairon/shared/chat/broadcast/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ def get_reference_id_from_broadcasting_logs(event_id):
@staticmethod
def update_broadcast_logs_with_template(reference_id: Text, event_id: Text, raw_template: List[Dict],
log_type: MessageBroadcastLogType,
retry_count: int = 0):
retry_count: int = 0, **kwargs):
message_broadcast_logs = MessageBroadcastLogs.objects(reference_id=reference_id,
event_id=event_id,
log_type=log_type,
retry_count=retry_count)
message_broadcast_logs.update(template=raw_template)
message_broadcast_logs.update(template=raw_template, **kwargs)

@staticmethod
def extract_message_ids_from_broadcast_logs(reference_id: Text, retry_count: int = 0):
Expand Down
Loading

0 comments on commit d271fed

Please sign in to comment.