diff --git a/botmaker/client.py b/botmaker/client.py index 6ff13dd..02158df 100644 --- a/botmaker/client.py +++ b/botmaker/client.py @@ -45,23 +45,19 @@ def _check_response(response): else: response.raise_for_status() - def check_whatsapp_contact( - self, channel: str, phone_number: str - ) -> Optional[str]: + def check_whatsapp_contacts( + self, channel: str, phone_numbers: 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_number]) + 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") - try: - checked = result[phone_number] - except KeyError: - checked = None - return checked + return result 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/setup.py b/setup.py index b12fecc..542722e 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ setuptools.setup( name='botmaker', - version='0.2.1', + version='0.3.0', author='Cuenca', author_email='dev@cuenca.com', description='BotMaker API Client', diff --git a/tests/cassettes/test_check_whatsapp_contact.yaml b/tests/cassettes/test_check_whatsapp_contacts.yaml similarity index 91% rename from tests/cassettes/test_check_whatsapp_contact.yaml rename to tests/cassettes/test_check_whatsapp_contacts.yaml index 5ea8d9d..e8945ff 100644 --- a/tests/cassettes/test_check_whatsapp_contact.yaml +++ b/tests/cassettes/test_check_whatsapp_contacts.yaml @@ -1,11 +1,11 @@ interactions: - request: - body: '{"contacts": ["+55 1 55 1234 5678"], "chatChannelNumber": "5215500000000"}' + 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: ['74'] + Content-Length: ['80'] Content-Type: [application/json] User-Agent: [python-requests/2.21.0] access-token: [DUMMY] 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 25645a2..a9cdaa4 100644 --- a/tests/test_whatsapp.py +++ b/tests/test_whatsapp.py @@ -4,20 +4,15 @@ @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 +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 @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'])