|
| 1 | +# Bandwidth Python SDK |
| 2 | + |
| 3 | +## Getting Started |
| 4 | + |
| 5 | +### Installation |
| 6 | + |
| 7 | +``` |
| 8 | +pip install bandwidth-sdk |
| 9 | +``` |
| 10 | + |
| 11 | +### Initialize |
| 12 | + |
| 13 | +``` |
| 14 | +from bandwidth.bandwidth_client import BandwidthClient |
| 15 | +
|
| 16 | +from bandwidth.messaging.models.message_request import MessageRequest |
| 17 | +
|
| 18 | +from bandwidth.voice.models.api_create_call_request import ApiCreateCallRequest |
| 19 | +from bandwidth.voice.bxml.response import Response |
| 20 | +from bandwidth.voice.bxml.verbs import * |
| 21 | +
|
| 22 | +from bandwidth.twofactorauth.models.two_factor_code_request_schema import TwoFactorCodeRequestSchema |
| 23 | +from bandwidth.twofactorauth.models.two_factor_verify_request_schema import TwoFactorVerifyRequestSchema |
| 24 | +
|
| 25 | +from bandwidth.webrtc.models.session import Session |
| 26 | +from bandwidth.webrtc.models.participant import Participant |
| 27 | +from bandwidth.webrtc.models.publish_permission_enum import PublishPermissionEnum |
| 28 | +
|
| 29 | +bandwidth_client = BandwidthClient( |
| 30 | + voice_basic_auth_user_name='username', |
| 31 | + voice_basic_auth_password='password', |
| 32 | + messaging_basic_auth_user_name='username', |
| 33 | + messaging_basic_auth_password='password', |
| 34 | + two_factor_auth_basic_auth_user_name='username', |
| 35 | + two_factor_auth_basic_auth_password='password', |
| 36 | + web_rtc_basic_auth_user_name='username', |
| 37 | + web_rtc_basic_auth_password='password' |
| 38 | +) |
| 39 | +account_id = "12345" |
| 40 | +``` |
| 41 | + |
| 42 | +### Create A Phone Call |
| 43 | + |
| 44 | +``` |
| 45 | +voice_client = bandwidth_client.voice_client.client |
| 46 | +
|
| 47 | +##Create phone call |
| 48 | +body = ApiCreateCallRequest() |
| 49 | +body.mfrom = "+17777777777" |
| 50 | +body.to = "+16666666666" |
| 51 | +body.application_id = "3-d-4-b-5" |
| 52 | +body.answer_url = "https://test.com" |
| 53 | +
|
| 54 | +try: |
| 55 | + response = voice_client.create_call(account_id, body=body) |
| 56 | + print(response.body.call_id) #c-3f758f24-a59bb21e-4f23-4d62-afe9-53o2ls3o4saio4l |
| 57 | + print(response.status_code) #201 |
| 58 | +except ApiErrorResponseException as e: |
| 59 | + print(e.description) #Invalid from: must be an E164 telephone number |
| 60 | + print(e.response_code) #400 |
| 61 | +``` |
| 62 | + |
| 63 | +### Send A Text Message |
| 64 | + |
| 65 | +``` |
| 66 | +messaging_client = bandwidth_client.messaging_client.client |
| 67 | +
|
| 68 | +body = MessageRequest() |
| 69 | +body.application_id = "1-d-b" |
| 70 | +body.to = ["+17777777777"] |
| 71 | +body.mfrom = "+18888888888" |
| 72 | +body.text = "Greetings!" |
| 73 | +
|
| 74 | +try: |
| 75 | + response = messaging_client.create_message(account_id, body) |
| 76 | + print(response.body.id) #1570819529611mexbyfr7ugrouuxy |
| 77 | + print(response.status_code) #202 |
| 78 | +except MessagingException as e: |
| 79 | + print(e.description) #Your request could not be accepted. |
| 80 | + print(e.response_code) #400 |
| 81 | +``` |
| 82 | + |
| 83 | +### Create BXML |
| 84 | + |
| 85 | +``` |
| 86 | +response = Response() |
| 87 | +speak_sentence = SpeakSentence( |
| 88 | + sentence="Test", |
| 89 | + voice="susan", |
| 90 | + locale="en_US", |
| 91 | + gender="female" |
| 92 | +) |
| 93 | +
|
| 94 | +response.add_verb(speak_sentence) |
| 95 | +print(response.to_bxml()) |
| 96 | +``` |
| 97 | + |
| 98 | +### Create A MFA Request |
| 99 | + |
| 100 | +``` |
| 101 | +auth_client = bandwidth_client.two_factor_auth_client.client |
| 102 | +
|
| 103 | +from_phone = "+18888888888" |
| 104 | +to_phone = "+17777777777" |
| 105 | +messaging_application_id = "1-d-b" |
| 106 | +scope = "scope" |
| 107 | +digits = 6 |
| 108 | +
|
| 109 | +body = TwoFactorCodeRequestSchema( |
| 110 | + mfrom = from_phone, |
| 111 | + to = to_phone, |
| 112 | + application_id = messaging_application_id, |
| 113 | + scope = scope, |
| 114 | + digits = digits, |
| 115 | + message = "Your temporary {NAME} {SCOPE} code is {CODE}" |
| 116 | +) |
| 117 | +auth_client.create_messaging_two_factor(account_id, body) |
| 118 | +
|
| 119 | +code = "123456" #This is the user input to validate |
| 120 | +
|
| 121 | +body = TwoFactorVerifyRequestSchema( |
| 122 | + mfrom = from_phone, |
| 123 | + to = to_phone, |
| 124 | + application_id = application_id, |
| 125 | + scope = scope, |
| 126 | + code = code, |
| 127 | + digits = digits, |
| 128 | + expiration_time_in_minutes = 3 |
| 129 | +) |
| 130 | +response = auth_client.create_verify_two_factor(account_id, body) |
| 131 | +print("Auth status: " + str(response.body.valid)) |
| 132 | +``` |
| 133 | + |
| 134 | +### WebRtc Participant & Session Management |
| 135 | + |
| 136 | +``` |
| 137 | +web_rtc_client = bandwidth_client.web_rtc_client.client |
| 138 | +
|
| 139 | +create_session_body = Session() |
| 140 | +create_session_body.tag = 'new-session' |
| 141 | +
|
| 142 | +create_session_response = web_rtc_client.create_session(account_id, create_session_body) |
| 143 | +session_id = create_session_response.body.id |
| 144 | +
|
| 145 | +create_participant_body = Participant() |
| 146 | +create_participant_body.publish_permissions = [ |
| 147 | + PublishPermissionEnum.AUDIO, |
| 148 | + PublishPermissionEnum.VIDEO |
| 149 | +] |
| 150 | +create_participant_body.callback_url = "https://sample.com" |
| 151 | +
|
| 152 | +create_participant_response = web_rtc_client.create_participant(account_id, create_participant_body) |
| 153 | +participant_id = create_participant_response.body.participant.id |
| 154 | +
|
| 155 | +web_rtc_client.add_participant_to_session(account_id, session_id, participant_id) |
| 156 | +``` |
| 157 | + |
| 158 | +## Supported Python Versions |
| 159 | + |
| 160 | +This package can be used with Python >= 3.0 |
| 161 | + |
| 162 | +## Documentation |
| 163 | + |
| 164 | +Documentation for this package can be found at https://dev.bandwidth.com/sdks/python.html |
| 165 | + |
| 166 | +## Credentials |
| 167 | + |
| 168 | +Information for credentials for this package can be found at https://dev.bandwidth.com/guides/accountCredentials.html |
0 commit comments