Skip to content

Commit 3799702

Browse files
authored
Merge pull request #6 from cuenca-mx/check-contact-list
Check contact list
2 parents 0f58008 + 76b176d commit 3799702

File tree

6 files changed

+19
-27
lines changed

6 files changed

+19
-27
lines changed

botmaker/client.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,19 @@ def _check_response(response):
4545
else:
4646
response.raise_for_status()
4747

48-
def check_whatsapp_contact(
49-
self, channel: str, phone_number: str
50-
) -> Optional[str]:
48+
def check_whatsapp_contacts(
49+
self, channel: str, phone_numbers: list
50+
) -> dict:
5151
"""
5252
Based on
5353
https://botmakeradmin.github.io/docs/es/#/messages-api?id=chequear-validez-de-n%C3%BAmeros-de-contactos-de-whatsapp
5454
"""
5555
channel = sanitize_phone_number(channel)
56-
data = dict(chatChannelNumber=channel, contacts=[phone_number])
56+
data = dict(chatChannelNumber=channel, contacts=phone_numbers)
5757
resp = self.post('/customer/checkWhatsAppContact', data)
5858
try:
5959
result = resp['result']
6060
except KeyError:
6161
# This should never happen
6262
raise BotmakerException("Expected 'result' in the response body")
63-
try:
64-
checked = result[phone_number]
65-
except KeyError:
66-
checked = None
67-
return checked
63+
return result

botmaker/resources/template_messages.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ def create(
3838
"""
3939
from_ = sanitize_phone_number(from_)
4040
if chat_platform == 'whatsapp':
41-
checked = cls._client.check_whatsapp_contact(from_, to)
42-
if not checked:
41+
check_dict = cls._client.check_whatsapp_contacts(from_, [to])
42+
if to not in check_dict:
4343
raise InvalidPhoneNumber(
4444
f"'{to} is not from valid WhatsApp contact")
4545
else:
46-
to = checked
46+
to = check_dict[to]
4747
else:
4848
to = sanitize_phone_number(to)
4949
data = dict(

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
setuptools.setup(
1515
name='botmaker',
16-
version='0.2.1',
16+
version='0.3.0',
1717
author='Cuenca',
1818
author_email='dev@cuenca.com',
1919
description='BotMaker API Client',

tests/cassettes/test_check_whatsapp_contact.yaml renamed to tests/cassettes/test_check_whatsapp_contacts.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
interactions:
22
- request:
3-
body: '{"contacts": ["+55 1 55 1234 5678"], "chatChannelNumber": "5215500000000"}'
3+
body: '{"contacts": ["+55 1 55 1234 5678","123"], "chatChannelNumber": "5215500000000"}'
44
headers:
55
Accept: ['*/*']
66
Accept-Encoding: ['[application/json, application/xml, text/plain]']
77
Connection: [keep-alive]
8-
Content-Length: ['74']
8+
Content-Length: ['80']
99
Content-Type: [application/json]
1010
User-Agent: [python-requests/2.21.0]
1111
access-token: [DUMMY]

tests/test_template_messages.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def test_template_message(client):
1515
assert tm == tm
1616
assert repr(tm)
1717
assert str(tm)
18+
assert tm.to == '5515512345678'
1819

1920

2021
@pytest.mark.vcr

tests/test_whatsapp.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,15 @@
44

55

66
@pytest.mark.vcr
7-
def test_check_whatsapp_contact(client):
8-
phone_number = '+55 1 55 1234 5678'
9-
checked_contact = '5515512345678'
10-
assert checked_contact == client.check_whatsapp_contact(
11-
'5215500000000', phone_number
12-
)
13-
14-
15-
@pytest.mark.vcr
16-
def test_invalid_whatsapp_contact(client):
17-
assert client.check_whatsapp_contact('5215500000000', '123') is None
7+
def test_check_whatsapp_contacts(client):
8+
contacts = ['+55 1 55 1234 5678', '123']
9+
result = client.check_whatsapp_contacts('5215500000000', contacts)
10+
assert '+55 1 55 1234 5678' in result # whatsapp
11+
assert result['+55 1 55 1234 5678'] == '5515512345678'
12+
assert '123' not in result # no whatsapp
1813

1914

2015
@pytest.mark.vcr
2116
def test_invalid_channel(client):
2217
with pytest.raises(BotmakerException):
23-
client.check_whatsapp_contact('52 55 1234 5678', '123')
18+
client.check_whatsapp_contacts('52 55 1234 5678', ['123'])

0 commit comments

Comments
 (0)