65
Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon Chris Howard, Digium

Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Embed Size (px)

Citation preview

Page 1: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Creating Communication Applications using the Asterisk RESTful Interface

API Strategy & Practice 2017, Portland Oregon Chris Howard, Digium

Page 2: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Who am I?

ChrisHoward•  DigiumTrainingManager•  #2373•  [email protected]•  @cch_123•  https://www.linkedin.com/in/christopher-c-howard

Page 3: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Background

Digium–DeveloperProductsTrainingmanager.AsteriaSolutionsGroup–DirectorofSoftwareDevelopmentNortel/Architel/MetaSolv–SeniorConsultantTeledyneBrown/EERSystems–Programmer(Adayay!)

Page 4: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Introduction to Asterisk

Page 5: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Introduction to Asterisk

•  What is Asterisk? •  What can Asterisk do? •  Who is Digium?

Page 6: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

What is Asterisk?

Open Source Communications Platform •  Asterisk is

–  software that turns an ordinary computer into a communications server

Page 7: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Asterisk Use Cases

•  Traditional PBX

•  VoIP only PBX

•  Hybrid PBX

•  VoIP Gateway

•  Feature Server

•  Call Center

•  Carrier Platform

•  WebRTC Server

Page 8: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Asterisk History

2014

– A

ster

isk

13

2008

– A

ster

isk

1.6.

0 20

09 –

Ast

eris

k 1.

6.1

2009

– A

ster

isk

1.6.

2

2010

– A

ster

isk

1.8

2011

– A

ster

isk

10

1999

– A

ster

isk

0.1

2004

– A

ster

isk

1.0

2005

– A

ster

isk

1.2

2006

– A

ster

isk

1.4

Vers

ion

Time

2012

– A

ster

isk

11

2013

– A

ster

isk

12

2016

– A

ster

isk

14

2017

– A

ster

isk

15

Page 9: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Asterisk Characteristics •  Very mature code base for a teenager

–  Over 17 years old –  Enterprise-class reliability and performance

•  Global Community –  Over 1 Million production servers

•  Dual License –  Open source - GPLv2 –  Commercial OEM

•  Supported –  Community support –  Commercial open source support available

Page 10: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Obligatory first application

Open Source Communications Platform

WebServer CommunicationsServer

<html> <head>Hello</head> <body> <h1>Hello World!</h1> </body> </html>

exten => 100,1,Answer() same => n,Wait(1) same => n,Playback(hello-world) same => n,Hangup()

Page 11: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Who is Digium?

•  “The Asterisk Company” •  Mark Spencer •  2 Fold Mission

–  Open Source –  Commercial

•  200+ employees •  Headquartered in Huntsville, AL •  Atlanta, San Diego

Page 12: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Business Lines

•  Digium has 2 primary business lines –  Business Communications Systems –  Custom Communications Solutions

•  And a wholly owned subsidiary, Digium Cloud Services

12

Page 13: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Business Products

SwitchvoxHardwarePBX

SwitchvoxMobileApps

SwitchvoxCloud

SIPTrunking

SwithvoxSwitchboard

Page 14: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Developer Products

CoursesAsteriskEssentials(online)AsteriskFastStartAsteriskAdvancedAsteriskAPIsforDevelopersCertificationsdCAAdCAP

TrainingSupport

SubscriptionsBusinessHourSupport24/7oncallwarrantysupport

https://www.respoke.io/

Hardware Platforms

Page 15: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Asterisk APIs History

Page 16: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Asterisk RESTFul Interface (ARI) Overview

WhenAsteriskwasfirstcreatedbackin1999,itsdesignwasfocusedonbeingastand-alonePrivateBrancheXchange(PBX)thatyoucouldconfigureviastaticconfigurationfiles.Whilethislevelofconfigurationissufficientformanyapplications,forsomedomains,itisfarmorepreferabletomanipulateAsteriskviaanexternalsystem.So,notlongintotheproject,twoAPIswereaddedtoAsterisk:theAsteriskGatewayInterface(AGI)andtheAsteriskManagerInterface(AMI).

AsteriskAPIHistory

Page 17: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Overview

TheAGIandAMIinterfacesarepowerfulandopenedupawiderangeofintegrationpossibilitiesfordevelopingAsteriskbasedappsandintegratingAsteriskwithothersystems.

