Examples

View as Markdown

Follow the examples to see how’s easy to use the Relay SDK to interact with inbound or outbound calls.

Outbound Calls

Using a Relay Consumer to make an outbound call and say “Welcome to SignalWire!”, then hangup.

1import logging
2from signalwire.relay.consumer import Consumer
3
4class CustomConsumer(Consumer):
5 def setup(self):
6 self.project = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
7 self.token = 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
8 self.contexts = ['office']
9
10 async def ready(self):
11 logging.info('CustomConsumer is ready!')
12 # Replace numbers with yours!
13 dial_result = await self.client.calling.dial(to_number='+18991114443', from_number='+18991114444')
14 if dial_result.successful is False:
15 logging.info('Outbound call not answered or failed')
16 return
17 # Take the Call object from "dial_result"
18 call = dial_result.call
19 await call.play_tts(text='Welcome to SignalWire!')
20 logging.info('Hanging up..')
21 await call.hangup()
22
23consumer = CustomConsumer()
24consumer.run()

Connect inbound calls with a series of devices

Using a Relay Consumer to manage inbound calls from the office context. Answer the call, try to connect it with 3 devices and wait until the remote call will be hanged up.

1from signalwire.relay.consumer import Consumer
2
3class CustomConsumer(Consumer):
4 def setup(self):
5 self.project = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
6 self.token = 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
7 self.contexts = ['office']
8 self.devices = [
9 { 'to_number': '+18991114443' },
10 { 'to_number': '+18991114444' },
11 { 'to_number': '+18991114445' }
12 ]
13
14 async def on_incoming_call(self, call):
15 result = await call.answer()
16 if result.successful is False:
17 print('Error answering the call')
18 return
19 result = await call.connect(device_list=self.devices)
20 if result.successful:
21 remote_call = result.call
22 # Wait until the remote leg is ended..
23 await remote_call.wait_for_ended()
24 await call.hangup()
25
26consumer = CustomConsumer()
27consumer.run()

Send an SMS

Using a Relay Consumer to send SMS.

1import logging
2from signalwire.relay.consumer import Consumer
3
4class CustomConsumer(Consumer):
5 def setup(self):
6 self.project = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
7 self.token = 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
8 self.contexts = ['office']
9
10 async def ready(self):
11 logging.info('CustomConsumer is ready!')
12 # Replace numbers with yours!
13 result = await self.client.messaging.send(context='office', to_number='+18991114443', from_number='+18991114444', body='Welcome to SignalWire!')
14 if result.successful:
15 logging.info(f'Message sent. ID: {result.message_id}')
16
17consumer = CustomConsumer()
18consumer.run()