|
2 | 2 |
|
3 | 3 | This package contains the code to use [Vonage's Voice API](https://developer.vonage.com/en/voice/voice-api/overview) in Python. This package includes methods for working with the Voice API. It also contains an NCCO (Call Control Object) builder to help you to control call flow.
|
4 | 4 |
|
| 5 | +## Structure |
| 6 | + |
| 7 | +There is a `Voice` class which contains the methods used to call Vonage APIs. To call many of the APIs, you need to pass a Pydantic model with the required options. These can be accessed from the `vonage_voice.models` subpackage. Errors can be accessed from the `vonage_voice.errors` module. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +It is recommended to use this as part of the main `vonage` package. The examples below assume you've created an instance of the `vonage.Vonage` class called `vonage_client`, like so: |
| 12 | + |
| 13 | +```python |
| 14 | +from vonage import Vonage, Auth |
| 15 | + |
| 16 | +vonage_client = Vonage(Auth('MY_AUTH_INFO')) |
| 17 | +``` |
| 18 | + |
| 19 | +### Create a Call |
| 20 | + |
| 21 | +To create a call, you must pass an instance of the `CreateCallRequest` model to the `create_call` method. If supplying an NCCO, import the NCCO actions you want to use and pass them in as a list to the `ncco` model field. |
| 22 | + |
| 23 | +```python |
| 24 | +from vonage_voice.models import CreateCallRequest, Talk |
| 25 | + |
| 26 | +ncco = [Talk(text='Hello world', loop=3, language='en-GB')] |
| 27 | + |
| 28 | +call = CreateCallRequest( |
| 29 | + to=[{'type': 'phone', 'number': '1234567890'}], |
| 30 | + ncco=ncco, |
| 31 | + random_from_number=True, |
| 32 | +) |
| 33 | + |
| 34 | +response = vonage_client.voice.create_call(call) |
| 35 | +print(response.model_dump()) |
| 36 | +``` |
| 37 | + |
| 38 | +### List Calls |
| 39 | + |
| 40 | +```python |
| 41 | +# Gets the first 100 results and the record_index of the |
| 42 | +# next page if there's more than 100 |
| 43 | +calls, next_record_index = vonage_client.voice.list_calls() |
| 44 | + |
| 45 | +# Specify filtering options |
| 46 | +from vonage_voice.models import ListCallsFilter |
| 47 | + |
| 48 | +call_filter = ListCallsFilter( |
| 49 | + status='completed', |
| 50 | + date_start='2024-03-14T07:45:14Z', |
| 51 | + date_end='2024-04-19T08:45:14Z', |
| 52 | + page_size=10, |
| 53 | + record_index=0, |
| 54 | + order='asc', |
| 55 | + conversation_uuid='CON-2be039b2-d0a4-4274-afc8-d7b241c7c044', |
| 56 | +) |
| 57 | + |
| 58 | +calls, next_record_index = vonage_client.voice.list_calls(call_filter) |
| 59 | +``` |
| 60 | + |
| 61 | +### Get Information About a Specific Call |
| 62 | + |
| 63 | +```python |
| 64 | +call = vonage_client.voice.get_call('CALL_ID') |
| 65 | +``` |
| 66 | + |
| 67 | +### Transfer a Call to a New NCCO |
| 68 | + |
| 69 | +```python |
| 70 | +ncco = [Talk(text='Hello world')] |
| 71 | +vonage_client.voice.transfer_call_ncco('UUID', ncco) |
| 72 | +``` |
| 73 | + |
| 74 | +### Transfer a Call to a New Answer URL |
| 75 | + |
| 76 | +```python |
| 77 | +vonage_client.voice.transfer_call_answer_url('UUID', 'ANSWER_URL') |
| 78 | +``` |
| 79 | + |
| 80 | +### Hang Up a Call |
| 81 | + |
| 82 | +End the call for a specified UUID, removing them from it. |
| 83 | + |
| 84 | +```python |
| 85 | +vonage_client.voice.hangup('UUID') |
| 86 | +``` |
| 87 | + |
| 88 | +### Mute/Unmute a Participant |
| 89 | + |
| 90 | +```python |
| 91 | +vonage_client.voice.mute('UUID') |
| 92 | +vonage_client.voice.unmute('UUID') |
| 93 | +``` |
| 94 | + |
| 95 | +### Earmuff/Unearmuff a UUID |
| 96 | + |
| 97 | +Prevent/allow a specified UUID participant to be able to hear audio. |
| 98 | + |
| 99 | +```python |
| 100 | +vonage_client.voice.earmuff('UUID') |
| 101 | +vonage_client.voice.unearmuff('UUID') |
| 102 | +``` |
| 103 | + |
| 104 | +### Play Audio Into a Call |
| 105 | + |
| 106 | +```python |
| 107 | +from vonage_voice.models import AudioStreamOptions |
| 108 | + |
| 109 | +# Only the `stream_url` option is required |
| 110 | +options = AudioStreamOptions( |
| 111 | + stream_url=['https://example.com/audio'], loop=2, level=0.5 |
| 112 | +) |
| 113 | +response = vonage_client.voice.play_audio_into_call('UUID', options) |
| 114 | +``` |
| 115 | + |
| 116 | +### Stop Playing Audio Into a Call |
| 117 | + |
| 118 | +```python |
| 119 | +vonage_client.voice.stop_audio_stream('UUID') |
| 120 | +``` |
| 121 | + |
| 122 | +### Play TTS Into a Call |
| 123 | + |
| 124 | +```python |
| 125 | +from vonage_voice.models import TtsStreamOptions |
| 126 | + |
| 127 | +# Only the `text` field is required |
| 128 | +options = TtsStreamOptions( |
| 129 | + text='Hello world', language='en-ZA', style=1, premium=False, loop=2, level=0.5 |
| 130 | +) |
| 131 | +response = voice.play_tts_into_call('UUID', options) |
| 132 | +``` |
| 133 | + |
| 134 | +### Stop Playing TTS Into a Call |
| 135 | + |
| 136 | +```python |
| 137 | +vonage_client.voice.stop_tts('UUID') |
| 138 | +``` |
| 139 | + |
| 140 | +### Play DTMF Tones Into a Call |
| 141 | + |
| 142 | +```python |
| 143 | +response = voice.play_dtmf_into_call('UUID', '1234*#') |
| 144 | +``` |
0 commit comments