|
1 | | -# Bandwidth Python SDK |
2 | | - |
3 | | -Bandwidth's API docs can be found at https://dev.bandwidth.com |
4 | | - |
5 | | -Python specific docs can be found at https://dev.bandwidth.com/sdks/python.html |
6 | | - |
7 | | -# Python SDK |
8 | | - |
9 | | -## Download & Install |
10 | | - |
11 | | -``` |
12 | | -pip install bandwidth-sdk |
13 | | -``` |
14 | | - |
15 | | -## Initialize Bandwidth Client |
16 | | - |
17 | | -```python |
18 | | -from bandwidth.bandwidth_client import BandwidthClient |
19 | | - |
20 | | -from bandwidth.messaging.models.message_request import MessageRequest |
21 | | -from bandwidth.messaging.exceptions.generic_client_exception import GenericClientException |
22 | | -from bandwidth.messaging.exceptions.path_client_exception import PathClientException |
23 | | - |
24 | | -from bandwidth.voice.models.api_create_call_request import ApiCreateCallRequest |
25 | | -from bandwidth.voice.models.modify_call_recording_state import ModifyCallRecordingState |
26 | | -from bandwidth.voice.exceptions.error_response_exception import ErrorResponseException |
27 | | -from bandwidth.voice.bxml.response import Response |
28 | | -from bandwidth.voice.bxml.verbs import * |
29 | | - |
30 | | -##Initialize client |
31 | | -voice_basic_auth_user_name = 'username' |
32 | | -voice_basic_auth_password = 'password' |
33 | | -messaging_basic_auth_user_name = 'token' |
34 | | -messaging_basic_auth_password = 'secret' |
35 | | - |
36 | | -bandwidth_client = BandwidthClient( |
37 | | - voice_basic_auth_user_name=voice_basic_auth_user_name, |
38 | | - voice_basic_auth_password=voice_basic_auth_password, |
39 | | - messaging_basic_auth_user_name=messaging_basic_auth_user_name, |
40 | | - messaging_basic_auth_password=messaging_basic_auth_password) |
41 | | -``` |
42 | | - |
43 | | -## Create Phone Call |
44 | | - |
45 | | -```python |
46 | | -voice_client = bandwidth_client.voice_client.client |
47 | | -account_id = "1" |
48 | | - |
49 | | -##Create phone call |
50 | | -body = ApiCreateCallRequest() |
51 | | -body.mfrom = "+17777777777" |
52 | | -body.to = "+16666666666" |
53 | | -body.application_id = "3-d-4-b-5" |
54 | | -body.answer_url = "https://test.com" |
55 | | - |
56 | | -try: |
57 | | - response = voice_client.create_call(account_id, body=body) |
58 | | - print(response.body.call_id) #c-3f758f24-a59bb21e-4f23-4d62-afe9-53o2ls3o4saio4l |
59 | | - print(response.status_code) #201 |
60 | | -except ErrorResponseException as e: |
61 | | - print(e.description) #Invalid from: must be an E164 telephone number |
62 | | - print(e.response_code) #400 |
63 | | -``` |
64 | | - |
65 | | -## Generate BXML |
66 | | - |
67 | | -```python |
68 | | -response = Response() |
69 | | -speak_sentence = SpeakSentence( |
70 | | - sentence="Test", |
71 | | - voice="susan", |
72 | | - locale="en_US", |
73 | | - gender="female" |
74 | | -) |
75 | | - |
76 | | -response.add_verb(speak_sentence) |
77 | | -print(response.to_bxml()) |
78 | | -``` |
79 | | - |
80 | | -## Send Text Message |
81 | | - |
82 | | -```python |
83 | | -messaging_client = bandwidth_client.messaging_client.client |
84 | | -account_id = "1" |
85 | | - |
86 | | -body = MessageRequest() |
87 | | -body.application_id = "1-d-b" |
88 | | -body.to = ["+17777777777"] |
89 | | -body.mfrom = "+18888888888" |
90 | | -body.text = "Greetings!" |
91 | | - |
92 | | -try: |
93 | | - response = messaging_client.create_message(account_id, body=body) |
94 | | - print(response.body.id) #1570819529611mexbyfr7ugrouuxy |
95 | | - print(response.status_code) #202 |
96 | | -except GenericClientException as e: |
97 | | - print(e.description) #Your request could not be accepted. |
98 | | - print(e.response_code) #400 |
99 | | -except PathClientException as e: |
100 | | - print(e.message) #Access is denied |
101 | | - print(e.response_code) #403 |
102 | | -``` |
0 commit comments