Skip to content

Commit

Permalink
Merge pull request #6 from cuenca-mx/check-contact-list
Browse files Browse the repository at this point in the history
Check contact list
  • Loading branch information
ricardo8990 authored Mar 29, 2019
2 parents 0f58008 + 76b176d commit 3799702
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 27 deletions.
14 changes: 5 additions & 9 deletions botmaker/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions botmaker/resources/template_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

setuptools.setup(
name='botmaker',
version='0.2.1',
version='0.3.0',
author='Cuenca',
author_email='[email protected]',
description='BotMaker API Client',
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
1 change: 1 addition & 0 deletions tests/test_template_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 7 additions & 12 deletions tests/test_whatsapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])

0 comments on commit 3799702

Please sign in to comment.