How to use Wazo programmable application

Hello,

People ask on Mattermost how to use programmable application with Wazo. So first, the concept for using application on Wazo is very simple. The goal for developpers is to receiving a call from a sip trunk, from a sip phone or webrtc phone and realize a program about what you want. Example IVR, routing etc …
By default an application do nothing, except to receive a message to indicate when a call is entered in the application.

So how to use it? First step you need to create your application in your tenant:

When your application is created you have the choice to link it to an incall:

The event you will received will be : application_call_entered
To alert your application, you have the choice to use a webhook or to use websocket. The payload is the same.

From the user perspective:

The event will be : application_user_outgoing_call_created

When you have finished to configure this simple routing, you are now done. The next step is to take your preferred editor and make code.

Show me the code:

#!/usr/bin/python3

from wazo_websocketd_client import Client as Websocket
from wazo_auth_client import Client as Auth
from wazo_calld_client import Client as Calld

username = ''
password = ''
wazo_domain = ''
client_id = 'my-application'

def notify(handler, msg):
    print("{}: {}".format(handler, msg))
    # call calld API or what you want

def application_call_entered(handler):
    message = "Incoming call is entered"
    notify(handler, message)

def application_user_outgoing_call_created(handler):
    message = "Outgoing call from user has been created"
    notify(handler, message)

def get_refresh_token():
    token_data = auth.token.new('wazo_user', access_type='offline', client_id=client_id)
    refresh_token = token_data['refresh_token']
    return token_data['refresh_token']

def get_token():
    token_data = auth.token.new('wazo_user', expiration=3600, refresh_token=refresh_token, client_id=client_id)
    return token_data['token']

def session_expired(data):
    token_data = auth.token.new('wazo_user', expiration=3600, refresh_token=refresh_token, client_id=client_id)
    token = token_data['token']
    ws.update_token(token)
    calld.update_token(token)

auth = Auth(wazo_domain, username=username, password=password, verify_certificate=False)
refresh_token = get_refresh_token()
ws = Websocket(wazo_domain, token=get_token(), verify_certificate=False, debug=False)
calld = Calld(wazo_domain, token=get_token(), verify_certificate=False)

ws.on('application_call_entered', application_call_entered)
ws.on('application_user_outgoing_call_created', application_user_outgoing_call_created)
ws.on('auth_session_expire_soon', session_expired)

ws.run()

If you want, you have the possibility to use node-red with the wazo-platform plugin. This example show you an incoming call, you answered it, launch moh and after 3 seconds you hangup the call.

Hope this mini tutorial help you to play with the application and Wazo.
Sylvain

3 Likes

Thank you so much @quintana ! Now it’s 100% crystal clear! :+1:

1 Like