Skip to content

Commit

Permalink
adding advancedMachineDetection to ncco builder and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkahan committed May 15, 2023
1 parent 25f695e commit be341ce
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
13 changes: 13 additions & 0 deletions src/vonage/ncco_builder/ncco.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Connect(Action):
timeout: Optional[int]
limit: Optional[conint(le=7200)]
machineDetection: Optional[Literal['continue', 'hangup']]
advancedMachineDetection: Optional[dict]
eventUrl: Optional[Union[List[str], str]]
eventMethod: Optional[constr(to_upper=True)]
ringbackTone: Optional[str]
Expand Down Expand Up @@ -103,6 +104,18 @@ def check_from_not_set(cls, v, values):
def ensure_url_in_list(cls, v):
return Ncco._ensure_object_in_list(v)

@validator('advancedMachineDetection')
def validate_advancedMachineDetection(cls, v):
if 'behavior' in v and v['behavior'] not in ('continue', 'hangup'):
raise ValueError('advancedMachineDetection["behavior"] must be one of: "continue", "hangup".')
if 'mode' in v and v['mode'] not in ('detect, detect_beep'):
raise ValueError('advancedMachineDetection["mode"] must be one of: "detect", "detect_beep".')
if 'beep_timeout' in v and type(v['beep_timeout']) != int:
raise ValueError('advancedMachineDetection["beep_timeout"] must be an integer.')
if 'beep_timeout' in v and (v['beep_timeout'] < 45 or v['beep_timeout'] > 120):
raise ValueError('advancedMachineDetection["beep_timeout"] must be >= 45 or <= 120.')
return v

class Config:
smart_union = True

Expand Down
2 changes: 2 additions & 0 deletions tests/test_ncco_builder/ncco_samples/ncco_action_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

connect_full = '{"action": "connect", "endpoint": [{"type": "phone", "number": "447000000000"}], "from": "447400000000", "randomFromNumber": false, "eventType": "synchronous", "timeout": 15, "limit": 1000, "machineDetection": "hangup", "eventUrl": ["http://example.com"], "eventMethod": "PUT", "ringbackTone": "http://example.com"}'

connect_advancedMachineDetection = '{"action": "connect", "endpoint": [{"type": "phone", "number": "447000000000"}], "from": "447400000000", "advancedMachineDetection": {"behavior": "continue", "mode": "detect", "beep_timeout": 45}, "eventUrl": ["http://example.com"]}'

talk_basic = '{"action": "talk", "text": "hello"}'

talk_full = '{"action": "talk", "text": "hello", "bargeIn": true, "loop": 3, "level": 0.5, "language": "en-GB", "style": 1, "premium": true}'
Expand Down
16 changes: 16 additions & 0 deletions tests/test_ncco_builder/ncco_samples/ncco_builder_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
ringbackTone='http://example.com',
)

connect_advancedMachineDetection = Ncco.Connect(
endpoint=ConnectEndpoints.PhoneEndpoint(number='447000000000'),
advancedMachineDetection={'behavior': 'continue', 'mode': 'detect', 'beep_timeout': 45},
)


talk_minimal = Ncco.Talk(text='hello')

talk = Ncco.Talk(text='hello', bargeIn=True, loop=3, level=0.5, language='en-GB', style=1, premium=True)
Expand Down Expand Up @@ -69,6 +75,16 @@
{'action': 'talk', 'text': 'hello'},
]

three_part_advancedMachineDetection_ncco = [
{'action': 'record', 'eventUrl': ['http://example.com/events']},
{
'action': 'connect',
'endpoint': [{'type': 'phone', 'number': '447000000000'}],
'advancedMachineDetection': {'behavior': 'continue', 'mode': 'detect', 'beep_timeout': 45},
},
{'action': 'talk', 'text': 'hello'},
]

insane_ncco = [
{'action': 'record', 'eventUrl': ['http://example.com/events']},
{'action': 'conversation', 'name': 'my_conversation'},
Expand Down
22 changes: 22 additions & 0 deletions tests/test_ncco_builder/test_ncco_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ def test_connect_options():
assert json.dumps(_action_as_dict(connect)) == nas.connect_full


def test_connect_advanced_machine_detection():
advancedMachineDetectionParams = {'behavior': 'continue', 'mode': 'detect', 'beep_timeout': 45}
endpoint = ConnectEndpoints.PhoneEndpoint(number='447000000000')
connect = Ncco.Connect(
endpoint=endpoint,
from_='447400000000',
advancedMachineDetection=advancedMachineDetectionParams,
eventUrl='http://example.com',
)
assert json.dumps(_action_as_dict(connect)) == nas.connect_advancedMachineDetection


def test_connect_random_from_number_error():
endpoint = ConnectEndpoints.PhoneEndpoint(number='447000000000')
with pytest.raises(ValueError) as err:
Expand All @@ -143,6 +155,16 @@ def test_connect_validation_errors():
Ncco.Connect(endpoint=endpoint, limit=7201)
with pytest.raises(ValidationError):
Ncco.Connect(endpoint=endpoint, machineDetection='do_nothing')
with pytest.raises(ValidationError):
Ncco.Connect(endpoint=endpoint, advancedMachineDetection={'behavior': 'do_nothing'})
with pytest.raises(ValidationError):
Ncco.Connect(endpoint=endpoint, advancedMachineDetection={'mode': 'detect_nothing'})
with pytest.raises(ValidationError):
Ncco.Connect(endpoint=endpoint, advancedMachineDetection={'beep_timeout': '100'})
with pytest.raises(ValidationError):
Ncco.Connect(endpoint=endpoint, advancedMachineDetection={'beep_timeout': 10})
with pytest.raises(ValidationError):
Ncco.Connect(endpoint=endpoint, advancedMachineDetection={'beep_timeout': 1000})


def test_talk_basic():
Expand Down
9 changes: 2 additions & 7 deletions tests/test_ncco_builder/test_ncco_builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pytest
import json

from vonage import Ncco
Expand All @@ -20,13 +19,9 @@ def test_build_ncco_from_args():


def test_build_ncco_from_list():
action_list = [nbs.record, nbs.talk_minimal]
action_list = [nbs.record, nbs.connect_advancedMachineDetection, nbs.talk_minimal]
ncco = Ncco.build_ncco(actions=action_list)
assert ncco == nbs.two_part_ncco
assert (
json.dumps(ncco)
== '[{"action": "record", "eventUrl": ["http://example.com/events"]}, {"action": "talk", "text": "hello"}]'
)
assert ncco == nbs.three_part_advancedMachineDetection_ncco


def test_build_insane_ncco():
Expand Down

0 comments on commit be341ce

Please sign in to comment.