•  UsingAGI,remotedialplanexecutioncouldbeenabledwhichalloweddeveloperstocontrolchannelsinAsteriskusingPHP,Python,Java,andotherlanguages.

•  WithAMI,thestateofAsteriskcouldbedisplayed,callsinitiated,andthelocationofchannelscontrolled.UsingbothAPIstogether,complexapplicationsusingAsteriskastheenginecouldbedeveloped.

AsteriskAPIHistory(continued)

Page 18: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

The AGI Dialplan App

YouwillneedtocreateanextensionthatcallstheAGIdialplanapplication.

[internal] exten => 301,1,NoOp(Hello World) same => n,Answer() same => n,AGI(hello-world.pl) same => n,Hangup()

TheAGIapplicationhandsoffcallcontroltotheprogramspecifiedintheargument.(defaultlocation/var/lib/asterisk/agi-bin/)

Page 19: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

AGI Execution Flow

AsteriskAsterisk DialplanDialplan AGI

Channel

Start

Noop

Answer

AGI– hello_world.pl

StreamFilehello_world

AGITerminates

Hangup

Streamhello_world

Hangup

Page 20: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

AGI Demo

•  Showhello-world.plscript.

•  TurnonAGIDebug.

•  Placecalltohello-worldAGIextension.

Page 21: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Asterisk Manager Interface

[root@localhost ~]# nc 127.0.0.1 5038 Asterisk Call Manager/3.2.0 Action: Login Username: test Secret: mysecret Response: Success Message: Authentication accepted

Page 22: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Asterisk Manager Interface – Originate Action

Action: Originate Channel: PJSIP/7001 Context: astricon Exten: 7002 Priority: 1 Response: Success Message: Originate successfully queued

Page 23: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Asterisk Manager Interface – Events

Event: DeviceStateChange Privilege: call,all Device: PJSIP/7001 State: INUSE

Page 24: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

AMI Demo

•  LogintoAMI

•  IssueOriginateCommand

•  Displayeventsfiredfromoriginate

Page 25: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

AGI/AMI Libraries and Frameworks

Name Language Website APIsAdhearsion Ruby http://www.adhearsion.com/ AMI/FastAGI

Asterisk-Java Java https://asterisk-java.org/ AMI/FastAGI

PAGI PHP http://marcelog.github.io/PAGI/ AGI

PAMI PHP http://marcelog.github.io/PAMI/ AMI

PHPAGI PHP http://phpagi.sourceforge.net/ AGI/FastAGI

Panoramisk Python https://github.com/gawel/panoramisk AMI/FastAGI

Pyst2 Python https://github.com/rdegges/pyst2 AMI/AGI

StarPy Python https://github.com/asterisk/starpy AMI/FastAGI

Nanoagi C++ https://sourceforge.net/projects/nanoagi/ AGI

