Telephone system:Space state
This script sets the spacestate variable according to what the webservice API returns. It then can be used in the dialplan to, for example, play a sound file or do call forwarding. Currently it is used to build a sound phrase for playing it as a greeting within our IVR
Contents
lua version
spacestate.lua
--in debian, do sudo apt install lua-sec lua-dkjson local json = require 'dkjson' local https = require "ssl.https" https.TIMEOUT = 2 function sendRequest() local path = "https://ackspace.nl/spaceAPI/" local body, code, headers, status = https.request(path) local data = json.decode(body) return data.state.open and "open" or "closed" end session:setVariable("spacestate", sendRequest() )
dialplan and play_and_get_digits
Note: on the DID trunk, redirect
is replaced with open
This code allows for user extensions (10x, 15x, 17x and 19x) as well as the service extensions (12xx and 13xx)
<action application="lua" data="spacestate.lua"/> <action application="set" data="welcomefile=${cond(${spacestate} == open ? redirect : gesloten )}"/> <action application="playback" data="../../../nl/nl/xander/ivr/welkom.wav"/> <action application="play_and_get_digits" data="3 4 3 7000 # ../../../nl/nl/xander/ivr/${welcomefile}.wav ../../../nl/nl/xander/ivr/onjuist.wav digits ^(1[0579]\d|1[23]\d\d|9\d\d)$ 2000 sip_failed XML ackspace"/> <action application="transfer" data="${digits} XML public"/>
javascript version
spacestate.js
// Checks the space state API: if "open" is not 'true', // it's assumed closed and plays a sound file if (session.ready()) { session.preAnswer(); var state = fetchUrl( "https://ackspace.nl/spaceAPI/" ); if (state == false) console_log( 3, "could not fetch space state"); else { // Remove newlines state = state.replace( /[\r\n]/g,""); if( !state.match( /.*"open"[ \t]*:[ \t]*true.*/i ) ) { console_log(4, "Space seems to be closed, or could not parse result.\n"); session.setVariable("spacestate", "closed"); } else { console_log(4, "Space is open!\n"); session.setVariable("spacestate", "open"); } } }
dialplan and ivr
The javascript is executed just before executing the IVR
<action application="javascript" data="spacestate.js"/> <action application="ivr" data="ackspace_ivr"/>
and the corresponding IVR setting is:
greet-long="phrase:ackspace_welcome:${spacestate}"
phrase
This snippet is put under lang/nl/IVR/ackspace.xml and checks the given variable whether it is set to 'open'
<macro name="ackspace_welcome"> <input pattern="^(open)$"> <match> <action function="play-file" data="ivr/welkom.wav"/> <action function="sleep" data="300"/> <action function="play-file" data="phrase:ackspace_welcome_short"/> </match> <nomatch> <action function="play-file" data="ivr/welkom.wav"/> <action function="sleep" data="300"/> <action function="play-file" data="ivr/gesloten.wav"/> <action function="sleep" data="2000"/> <action function="play-file" data="phrase:ackspace_welcome_short"/> </nomatch> </input> </macro>