From 50d3bd0f0a9493ed9b414fe0a4d9e55429a2072b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20L=C3=B3pez?= Date: Wed, 27 Mar 2019 15:45:10 -0600 Subject: [PATCH 1/8] check contact list method --- botmaker/client.py | 16 ++++++++-- .../test_check_whatsapp_contact_list.yaml | 31 +++++++++++++++++++ tests/test_whatsapp.py | 8 +++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 tests/cassettes/test_check_whatsapp_contact_list.yaml diff --git a/botmaker/client.py b/botmaker/client.py index 6ff13dd..cdfb774 100644 --- a/botmaker/client.py +++ b/botmaker/client.py @@ -25,7 +25,7 @@ def post(self, endpoint: str, data: dict, **kwargs) -> dict: return self.request('post', endpoint, data, **kwargs) def request( - self, method: str, endpoint: str, data: dict, **kwargs + self, method: str, endpoint: str, data: dict, **kwargs ) -> dict: url = self.BASE_URL + endpoint response = requests.request( @@ -46,7 +46,7 @@ def _check_response(response): response.raise_for_status() def check_whatsapp_contact( - self, channel: str, phone_number: str + self, channel: str, phone_number: str ) -> Optional[str]: """ Based on @@ -65,3 +65,15 @@ def check_whatsapp_contact( except KeyError: checked = None return checked + + def check_whatsapp_contact_list(self, channel: str, + phone_numbers: list) -> list: + channel = sanitize_phone_number(channel) + data = dict(chatChannelNumber=channel, contacts=phone_numbers) + resp = self.post('/customer/checkWhatsAppContact', data) + try: + result = resp['result'] + except KeyError: + # This should never happen + raise BotmakerException("Expected 'result' in the response body") + return result diff --git a/tests/cassettes/test_check_whatsapp_contact_list.yaml b/tests/cassettes/test_check_whatsapp_contact_list.yaml new file mode 100644 index 0000000..e8945ff --- /dev/null +++ b/tests/cassettes/test_check_whatsapp_contact_list.yaml @@ -0,0 +1,31 @@ +interactions: +- request: + body: '{"contacts": ["+55 1 55 1234 5678","123"], "chatChannelNumber": "5215500000000"}' + headers: + Accept: ['*/*'] + Accept-Encoding: ['[application/json, application/xml, text/plain]'] + Connection: [keep-alive] + Content-Length: ['80'] + Content-Type: [application/json] + User-Agent: [python-requests/2.21.0] + access-token: [DUMMY] + method: POST + uri: https://go.botmaker.com/api/v1.0/customer/checkWhatsAppContact + response: + body: + string: '{"problems":null,"result":{"+55 1 55 1234 5678":"5515512345678"}}' + headers: + Accept: ['[application/json, application/xml, text/plain]'] + Access-Control-Allow-Credentials: ['true'] + Access-Control-Allow-Headers: ['token, content-type, accept-encoding', 'bearer-token,access-token, + Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin'] + Access-Control-Allow-Methods: ['GET, POST, PUT, DELETE, OPTIONS, HEAD'] + Access-Control-Allow-Origin: ['*'] + Content-Length: ['65'] + Content-Type: [application/json] + Date: ['Fri, 08 Feb 2019 21:39:21 GMT', 'Fri, 08 Feb 2019 21:39:21 GMT'] + Server: [BotMaker] + Strict-Transport-Security: [max-age=86400; includeSubDomains] + Vary: ['Accept-Encoding, User-Agent'] + status: {code: 200, message: OK} +version: 1 diff --git a/tests/test_whatsapp.py b/tests/test_whatsapp.py index 25645a2..5e0d0ed 100644 --- a/tests/test_whatsapp.py +++ b/tests/test_whatsapp.py @@ -17,6 +17,14 @@ def test_invalid_whatsapp_contact(client): assert client.check_whatsapp_contact('5215500000000', '123') is None +@pytest.mark.vcr +def test_check_whatsapp_contact_list(client): + contacts = ['+55 1 55 1234 5678', '123'] + result = client.check_whatsapp_contact_list('5215500000000', contacts) + assert '+55 1 55 1234 5678' in result # whatsapp + assert '123' not in result # no whatsapp + + @pytest.mark.vcr def test_invalid_channel(client): with pytest.raises(BotmakerException): From d05a8f3d2126db74d46f9ac0271bc83728ac335d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20L=C3=B3pez?= Date: Wed, 27 Mar 2019 15:48:27 -0600 Subject: [PATCH 2/8] polish --- botmaker/client.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/botmaker/client.py b/botmaker/client.py index cdfb774..18f0f6d 100644 --- a/botmaker/client.py +++ b/botmaker/client.py @@ -25,7 +25,7 @@ def post(self, endpoint: str, data: dict, **kwargs) -> dict: return self.request('post', endpoint, data, **kwargs) def request( - self, method: str, endpoint: str, data: dict, **kwargs + self, method: str, endpoint: str, data: dict, **kwargs ) -> dict: url = self.BASE_URL + endpoint response = requests.request( @@ -46,7 +46,7 @@ def _check_response(response): response.raise_for_status() def check_whatsapp_contact( - self, channel: str, phone_number: str + self, channel: str, phone_number: str ) -> Optional[str]: """ Based on @@ -66,8 +66,9 @@ def check_whatsapp_contact( checked = None return checked - def check_whatsapp_contact_list(self, channel: str, - phone_numbers: list) -> list: + def check_whatsapp_contact_list( + self, channel: str, phone_numbers: list + ) -> list: channel = sanitize_phone_number(channel) data = dict(chatChannelNumber=channel, contacts=phone_numbers) resp = self.post('/customer/checkWhatsAppContact', data) From 9f6f1b0d7bc3ad9c6def917b24cdc0b96eef136c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20L=C3=B3pez?= Date: Wed, 27 Mar 2019 15:57:27 -0600 Subject: [PATCH 3/8] refactor --- botmaker/client.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/botmaker/client.py b/botmaker/client.py index 18f0f6d..1ecb89a 100644 --- a/botmaker/client.py +++ b/botmaker/client.py @@ -48,18 +48,7 @@ def _check_response(response): def check_whatsapp_contact( self, channel: str, phone_number: str ) -> Optional[str]: - """ - Based on - https://botmakeradmin.github.io/docs/es/#/messages-api?id=chequear-validez-de-n%C3%BAmeros-de-contactos-de-whatsapp - """ - channel = sanitize_phone_number(channel) - data = dict(chatChannelNumber=channel, contacts=[phone_number]) - resp = self.post('/customer/checkWhatsAppContact', data) - try: - result = resp['result'] - except KeyError: - # This should never happen - raise BotmakerException("Expected 'result' in the response body") + result = self.check_whatsapp_contact_list(channel, [phone_number]) try: checked = result[phone_number] except KeyError: @@ -68,7 +57,11 @@ def check_whatsapp_contact( def check_whatsapp_contact_list( self, channel: str, phone_numbers: list - ) -> list: + ) -> dict: + """ + Based on + https://botmakeradmin.github.io/docs/es/#/messages-api?id=chequear-validez-de-n%C3%BAmeros-de-contactos-de-whatsapp + """ channel = sanitize_phone_number(channel) data = dict(chatChannelNumber=channel, contacts=phone_numbers) resp = self.post('/customer/checkWhatsAppContact', data) From 492beed4435ef1f23297981bdbbdf4837f0b0f13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20L=C3=B3pez?= Date: Wed, 27 Mar 2019 16:03:51 -0600 Subject: [PATCH 4/8] upgrade version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index b12fecc..4c0e574 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ setuptools.setup( name='botmaker', - version='0.2.1', + version='0.2.2', author='Cuenca', author_email='dev@cuenca.com', description='BotMaker API Client', From 5ae21eadbc36b6da1cdefc3f1bb78952abda4a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20L=C3=B3pez?= Date: Wed, 27 Mar 2019 20:00:23 -0600 Subject: [PATCH 5/8] refactor check_whatsapp_contacts --- botmaker/client.py | 16 ++-------- botmaker/resources/template_messages.py | 6 ++-- .../test_check_whatsapp_contact.yaml | 31 ------------------- ...yaml => test_check_whatsapp_contacts.yaml} | 0 tests/test_template_messages.py | 1 + tests/test_whatsapp.py | 20 ++---------- 6 files changed, 10 insertions(+), 64 deletions(-) delete mode 100644 tests/cassettes/test_check_whatsapp_contact.yaml rename tests/cassettes/{test_check_whatsapp_contact_list.yaml => test_check_whatsapp_contacts.yaml} (100%) diff --git a/botmaker/client.py b/botmaker/client.py index 1ecb89a..aed8b26 100644 --- a/botmaker/client.py +++ b/botmaker/client.py @@ -25,7 +25,7 @@ def post(self, endpoint: str, data: dict, **kwargs) -> dict: return self.request('post', endpoint, data, **kwargs) def request( - self, method: str, endpoint: str, data: dict, **kwargs + self, method: str, endpoint: str, data: dict, **kwargs ) -> dict: url = self.BASE_URL + endpoint response = requests.request( @@ -45,18 +45,8 @@ def _check_response(response): else: response.raise_for_status() - def check_whatsapp_contact( - self, channel: str, phone_number: str - ) -> Optional[str]: - result = self.check_whatsapp_contact_list(channel, [phone_number]) - try: - checked = result[phone_number] - except KeyError: - checked = None - return checked - - def check_whatsapp_contact_list( - self, channel: str, phone_numbers: list + def check_whatsapp_contacts( + self, channel: str, phone_numbers: list ) -> dict: """ Based on diff --git a/botmaker/resources/template_messages.py b/botmaker/resources/template_messages.py index 866276b..e036de8 100644 --- a/botmaker/resources/template_messages.py +++ b/botmaker/resources/template_messages.py @@ -38,12 +38,12 @@ def create( """ from_ = sanitize_phone_number(from_) if chat_platform == 'whatsapp': - checked = cls._client.check_whatsapp_contact(from_, to) - if not checked: + check_dict = cls._client.check_whatsapp_contacts(from_, [to]) + if to not in check_dict: raise InvalidPhoneNumber( f"'{to} is not from valid WhatsApp contact") else: - to = checked + to = check_dict[to] else: to = sanitize_phone_number(to) data = dict( diff --git a/tests/cassettes/test_check_whatsapp_contact.yaml b/tests/cassettes/test_check_whatsapp_contact.yaml deleted file mode 100644 index 5ea8d9d..0000000 --- a/tests/cassettes/test_check_whatsapp_contact.yaml +++ /dev/null @@ -1,31 +0,0 @@ -interactions: -- request: - body: '{"contacts": ["+55 1 55 1234 5678"], "chatChannelNumber": "5215500000000"}' - headers: - Accept: ['*/*'] - Accept-Encoding: ['[application/json, application/xml, text/plain]'] - Connection: [keep-alive] - Content-Length: ['74'] - Content-Type: [application/json] - User-Agent: [python-requests/2.21.0] - access-token: [DUMMY] - method: POST - uri: https://go.botmaker.com/api/v1.0/customer/checkWhatsAppContact - response: - body: - string: '{"problems":null,"result":{"+55 1 55 1234 5678":"5515512345678"}}' - headers: - Accept: ['[application/json, application/xml, text/plain]'] - Access-Control-Allow-Credentials: ['true'] - Access-Control-Allow-Headers: ['token, content-type, accept-encoding', 'bearer-token,access-token, - Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin'] - Access-Control-Allow-Methods: ['GET, POST, PUT, DELETE, OPTIONS, HEAD'] - Access-Control-Allow-Origin: ['*'] - Content-Length: ['65'] - Content-Type: [application/json] - Date: ['Fri, 08 Feb 2019 21:39:21 GMT', 'Fri, 08 Feb 2019 21:39:21 GMT'] - Server: [BotMaker] - Strict-Transport-Security: [max-age=86400; includeSubDomains] - Vary: ['Accept-Encoding, User-Agent'] - status: {code: 200, message: OK} -version: 1 diff --git a/tests/cassettes/test_check_whatsapp_contact_list.yaml b/tests/cassettes/test_check_whatsapp_contacts.yaml similarity index 100% rename from tests/cassettes/test_check_whatsapp_contact_list.yaml rename to tests/cassettes/test_check_whatsapp_contacts.yaml diff --git a/tests/test_template_messages.py b/tests/test_template_messages.py index 99170ce..3d5b6d4 100644 --- a/tests/test_template_messages.py +++ b/tests/test_template_messages.py @@ -15,6 +15,7 @@ def test_template_message(client): assert tm == tm assert repr(tm) assert str(tm) + assert tm.to == '5515512345678' @pytest.mark.vcr diff --git a/tests/test_whatsapp.py b/tests/test_whatsapp.py index 5e0d0ed..1476eb0 100644 --- a/tests/test_whatsapp.py +++ b/tests/test_whatsapp.py @@ -4,23 +4,9 @@ @pytest.mark.vcr -def test_check_whatsapp_contact(client): - phone_number = '+55 1 55 1234 5678' - checked_contact = '5515512345678' - assert checked_contact == client.check_whatsapp_contact( - '5215500000000', phone_number - ) - - -@pytest.mark.vcr -def test_invalid_whatsapp_contact(client): - assert client.check_whatsapp_contact('5215500000000', '123') is None - - -@pytest.mark.vcr -def test_check_whatsapp_contact_list(client): +def test_check_whatsapp_contacts(client): contacts = ['+55 1 55 1234 5678', '123'] - result = client.check_whatsapp_contact_list('5215500000000', contacts) + result = client.check_whatsapp_contacts('5215500000000', contacts) assert '+55 1 55 1234 5678' in result # whatsapp assert '123' not in result # no whatsapp @@ -28,4 +14,4 @@ def test_check_whatsapp_contact_list(client): @pytest.mark.vcr def test_invalid_channel(client): with pytest.raises(BotmakerException): - client.check_whatsapp_contact('52 55 1234 5678', '123') + client.check_whatsapp_contacts('52 55 1234 5678', ['123']) From 219bb1c8365351f712d6ed4e97c0051c2ee356c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20L=C3=B3pez?= Date: Wed, 27 Mar 2019 20:02:41 -0600 Subject: [PATCH 6/8] polish --- botmaker/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/botmaker/client.py b/botmaker/client.py index aed8b26..02158df 100644 --- a/botmaker/client.py +++ b/botmaker/client.py @@ -25,7 +25,7 @@ def post(self, endpoint: str, data: dict, **kwargs) -> dict: return self.request('post', endpoint, data, **kwargs) def request( - self, method: str, endpoint: str, data: dict, **kwargs + self, method: str, endpoint: str, data: dict, **kwargs ) -> dict: url = self.BASE_URL + endpoint response = requests.request( @@ -46,7 +46,7 @@ def _check_response(response): response.raise_for_status() def check_whatsapp_contacts( - self, channel: str, phone_numbers: list + self, channel: str, phone_numbers: list ) -> dict: """ Based on From 42ec0a4682e8890d76c03293199a002a5b5cd59b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20L=C3=B3pez?= Date: Wed, 27 Mar 2019 20:06:03 -0600 Subject: [PATCH 7/8] update version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 4c0e574..542722e 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ setuptools.setup( name='botmaker', - version='0.2.2', + version='0.3.0', author='Cuenca', author_email='dev@cuenca.com', description='BotMaker API Client', From 76b176d53b39ace3c97ceb7fcb62481318a4b92e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20L=C3=B3pez?= Date: Wed, 27 Mar 2019 20:10:52 -0600 Subject: [PATCH 8/8] add assert --- tests/test_whatsapp.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_whatsapp.py b/tests/test_whatsapp.py index 1476eb0..a9cdaa4 100644 --- a/tests/test_whatsapp.py +++ b/tests/test_whatsapp.py @@ -8,6 +8,7 @@ def test_check_whatsapp_contacts(client): contacts = ['+55 1 55 1234 5678', '123'] result = client.check_whatsapp_contacts('5215500000000', contacts) assert '+55 1 55 1234 5678' in result # whatsapp + assert result['+55 1 55 1234 5678'] == '5515512345678' assert '123' not in result # no whatsapp