Skip to content

Commit

Permalink
Whatsapp template (#1485)
Browse files Browse the repository at this point in the history
* - new user email issue

* - new user email issue

* - correcting nltk downloader

* - updated templated creation base url

* - updated templated creation base url

---------

Co-authored-by: hghuge <[email protected]>
  • Loading branch information
hiteshghuge and hghuge authored Sep 4, 2024
1 parent 5144a42 commit 2b2e37f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 74 deletions.
37 changes: 19 additions & 18 deletions kairon/shared/channels/whatsapp/bsp/dialog360.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ def add_template(self, data: Dict, bot: Text, user: Text):
try:
Utility.validate_create_template_request(data)
config = ChatDataProcessor.get_channel_config(ChannelTypes.WHATSAPP.value, self.bot, mask_characters=False)
partner_id = Utility.environment["channels"]["360dialog"]["partner_id"]
waba_account_id = config.get("config", {}).get("waba_account_id")
base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["hub_base_url"]
template_endpoint = f'/v1/partners/{partner_id}/waba_accounts/{waba_account_id}/waba_templates'
headers = {"Authorization": BSP360Dialog.get_partner_auth_token()}
url = f"{base_url}{template_endpoint}"
api_key = config.get("config", {}).get("api_key")
base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["waba_base_url"]
header = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["auth_header"]

headers = {header: api_key}
url = f"{base_url}/v1/configs/templates"
resp = Utility.execute_http_request(request_method="POST", http_url=url, request_body=data, headers=headers,
validate_status=True, err_msg="Failed to add template: ",
expected_status_code=201)
Expand Down Expand Up @@ -124,14 +124,15 @@ def edit_template(self, data: Dict, template_id: str):
logger.exception(e)
raise AppException(str(e))

def delete_template(self, template_id: str):
def delete_template(self, template_name: str):
try:
config = ChatDataProcessor.get_channel_config(ChannelTypes.WHATSAPP.value, self.bot, mask_characters=False)
partner_id = Utility.environment["channels"]["360dialog"]["partner_id"]
waba_account_id = config.get("config", {}).get("waba_account_id")
base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["hub_base_url"]
template_endpoint = f'/v1/partners/{partner_id}/waba_accounts/{waba_account_id}/waba_templates/{template_id}'
headers = {"Authorization": BSP360Dialog.get_partner_auth_token()}
api_key = config.get("config", {}).get("api_key")
base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["waba_base_url"]
header = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["auth_header"]

headers = {header: api_key}
template_endpoint = f'/v1/configs/templates/{template_name}'
url = f"{base_url}{template_endpoint}"
resp = Utility.execute_http_request(request_method="DELETE", http_url=url, headers=headers,
validate_status=True, err_msg="Failed to delete template: ")
Expand All @@ -149,12 +150,12 @@ def list_templates(self, **kwargs):
if kwargs:
filters = str(kwargs).replace('\'', "\"")
config = ChatDataProcessor.get_channel_config(ChannelTypes.WHATSAPP.value, self.bot, mask_characters=False)
account_id = config.get("config", {}).get("waba_account_id")
base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["hub_base_url"]
partner_id = config.get("config", {}).get("partner_id", Utility.environment["channels"]["360dialog"]["partner_id"])
template_endpoint = f'/api/v2/partners/{partner_id}/waba_accounts/{account_id}/waba_templates?filters={filters}&sort=business_templates.name'
headers = {"Authorization": BSP360Dialog.get_partner_auth_token()}
url = f"{base_url}{template_endpoint}"
api_key = config.get("config", {}).get("api_key")
base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["waba_base_url"]
header = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["auth_header"]

headers = {header: api_key}
url = f"{base_url}/v1/configs/templates?filters={filters}&sort=business_templates.name"
resp = Utility.execute_http_request(request_method="GET", http_url=url, headers=headers,
validate_status=True, err_msg="Failed to get template: ")
return resp.get("waba_templates")
Expand Down
45 changes: 14 additions & 31 deletions tests/unit_test/channels/bsp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,6 @@ def test_add_template(self, monkeypatch):
with mock.patch.dict(Utility.environment, {'channels': {"360dialog": {"partner_id": "new_partner_id"}}}):
responses.reset()
bot = "62bc24b493a0d6b7a46328ff"
partner_id = "new_partner_id"
waba_account_id = "Cyih7GWA"
data = {
"name": "Introduction template",
"category": "MARKETING",
Expand Down Expand Up @@ -375,13 +373,9 @@ def test_add_template(self, monkeypatch):
"category": "MARKETING"
}

def _get_partners_auth_token(*args, **kwargs):
return "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIs.ImtpZCI6Ik1EZEZOVFk1UVVVMU9FSXhPRGN3UVVZME9EUTFRVFJDT1.RSRU9VUTVNVGhDTURWRk9UUTNPQSJ9"

monkeypatch.setattr(BSP360Dialog, 'get_partner_auth_token', _get_partners_auth_token)

base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["hub_base_url"]
url = f"{base_url}/v1/partners/{partner_id}/waba_accounts/{waba_account_id}/waba_templates"
base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["waba_base_url"]
url = f"{base_url}/v1/configs/templates"
responses.add("POST", json=api_resp, url=url, status=201)
template = BSP360Dialog(bot, "test").add_template(data, bot, "test")
assert template == {'category': 'MARKETING', 'id': '594425479261596', 'status': 'PENDING'}
Expand Down Expand Up @@ -438,8 +432,6 @@ def test_add_template_error(self, monkeypatch):
def test_add_template_failure(self, monkeypatch):
with mock.patch.dict(Utility.environment, {'channels': {"360dialog": {"partner_id": "new_partner_id"}}}):
bot = "62bc24b493a0d6b7a46328ff"
partner_id = "new_partner_id"
waba_account_id = "Cyih7GWA"
data = {
"name": "Introduction template",
"category": "MARKETING",
Expand Down Expand Up @@ -473,8 +465,8 @@ def _get_partners_auth_token(*args, **kwargs):
return "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIs.ImtpZCI6Ik1EZEZOVFk1UVVVMU9FSXhPRGN3UVVZME9EUTFRVFJDT1.RSRU9VUTVNVGhDTURWRk9UUTNPQSJ9"

monkeypatch.setattr(BSP360Dialog, 'get_partner_auth_token', _get_partners_auth_token)
base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["hub_base_url"]
url = f"{base_url}/v1/partners/{partner_id}/waba_accounts/{waba_account_id}/waba_templates"
base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["waba_base_url"]
url = f"{base_url}/v1/configs/templates"
responses.add("POST", json={}, url=url, status=500)

with pytest.raises(AppException, match=r"Failed to add template: *"):
Expand Down Expand Up @@ -625,9 +617,7 @@ def _get_partners_auth_token(*args, **kwargs):
def test_delete_template(self, monkeypatch):
with mock.patch.dict(Utility.environment, {'channels': {"360dialog": {"partner_id": "new_partner_id"}}}):
bot = "62bc24b493a0d6b7a46328ff"
template_id = "test_id"
partner_id = "new_partner_id"
waba_account_id = "Cyih7GWA"
template_name = "test_id"
api_resp = {
"meta": {
"developer_message": "template name=Introduction template was deleted",
Expand All @@ -636,31 +626,24 @@ def test_delete_template(self, monkeypatch):
}
}

def _get_partners_auth_token(*args, **kwargs):
return "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIs.ImtpZCI6Ik1EZEZOVFk1UVVVMU9FSXhPRGN3UVVZME9EUTFRVFJDT1.RSRU9VUTVNVGhDTURWRk9UUTNPQSJ9"

monkeypatch.setattr(BSP360Dialog, 'get_partner_auth_token', _get_partners_auth_token)

base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["hub_base_url"]
url = f"{base_url}/v1/partners/{partner_id}/waba_accounts/{waba_account_id}/waba_templates/{template_id}"
base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["waba_base_url"]
url = f"{base_url}/v1/configs/templates/{template_name}"
responses.add("DELETE", json=api_resp, url=url)
template = BSP360Dialog(bot, "test").delete_template(template_id)
template = BSP360Dialog(bot, "test").delete_template(template_name)
assert template == {'meta': {'developer_message': 'template name=Introduction template was deleted', 'http_code': 200, 'success': True}}

@responses.activate
def test_delete_template_failure(self, monkeypatch):
with mock.patch.dict(Utility.environment, {'channels': {"360dialog": {"partner_id": "new_partner_id"}}}):
bot = "62bc24b493a0d6b7a46328ff"
template_id = "test_id"
partner_id = "new_partner_id"
waba_account_id = "Cyih7GWA"

def _get_partners_auth_token(*args, **kwargs):
return "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIs.ImtpZCI6Ik1EZEZOVFk1UVVVMU9FSXhPRGN3UVVZME9EUTFRVFJDT1.RSRU9VUTVNVGhDTURWRk9UUTNPQSJ9"

monkeypatch.setattr(BSP360Dialog, 'get_partner_auth_token', _get_partners_auth_token)
base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["hub_base_url"]
url = f"{base_url}/v1/partners/{partner_id}/waba_accounts/{waba_account_id}/waba_templates/{template_id}"
base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["waba_base_url"]
url = f"{base_url}/v1/configs/templates/{template_id}"
responses.add("DELETE", json={}, url=url, status=500)

with pytest.raises(AppException, match=r"Failed to delete template: *"):
Expand Down Expand Up @@ -724,8 +707,8 @@ def _get_partners_auth_token(*args, **kwargs):

monkeypatch.setattr(BSP360Dialog, 'get_partner_auth_token', _get_partners_auth_token)

base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["hub_base_url"]
url = f"{base_url}/api/v2/partners/new_partner_id/waba_accounts/Cyih7GWA/waba_templates?filters=%7B%22id%22:%20%22test_id%22%7D&sort=business_templates.name"
base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["waba_base_url"]
url = f"{base_url}/v1/configs/templates?filters=%7B%22id%22:%20%22test_id%22%7D&sort=business_templates.name"
responses.add("GET", json=api_resp, url=url)
template = BSP360Dialog(bot, "test").get_template(template_id)
assert template == [{'category': 'MARKETING', 'components': [{'example': {'body_text': [['Peter']]},
Expand All @@ -747,8 +730,8 @@ def _get_partners_auth_token(*args, **kwargs):
return "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIs.ImtpZCI6Ik1EZEZOVFk1UVVVMU9FSXhPRGN3UVVZME9EUTFRVFJDT1.RSRU9VUTVNVGhDTURWRk9UUTNPQSJ9"

monkeypatch.setattr(BSP360Dialog, 'get_partner_auth_token', _get_partners_auth_token)
base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["hub_base_url"]
url = f"{base_url}/api/v2/partners/new_partner_id/waba_accounts/Cyih7GWA/waba_templates?filters=%7B%22id%22:%20%22test_id%22%7D&sort=business_templates.name"
base_url = Utility.system_metadata["channels"]["whatsapp"]["business_providers"]["360dialog"]["waba_base_url"]
url = f"{base_url}/v1/configs/templates?filters=%7B%22id%22:%20%22test_id%22%7D&sort=business_templates.name"
responses.add("GET", json={}, url=url, status=500)

with pytest.raises(AppException, match=r"Failed to get template: *"):
Expand Down
Loading

0 comments on commit 2b2e37f

Please sign in to comment.