AsteriNET .NET(C#/VB) https://github.com/AsterNET/AsterNET AMI/FastAGI

Ding-Dong Node.js https://www.npmjs.com/package/ding-dong FastAGI

AGI/AMILibrariesandFrameworks

Page 26: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Asterisk RESTful Interface (ARI) Overview

Page 27: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Overview

WhileAMIisgoodatcallcontrolandeventmonitoringandAGIisgoodatallowingaremoteprocesstoexecutedialplanapplications,neitheroftheseAPIsweredesignedtoletadeveloperbuildtheirowncustomcommunicationsapplication.

AsteriskAPIHistory–ARIRational

Page 28: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Overview

NeitherAPIexposesthekindsofcommunicationsprimitivesthatexistinAsteriskneededtoeasilybuildsuchanapplication.Thatwasn’ttheirintendedpurpose.

So,theAsteriskDeveloperCommunitysetouttobuildabetterAPIthatwouldmakeiteasiertobuildcustomcommunicationsapplications.ARIwastheresultofthiseffort.

AsteriskAPIHistory–ARIRational(continued)

Page 29: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

The Asterisk RESTfull Interface (ARI)

Page 30: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Overview

•  ARIisaRESTinterfaceallowsapplicationdeveloperstobuildrich,customcommunicationsapplicationsinthelanguageoftheirchoice.

•  ARIexposestherawprimitiveobjectsinAsterisknormallyreservedforCmodules—channels,bridges,endpoints,media,etc.—throughanintuitiveRESTinterface.

•  Asanasynchronousinterface,ARIconveysthestateoftheobjectsbeingcontrolledbytheuserviaJSONeventsoveraWebSocket.

WhatistheAsteriskRESTfulInterface(ARI)

Page 31: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Overview

•  Thewebsocketinterfacethatlistensforevents.

•  TheRESTinterfacewhereyoucansendcommandsandreceiveresponses.

ARIconsistof2“parts”.Thisisanimportantconcepttoremember.Thereare2distinctinterfacesthatmakeupARI.

Page 32: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

AGI vs. AMI vs. ARI

ARIisnotareplacementforAMIorAGI.

•  AGIallowsyoutocontroldialplanexecutionofachannel.•  AMIallowsyoutomanagecallsatahighlevel.

•  ARIallowsyoutoreplacedialplanapplicationswithyourowncustomcommunicationsapplication.

ARIisnotabouttellingachanneltoexecutetheVoiceMaildialplanapplicationorredirectingachannelinthedialplantoaVoiceMailextension.

ItisaboutlettingyoubuildyourownVoiceMailapplication.

Page 33: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Quick Start Guide

1.EnabletheAsteriskHTTPserviceinhttp.conf:[general]enabled = yesbindaddr = 0.0.0.0

2.ConfigureanARIuserinari.conf:

[general]enabled = yes pretty = yes [asterisk]type = userread_only = nopassword = asterisk

Page 34: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

The Stasis Dialplan App

YouwillneedtocreateanextensionthatcallstheStasisdialplanapplication.

[internal] exten => 403,1,NoOp() same => n,Answer() same => n,Stasis(hello-world) same => n,Hangup()

StasisisthemechanismthatAsteriskusestohandcontrolofachanneloverfromthedialplan-whichisthetraditionalwayinwhichchannelsarecontrolled-toARIandtheclient.

Page 35: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Websock Connection

Nowwewillneedtosetupawebsocketconnectiontoasteriskindicatingthatwearethehello-worldapplication.wscat -c "ws://192.168.1.100:8088/ari/events?api_key=admin:digium&app=hello-world"

Wecanusethewscatapplicationtoconnecttoasterisk,identifyourselfandsaywearetheapp-helloworld.

InthecaseofARI,aWebSocketconnectionisusedtopassasynchronouseventsfromAsterisktotheclient.

TheseeventsarerelatedtotheRESTfulinterface,butaretechnicallyindependentofit.TheyallowAsterisktoinformtheclientofchangesinresourcestatethatmayoccurbecauseofandinconjunctionwiththechangesmadebytheclientthroughARI.

Page 36: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

The ARI Websock Session

WhenyoucalltheextensionoftheStasisapplicationyouwillseeaStasisStartmessageinyourwscatsection.Notethe“id:”attribute.Wewillusethattoissuecommandstothecall.

{ "type": "StasisStart", "timestamp": "2017-04-05T11:04:14.869-0700", "args": [], "channel": { "id": "1491415454.6", "name": "PJSIP/302-00000003", "state": "Up", "caller": { "name": "302", "number": "302" }, "connected": { "name": "", "number": "" },x "accountcode": "", "dialplan": { "context": "internal", "exten": "401", "priority": 3 }, "creationtime": "2017-04-05T11:04:14.714-0700", "language": "en" }, "application": "hello-world"}

Page 37: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Request/Response NowthatthechannelisintheStasisapplicationwecansendRESTrequesttoit.Thefollowingisanexampleusingcurl.curl -v -u apitest:mysecret -X POST "http://192.168.1.100:8088/ari/channels/1491415454.6/play?media=sound:hello-world"

NotethatthechannelidneedstomatchtheideintheStrasisStartmessage.Theresponsewillbesimilartothefollowing:{ "id": "adb889f4-6dba-404c-b8f1-743b32a2c0fc", "media_uri": "sound:hello-world", "target_uri": "channel:1491415454.6", "language": "en", "state": "queued"* Connection #0 to host 192.168.101.50 left intact}

Page 38: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

The ARI Websock Session After Request

ThewebsocketsessionwillshoweventthatweregeneratedbasedontheARIrequest.

< { "type": "PlaybackStarted", "playback": { "target_uri": "channel:1491415454.6", "media_uri": "sound:hello-world", "id": "6455cfc1-3714-4584-90b2-3133f8cd1d4f", "state": "playing", "language": "en" }, "application": "hello-world"}< { "type": "PlaybackFinished", "playback": { "target_uri": "channel:1491415454.6", "media_uri": "sound:hello-world", "id": "6455cfc1-3714-4584-90b2-3133f8cd1d4f", "state": "done", "language": "en" }, "application": "hello-world"}

Page 39: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

AMI Demo

•  CreateStasisExtension•  Usewscattomonitorevents•  Sendhello-worldsoundtothechannelfromtheevent

ARIDemo

Page 40: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI REST Resource Categories

•  Asterisk•  endpoints•  channels•  bridges•  recordings•  sounds

•  playbacks•  deviceStates•  Mailboxes•  events•  applications

Page 41: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Swagger Specification

The REST API that makes up ARI is documented using Swagger, a lightweight specification for documenting RESTful API's. The swagger specifications are located in the rest-api/api-docs directory of the Asterisk distribution.

Page 42: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Swagger Specification

Page 43: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Applications

•  Thewebsocketinterfacethatlistensforevents.•  TheRESTinterfacewhereyoucansendcommandsand

receiveresponses.

ThepreviousdemoshowedthearchitectureoftheAsteriskRESTfullinterfaceandanexampleofusingthe“2channel”approach.

Youcanusethese2channelstocreateanAsteriskApplicationhoweveritisratherdifficultkeepingupwiththewebsocketeventsandcontrollinganactivesystem.

Page 44: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Applications

TheAnswer… ARIFrameworks!

OneofthebeautifulthingsaboutARIisthatit'ssoeasytojustbangoutarequest.Howeveryoudon’twanttolotsofdirectHTTPcallsthroughoutyourapplicationandhowdoyoukeepupwithalloftheeventsthatyouarerespondingto…

Page 45: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Frameworks

ARIFrameworksareanabstractionlayerfortherawARIcalls.TheabstractionlayersallowforlanguagenativefunctionsandeventhandlersthathandlealloftheunderlyingARIeventsandActions.MostframeworkswerebuiltfromtheARIswaggerspec.

Page 46: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Available ARI Frameworks

Library Language Resourceari-py Python https://github.com/asterisk/ari-py AsterNET.ARI C# / .NET https://github.com/skrusty/AsterNET.ARI ari4java Java https://github.com/l3nz/ari4java node-ari-client NodeJS https://github.com/asterisk/node-ari-client phpari PHP http://www.phpari.org/ Phparia PHP http://phparia.org/ asterisk-ari-client Ruby https://github.com/svoboda-jan/asterisk-ari

Page 47: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Frameworks

Note:Forthissessionwearegoingtofocusonusingthenode-ari-clientframework.Let’srevisitthehello-worldsamplethatwebuiltusingwscatandcurlbutnowusingthenode-ari-clientframework.

Page 48: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Frameworks – hello-world.js

Page 49: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

AMI Demo

•  Showthehello-world.jsapplication•  StarttheNodeApp.•  Dial403tohearthesound.

ARIFrameworkDemo

Page 50: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Application

Let’slookatcreatingacommunicationapplicationlikeaconferencebridge.Nowforafewquickrequirements:•  Unlimitedparticipants•  Press1mutesself•  Press2unmutesself•  Press3releasesamonkeytothebridge•  Press4removesallmonkeysfrombridge

Page 51: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Application – Create/Add Channel to Bridge Thefirstthingweneedtodowhenawestartthe“ariconf”ARIapplicationistocreateabridge.var client = require('ari-client'); client.connect( 'http://192.168.1.100:8088', 'admin', 'digium', function(err, ari) {

var bridge = ari.Bridge(); bridge.create({ type: 'mixing,dtmf_events', name: 'ariconf’ }); ari.start('ariconf'); }

)

Page 52: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Application – Cleanup Bridges Note:Bridgesdon’tdiewhenARIscriptdies.function cleanupBridges() {

ari.bridges.list( function(err, bridges) { var ariconfBridges = bridges.filter( function(candidate) { return candidate['name'] === 'ariconf'; } ); ariconfBridges.forEach( function(bridge) { bridge.destroy(function(err) {}); }); } );

}

Page 53: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Application – Add New Channels to the Bridge

Nowthattheapplicationhasstartedandwehaveabridgecreatedwewillwanttoaddanynewchannelthatentersthe‘ariconf’Stasisapplicationtothebridge.Thiswillbehandledinthe“StasisStart”event.Firstweneedtofindthebridgeandthenweaddthechanneltothebridge.

Page 54: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Application – Add Channel to Bridge

function joinBridge(channel) { ari.bridges.list( function(err, bridges) { var bridge = bridges.filter( function(candidate) { return candidate['name'] === ‘ariconf’; } )[0]; if (!bridge) { console.log("bridge not found!"); } else { bridge.addChannel({channel: channel.id}); } );

}

Page 55: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Application – Add Channel to Bridge

ari.on( 'StasisStart’, function(event, incomingChannel) { incomingChannel.answer (

function(err) { joinBridge(incomingChannel, "ariconf"); } ); }

);

NowcallthejoinBridgefunctionfromthe“StasisStart”event

Page 56: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Application – Handle DTMF

SonowthatwehaveaConferenceBridgeapplicationthatcanhandlemultiplecallswewanttoaddafewfeaturesthataretriggeredbyDTMFkeypresses.Thisisdoneusingthe“ChannelDtmfReceived”events.

Page 57: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Application – DTMF Events

incoming.on( 'ChannelDtmfReceived', function(dtmfEvent, channel) { const digit = dtmfEvent.digit; switch (digit) { case '1': setMute(channel); break; case '2': setUnmute(channel) break; default: console.log('Unknown DTMF digit: %s', digit); } }

);

Page 58: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Application – Create/Add Channel to Bridge Thecodeforthemute/unmuteisprettysimple.

function setMute(channel) { ari.channels.mute({ channelId: channel.id, direction: 'in’ });

} function setUnmute(channel) {

ari.channels.unmute({ channelId: channel.id, direction: 'in’ });

}

Page 59: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Application – Monkey Time

NowthatwehavethebasicDTMFhandlersinplacewecanaddmorefunctionality.Whenthecallerpresses3wewilladdascreamingmonkeytothebridge.Wewillusetheoriginatecommandtoimplementthis.Firstoriginateacalltothebridgeandwhenthebridgeanswerswewillconnecttheothersidetosomedialplanthatplaysthett-monkeyssoundfile.

Page 60: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Application – Monkeys Dialplan

Thedialplanforthett-monkeysplayback

[monkeys] exten => monkeys,1,Noop(playback tt-monkeys) same => n,Playback(tt-monkeys) same => n,Hangup()

Page 61: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Application – Add Monkey to Bridge

function originateMonkey(channel) { ari.channels.originate({ endpoint: 'Local/' + channel.dialplan.exten + '@astricon', context: 'monkeys', extension: 'monkeys', priority: "1", variables: { "__monkey": "true” } });

}

WeoriginatetotheconferencebridgeusingaLocalchannel.Oncethebridgeanswerswesendittothemionkeysdialplantopaybackthett-monkeyssound.

Note:wesetachannelvariablecalledmonkey

Page 62: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Application – Kick the Monkey

Nowsincewehaveawaytoaddmonkeystotheconferencebridgeitsurewouldbenicetobeabletokickthemout.Wecandothisbylookingatallofthechannelsinthebridgeandifanyofthemhavethemonkeychannelvariablethenwecancallhangup()onthechanneltoremovethem.

Page 63: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Application – Add Monkey to Bridge

function originateMonkey(channel) { ari.channels.originate({ endpoint: 'Local/' + channel.dialplan.exten + '@astricon', context: 'monkeys', extension: 'monkeys', priority: "1", variables: { "__monkey": "true” } });

}

WeoriginatetotheconferencebridgeusingaLocalchannel.Oncethebridgeanswerswesendittothemionkeysdialplantopaybackthett-monkeyssound.

Note:wesetachannelvariablecalledmonkey

Page 64: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

ARI Application – No More Monkeys On My Bridge

bridge.channels.forEach( function(channel) { ari.channels.get({ channelId: channel }, function(err, bridgechannel) { bridgechannel.getChannelVar({ variable: 'monkey’ }).then( function(monkey) { bridgechannel.hangup(); } ).catch(); }

);

Page 65: Creating Communication Applications using the Asterisk ... · Creating Communication Applications using the Asterisk RESTful Interface API Strategy & Practice 2017, Portland Oregon

Asterisk APIs Training

Note:ThereisanAsteriskAPIsforDevelopersclassscheduledforNov-14toNov-16attheDigiumHQinHuntsville,AL.https://www.digium.com/training/asteriskQuestions?