|
1 | 1 | # Bandwidth Java SDK
|
2 |
| - |
3 |
| -Bandwidth's API docs can be found at https://dev.bandwidth.com |
4 | 2 |
|
5 |
| -Java specific docs can be found at https://dev.bandwidth.com/sdks/java.html |
| 3 | +## Getting Started |
6 | 4 |
|
7 |
| -## Download & Install |
| 5 | +### Installation |
8 | 6 |
|
9 |
| -Maven: |
| 7 | +Add the following dependency to your `pom.xml` file |
10 | 8 |
|
11 |
| -```xml |
| 9 | +``` |
12 | 10 | <!-- https://mvnrepository.com/artifact/com.bandwidth.sdk/bandwidth-sdk -->
|
13 | 11 | <dependency>
|
14 | 12 | <groupId>com.bandwidth.sdk</groupId>
|
15 | 13 | <artifactId>bandwidth-sdk</artifactId>
|
16 |
| - <version>1.0.0</version> |
| 14 | + <version>{version}</version> |
17 | 15 | </dependency>
|
18 | 16 | ```
|
19 | 17 |
|
20 |
| -## Initialize Bandwidth Client |
21 |
| - |
22 |
| -```java |
| 18 | +### Initialize |
23 | 19 |
|
24 |
| -//Set the voice client configuration with credentials |
| 20 | +``` |
25 | 21 | BandwidthClient client = new BandwidthClient.Builder()
|
26 |
| - .messagingBasicAuthCredentials("MESSAGING_API_TOKEN", "MESSAGING_API_SECRET") |
27 |
| - .voiceBasicAuthCredentials("VOICE_API_USERNAME", "VOICE_API_PASSWORD") |
28 |
| - .environment(Environment.PRODUCTION) |
| 22 | + .messagingBasicAuthCredentials("username", "password") |
| 23 | + .voiceBasicAuthCredentials("username", "password") |
| 24 | + .twoFactorAuthBasicAuthCredentials("username", "password") |
| 25 | + .webRtcBasicAuthCredentials("username", "password") |
29 | 26 | .build();
|
| 27 | +String accountId = "12345"; |
| 28 | +``` |
| 29 | + |
| 30 | +### Create A Phone Call |
30 | 31 |
|
31 |
| -//Fully qualified name to remove confilicts |
32 |
| -com.bandwidth.messaging.controllers.APIController messagingController = client.getMessagingClient().getAPIController(); |
| 32 | +``` |
33 | 33 | com.bandwidth.voice.controllers.APIController voiceController = client.getVoiceClient().getAPIController();
|
34 | 34 |
|
| 35 | +String to = "+15554443333"; |
| 36 | +String from = "+15553334444"; |
| 37 | +String applicationId = "3-a-b-c"; |
| 38 | +String answerUrl = "https://sample.com"; |
| 39 | +
|
| 40 | +ApiCreateCallRequest body = new ApiCreateCallRequest(); |
| 41 | +body.setTo(to); |
| 42 | +body.setFrom(from); |
| 43 | +body.setApplicationId(applicationId); |
| 44 | +body.setAnswerUrl(answerUrl); |
| 45 | +
|
| 46 | +ApiResponse<ApiCallResponse> createCallResponse = voiceController.createCall(accountId, body); |
| 47 | +System.out.println(createCallResponse.getResult().getCallId()); |
35 | 48 | ```
|
36 | 49 |
|
37 |
| -## Create Phone Call |
| 50 | +### Send A Text Message |
38 | 51 |
|
39 |
| -```java |
40 |
| -import com.bandwidth.voice.models.ApiCreateCallRequest; |
| 52 | +``` |
| 53 | +String to = "+15554443333"; |
| 54 | +ArrayList<String> toNumbers = new ArrayList<String>(); |
| 55 | +toNumbers.add(to); |
| 56 | +String from = "+15553334444"; |
| 57 | +String applicationId = "3-a-b-d"; |
| 58 | +String text = "Hello from Java"; |
| 59 | +
|
| 60 | +MessageRequest body = new MessageRequest(); |
| 61 | +body.setTo(toNumbers); |
| 62 | +body.setFrom(from); |
| 63 | +body.setText(text); |
| 64 | +body.setApplicationId(applicationId); |
| 65 | +
|
| 66 | +ApiResponse<BandwidthMessage> createMessageResponse = messagingController.createMessage(accountId, body); |
| 67 | +System.out.println(createMessageResponse.getResult().getMessageId()); |
| 68 | +``` |
41 | 69 |
|
42 |
| -//Create the ApiCreateCallRequst object and populate. |
43 |
| -ApiCreateCallRequest callRequest = new ApiCreateCallRequest(); |
| 70 | +### Create BXML |
44 | 71 |
|
45 |
| -callRequest.setApplicationId("application.Id"); |
46 |
| -callRequest.setTo("+19999999999"); |
47 |
| -callRequest.setAnswerUrl("https://test.com"); |
48 |
| -callRequest.setFrom("+17777777777"); |
| 72 | +``` |
| 73 | +SpeakSentence speakSentence = SpeakSentence.builder() |
| 74 | + .text("Hello world") |
| 75 | + .voice("susan") |
| 76 | + .gender("female") |
| 77 | + .locale("en_US") |
| 78 | + .build(); |
| 79 | +
|
| 80 | +String response = new Response() |
| 81 | + .add(speakSentence) |
| 82 | + .toBXML(); |
| 83 | +System.out.println(response); |
| 84 | +``` |
49 | 85 |
|
50 |
| -//The voice client createCall can throw these exceptions. |
51 |
| -try { |
52 |
| - ApiResponse<ApiCallResponse> response = voiceController.createCall("account.id", callRequest); |
53 |
| - System.out.println(response.getResult().getCallId()); |
54 |
| -} catch (IOException | ApiException e) { |
55 |
| - //Handle |
56 |
| -} |
| 86 | +### Create A MFA Request |
57 | 87 |
|
| 88 | +``` |
| 89 | +String to = "+15554443333"; |
| 90 | +String from = "+15553334444"; |
| 91 | +String applicationId = "3-a-c-b"); |
| 92 | +String scope = "scope"; |
| 93 | +int digits = 6; |
| 94 | +String message = "Your temporary {NAME} {SCOPE} code is {CODE}"; |
| 95 | +
|
| 96 | +TwoFactorCodeRequestSchema body = new TwoFactorCodeRequestSchema(); |
| 97 | +body.setTo(to); |
| 98 | +body.setFrom(from); |
| 99 | +body.setApplicationId(applicationId); |
| 100 | +body.setScope(scope); |
| 101 | +body.setDigits(digits); |
| 102 | +body.setMessage(message); |
| 103 | +
|
| 104 | +mfaController.createVoiceTwoFactor(accountId, body); |
| 105 | +
|
| 106 | +String code = "123456"; //this is the user code to verify |
| 107 | +int expirationTimeInMinutes = 3; |
| 108 | +
|
| 109 | +TwoFactorVerifyRequestSchema body = new TwoFactorVerifyRequestSchema(); |
| 110 | +body.setTo(to); |
| 111 | +body.setApplicationId(applicationId); |
| 112 | +body.setScope(scope); |
| 113 | +body.setCode(code); |
| 114 | +body.setExpirationTimeInMinutes(expirationTimeInMinutes); |
| 115 | +
|
| 116 | +ApiResponse<TwoFactorVerifyCodeResponse> response = mfaController.createVerifyTwoFactor(accountId, body); |
| 117 | +System.out.println(response.getResult().getValid()); |
58 | 118 | ```
|
59 | 119 |
|
60 |
| -## Generate BXML |
| 120 | +### WebRtc Participant & Session Management |
61 | 121 |
|
62 |
| -```java |
63 |
| -import com.bandwidth.sdk.voice.models.verbs.*; |
| 122 | +``` |
| 123 | +Session createSessionBody = new Session(); |
| 124 | +createSessionBody.setTag("new-session"); |
64 | 125 |
|
65 |
| -//Create a Bandwidth XML (BXML) SpeakSentence Verb. Supply the sentence to be spoken. |
66 |
| -SpeakSentence speakSentence = SpeakSentence.builder() |
67 |
| - .text("Hello World") |
68 |
| - .build(); |
| 126 | +ApiResponse<Session> createSessionResponse = webrtcController.createSession(accountId, createSessionBody); |
| 127 | +String sessionId = createSessionResponse.getResult().getId(); |
69 | 128 |
|
70 |
| -//Create the response object and add the speakSentence verb to the response. |
71 |
| -Response response = Response.builder().build().add(speakSentence); |
| 129 | +Participant createParticipantBody = new Participant(); |
| 130 | +createParticipantBody.setCallbackUrl("https://sample.com"); |
| 131 | +ArrayList<PublishPermissionEnum> publishPermissions = new ArrayList<PublishPermissionEnum>(); |
| 132 | +publishPermissions.add(PublishPermissionEnum.AUDIO); |
| 133 | +publishPermissions.add(PublishPermissionEnum.VIDEO); |
72 | 134 |
|
73 |
| -//view the BXML |
74 |
| -System.out.println( response.toXml() ) |
| 135 | +ApiResponse<AccountsParticipantsResponse> createParticipantResponse = webrtcController.createParticipant(accountId, createParticipantBody); |
| 136 | +String participantId = createParticipantResponse.getResult().getParticipant().getId(); |
75 | 137 |
|
| 138 | +webrtcController.addParticipantToSession(accountId, sessionId, participantId, null); |
76 | 139 | ```
|
77 | 140 |
|
78 |
| -## Send Text Message |
| 141 | +## Supported Java Versions |
79 | 142 |
|
80 |
| -```java |
81 |
| -import com.bandwidth.messaging.models.MessageRequest; |
| 143 | +This package can be used with Java >= 1.8 |
82 | 144 |
|
83 |
| -MessageRequest messageRequest = new MessageRequest(); |
| 145 | +## Documentation |
84 | 146 |
|
85 |
| -List<String> toNumbers = new ArrayList<>(); |
| 147 | +Documentation for this package can be found at https://dev.bandwidth.com/sdks/java.html |
86 | 148 |
|
87 |
| -toNumbers.add("+12345678902"); |
| 149 | +## Credentials |
88 | 150 |
|
89 |
| -messageRequest.setApplicationId(MSG_APPLICATION_ID); |
90 |
| -messageRequest.setText("Hey, check this out!"); |
91 |
| -messageRequest.setFrom("+12345678901"); |
92 |
| -messageRequest.setTo( toNumbers ); |
93 |
| -messageRequest.setTag("test tag"); |
| 151 | +Information for credentials for this package can be found at https://dev.bandwidth.com/guides/accountCredentials.html |
94 | 152 |
|
95 |
| -try { |
96 |
| - ApiResponse<BandwidthMessage> response = messagingController.createMessage(accountId, messageRequest); |
97 |
| - System.out.println(response.getResult().getId()); |
98 |
| -} catch (ApiException | IOException e){ |
99 |
| - //Handle |
100 |
| -} |
101 |
| -``` |
|
0 commit comments