Relay SDK for Python
Getting Started
The Relay SDK for Python enables developers to connect and use SignalWire's Relay APIs within their own Python code. Our Relay SDK allows developers to build or add robust and innovative communication services to their applications.
The Relay SDK for Python is easy to use and only takes a few minute to setup and get running.
Installation
Install the package using either pip
or pipenv
:
Using pip
:
pip install signalwire
Using pipenv
:
pipenv install signalwire
Minimum Requirements
The Python SDK is build on top of asyncio and requires at least Python 3.6 to be run.
We suggest to use Python 3.7 or above.
Using the SDK
The Python SDK can be used to get up and running with Relay quickly and easily. In order to use the SDK, you must get your project and token from your SignalWire dashboard.
There are a few ways to get started, depending on your needs: Relay.Consumer
, Relay.Task
and Relay.Client
.
Relay Consumer
A Relay.Consumer
creates a long running process, allowing you to respond to incoming requests and events in realtime. Relay Consumers abstract all the setup of connecting to Relay and automatically dispatches workers to handle requests; so you can concentrate on writing your code without having to worry about multi-threading or blocking, everything just works. Think of Relay Consumers like a background worker system for all your calling and messaging needs.
Relay Consumers can scale easily, simply by running multiple instances of your Relay.Consumer
process. Each event will only be delivered to a single consumer, so as your volume increases, just scale up! This process works well whether you are using Docker Swarm, a Procfile on Heroku, your own webserver, and most other environments.
Setting up a new consumer is the easiest way to get up and running:
from signalwire.relay.consumer import Consumer
class CustomConsumer(Consumer):
def setup(self):
self.project = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
self.token = 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
self.contexts = ['home', 'office']
async def ready(self):
# Consumer is successfully connected with Relay.
# You can make calls or send messages here..
async def on_incoming_call(self, call):
result = await call.answer()
if result.successful:
print('Call answered..')
# Run your consumer..
consumer = CustomConsumer()
consumer.run()
Learn more about Relay Consumers
Relay Task
A Relay.Task
is simple way to send jobs to your Relay.Consumers
from a short lived process, like a web framework. Relay Tasks allow you to pass commands down to your Consumers without blocking your short lived request. Think of a Relay Task as a way to queue a job for your background workers to processes asynchronously.
For example, if you wanted to make an outbound call and play a message when your user clicks a button on your web application, since Relay is a realtime protocol and relies on you to tell it what to do in realtime, if you did this within your web application, your web server would block until the call was finished... this may take a long time! Instead, simply create a new Relay Task. This task will be handled by a running Relay Consumer process and your web application can respond back to your user immediately.
Send a task in the office
context with custom data:
# create-task.py
from signalwire.relay.task import Task
project = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
token = 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
task = Task(project=project, token=token)
success = task.deliver('office', {
'uuid': 'unique id',
'data': 'data for your job'
})
if success:
print('Task delivered')
else:
print('Error delivering task..')
Handle the task in a Consumer:
# consumer-task.py
from signalwire.relay.consumer import Consumer
class CustomConsumer(Consumer):
def setup(self):
self.project = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
self.token = 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
self.contexts = ['office']
async def on_task(self, message):
print('Handle inbound task')
print(message['uuid']) # => 'unique id'
print(message['data']) # => 'data for your job'
consumer = CustomConsumer()
consumer.run()
Relay Client
Relay.Client
is a lower level object, giving you a basic connection to Relay but that is all. It is best used when you are creating a script only concerned with sending outbound requests or you want complete control over the Relay connection yourself.
Setting up a new client and make an outbound call:
from signalwire.relay.client import Client
async def ready(client):
call = client.calling.new_call(from_number='+1XXXXXXXXXX', to_number='+1YYYYYYYYYY')
result = await call.dial()
if result.successful:
print('Your call has been answered..')
project = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
token = 'PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
client = Client(project=project, token=token)
client.on('ready', ready)
client.connect()
Learn more about Relay Clients
Contexts
Relay uses Contexts as a simple way to separate events to specific consumers, allowing you to write consumers for specific types of calls or messages or scale them independently. A Context is simply a named string, that allows you to categorize requests. When creating outbound requests, or configuring phone numbers for inbound requests, you can specify the context; Relay will then deliver that call or event to Consumers that are configured to listen for that context.
For example, you could have a customer support phone number configured to send to Relay with the support
context, and a personal number configured with personal
context. Relay would deliver these events to any Consumer listening for those contexts. This gives you a lot of control in how messages are delivered to your Consumers, allowing you to write Consumer classes specific to the context, scale them independently, or separate traffic based on your own business rules.