Repository navigation
Expand file tree
/
Copy pathRelayAnswerAndWelcome.java
More file actions
71 lines (60 loc) · 2.43 KB
/
Copy pathRelayAnswerAndWelcome.java
File metadata and controls
71 lines (60 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* RelayAnswerAndWelcome -- answer an inbound call, play a TTS greeting,
* and hang up. This is the canonical RELAY "hello world": the smallest
* SDK program that proves authenticated WebSocket connection +
* inbound-call dispatch + TTS playback + clean hangup all work end to end.
*
* <p>Mirror of Python's {@code examples/relay_answer_and_welcome.py}.
*
* <p>Required environment variables:
* <ul>
* <li>{@code SIGNALWIRE_PROJECT_ID} -- SignalWire project ID</li>
* <li>{@code SIGNALWIRE_API_TOKEN} -- SignalWire API token</li>
* <li>{@code SIGNALWIRE_SPACE} -- e.g. {@code example.signalwire.com}</li>
* </ul>
*
* <p>Optional:
* <ul>
* <li>{@code SIGNALWIRE_CONTEXT} -- defaults to {@code default}</li>
* <li>{@code SIGNALWIRE_LOG_LEVEL=debug} for the full RELAY trace</li>
* </ul>
*/
import com.signalwire.sdk.relay.RelayClient;
import java.util.List;
import java.util.Map;
public class RelayAnswerAndWelcome {
public static void main(String[] args) throws Exception {
String project = System.getenv("SIGNALWIRE_PROJECT_ID");
String token = System.getenv("SIGNALWIRE_API_TOKEN");
String space = System.getenv("SIGNALWIRE_SPACE");
String context = System.getenv().getOrDefault("SIGNALWIRE_CONTEXT", "default");
if (project == null || token == null || space == null) {
System.err.println(
"RelayAnswerAndWelcome: SIGNALWIRE_PROJECT_ID, "
+ "SIGNALWIRE_API_TOKEN, and SIGNALWIRE_SPACE are required.");
System.exit(2);
}
var client = RelayClient.builder()
.project(project)
.token(token)
.space(space)
.contexts(List.of(context))
.build();
client.onCall(call -> {
System.out.println("Incoming call: " + call.getCallId());
// Answer the call.
call.answer();
// Play TTS greeting and wait for completion.
var action = call.play(List.of(Map.of(
"type", "tts",
"params", Map.of("text", "Welcome to SignalWire!")
)));
action.waitForCompletion();
// Hang up.
call.hangup();
System.out.println("Call ended: " + call.getCallId());
});
System.out.println("Waiting for inbound calls on context '" + context + "' ...");
client.run();
}
}