Telephone system:Space state

From Hackerspace ACKspace
Revision as of 14:20, 20 May 2022 by Xopr (talk | contribs) (added a lua version (since mod_spidermonkey and mod_v8 are gone on the current installation))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

lua version


--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 = ""

    local body, code, headers, status = https.request(path)
    local data = json.decode(body)
    return and "open" or "closed"


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


// Checks the space state API: if "open" is not 'true',
// it's assumed closed and plays a sound file

if (session.ready())

    var state = fetchUrl( "" );
    if (state == false)
        console_log( 3, "could not fetch space state");
        // 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:



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)$">
            <action function="play-file" data="ivr/welkom.wav"/>
            <action function="sleep" data="300"/>
            <action function="play-file" data="phrase:ackspace_welcome_short"/>
            <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"/>