3
« on: March 30, 2018, 06:11:13 AM »
ok, so I have a JS script now that listens for engine.timer and creates a call everytime engine.timer fires (basically every second)
function onTimer(msg)
{
// Get DB Object
var m = new Message("database");
// Specify connection to use
m.account = "ivrcon";
// Define Query
m.query = "SELECT OK, number, call FROM get_next_call();";
// Run the Query
if (m.dispatch())
{
if (m.rows > 0)
{
Engine.debug(Engine.DebugInfo, "CREATING CALL.EXECUTE");
var executeMsg = new Message("call.execute", msg);
executeMsg.callto = "dumb/";
executeMsg.target = "123456";
executeMsg.caller = "test";
executeMsg.callername = "test";
Engine.debug(Engine.DebugInfo, "DISPATCHING CALL.EXECUTE");
if(executeMsg.dispatch())
{
Engine.debug(Engine.DebugInfo, "PEER ID: " + executeMsg.peerid);
Message.install(onCallAnswered,"call.answered",80, "id", executeMsg.peerid);
Message.install(onChanNotify,"chan.notify",80, "id", executeMsg.id);
Message.install(onChanDtmf,"chan.dtmf",80, "id", executeMsg.peerid);
// one of both parties can hangup
Message.install(onChanDisconnected,"chan.disconnected",100, "id", executeMsg.peerid);
Message.install(onChanDisconnected,"chan.disconnected",100, "id", executeMsg.id);
}
}
}
else
{
Engine.output("Query failed");
}
return true;
}
function onCallAnswered(msg)
{
Engine.debug(Engine.DebugInfo, "Chan.Answered ID: " + msg.id);
Engine.debug(Engine.DebugInfo, "Chan.Answered PeerID: " + msg.peerid);
var chanAttachMsg = new Message("chan.masquerade");
chanAttachMsg.message = "chan.attach";
chanAttachMsg.id = msg.peerid;
chanAttachMsg.source = "wave/play//usr/share/yate/sounds/prompt_for_destination.alaw";
chanAttachMsg.notify = msg.id;
chanAttachMsg.dispatch();
wait_for_notify = true;
msg.handled = true;
return true;
}
Engine.debugName("ivr-handler");
Message.trackName("ivr-handler");
Engine.debugEnabled(true);
// Message handler
Message.install(onTimer,"engine.timer",80);
So, I wonder if I need to install the handler with the "id" parameter and the filter for the message IDs. Anyway, like this, many calls will be created from yate in parallel, so there will be lots of chan.dtmf, chan.notify, chan.disconnected etc. messages. This leads me to the following question:
1. When engine.timer fires and the handler will call "onTimer", is this a new script instance every time or will the same script be used?
2. Are the executed calls and their messages always received by the same instance (does each call have it's own instance (like question above)) and can I use "instance-only visible" variables to store states and additional information for processing? Or do I always need to track the "id" parameter in the handlers to know to which call a message belongs? Or is the same instance always receiving the message because of the installed filter on message.id?
Also, as I understand, as this whole script is not called in the normal routing process, functions like Channel.callJust/callTo etc. are not available.
Thank you.