Examples
Follow the examples to see how’s easy to use the Relay SDK to interact with inbound or outbound calls.
Inbound Calls
Using SignalWire.Relay.Consumer to manage inbound calls from both
homeandofficecontexts. Answer the call, ask the user to enter their PIN and playback the digits sent if successful.
1 using SignalWire.Relay; 2 using SignalWire.Relay.Calling; 3 using System; 4 using System.Collections.Generic; 5 6 namespace Example 7 { 8 internal class ExampleConsumer : Consumer 9 { 10 protected override void Setup() 11 { 12 Project = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"; 13 Token = "PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 14 Contexts = new List<string> { "home", "office" }; 15 } 16 17 // This is executed in a new thread each time, so it is safe to use blocking calls 18 protected override void OnIncomingCall(Call call) 19 { 20 // Answer the incoming call, block until it's answered or an error occurs 21 AnswerResult resultAnswer = call.Answer(); 22 23 // Prompt with TTS and collect the PIN, block until it's finished or an error occurs 24 PromptResult resultPrompt = call.PromptTTS( 25 "Welcome to SignalWire! Please enter your PIN", 26 new CallCollect 27 { 28 InitialTimeout = 10, 29 Digits = new CallCollect.DigitsParams { Max = 4, DigitTimeout = 5 } 30 }); 31 32 if (resultPrompt.Successful) 33 { 34 // Play back what was collected 35 call.PlayTTS("You entered " + resultPrompt.Result + ". Thanks and good bye!"); 36 } 37 38 // Hangup 39 call.Hangup(); 40 } 41 } 42 43 internal class Program 44 { 45 public static void Main() 46 { 47 new ExampleConsumer().Run(); 48 } 49 } 50 }
Outbound Calls
Make a call and, on
answered, prompt the user to enter digits.
1 using SignalWire.Relay; 2 using SignalWire.Relay.Calling; 3 using System; 4 5 namespace Example 6 { 7 internal class ExampleConsumer : Consumer 8 { 9 protected override void Setup() 10 { 11 Project = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"; 12 Token = "PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 13 } 14 15 protected override void Ready() 16 { 17 // Create a new phone call and dial it immediately, block until it's answered, times out, 18 // busy, or another error occurs 19 DialResult resultDial = Client.Calling.DialPhone("+1XXXXXXXXXX", "+1YYYYYYYYYY"); 20 21 // Prompt with TTS and collect the PIN, block until it's finished or an error occurs 22 PromptResult resultPrompt = resultDial.Call.PromptTTS( 23 "Welcome to SignalWire! Please enter your PIN", 24 new CallCollect 25 { 26 InitialTimeout = 10, 27 Digits = new CallCollect.DigitsParams { Max = 4, DigitTimeout = 5 } 28 }); 29 30 if (resultPrompt.Successful) 31 { 32 // Play back what was collected 33 resultDial.Call.PlayTTS("You entered " + resultPrompt.Result + ". Thanks and good bye!"); 34 } 35 36 // Hangup 37 resultDial.Call.Hangup(); 38 } 39 } 40 41 internal class Program 42 { 43 public static void Main() 44 { 45 new ExampleConsumer().Run(); 46 } 47 } 48 }