如何在 Stasis 应用程序中获取拨打的号码
How to get the dialed number in Stasis app
我正在努力研究 ARI 和 Asterisk,我的目标是从一个分机拨号到另一个分机。我从分机 5002 拨了 5001。现在在 stasisStart 函数中,我想创建一个新频道,并使用拨打的号码 (5001) 并将 'PJSIP/5001' 传递给端点。如何获取拨打的号码?
拨号方案:
exten => _500Z, 1, Stasis(test-app)
测试-app.js
function stasisStart(event, channel) {
// I want to dial 'PJSIP/5001' (the dialed number)
client.channels.originate({ endpoint: 'PJSIP/5001', app: 'test-app' },)
.then(function (originatedChannel) {
console.log(' originated ');
console.log(util.format('originated channel id %s - name: %s', originatedChannel.id, originatedChannel.name));
})
.catch(function (err) { console.log('error happened'); });
}
Channel
id: "1603812843.140"
name: "PJSIP/5002-0000004e"
state: "Ring"
caller: {"name":"","number":"5002"}
connected: {"name":"","number":""}
accountcode: ""
dialplan: {"context":"internal","exten":"5001","priority":1,"app_name":"Stasis","app_data":"test-app"}
creationtime: "2020-10-27T11:34:03.251-0400"
language: "en"
我发现此示例有效。我想我对端点感到困惑,我以为我必须在拨号方案中指定它,但不是。
/**
* This example shows how a call can be originated from a channel entering a
* Stasis application to an endpoint. The endpoint channel will then enter the
* Stasis application and the 2 channels will be placed into a mixing bridge.
* exten => _500Z,1,NoOp()
* same => n,Stasis(originate-example)
* same => n,Hangup()
*/
'use strict';
var client = require('ari-client');
var ENDPOINT = 'PJSIP/5001'; // TODO: enter your endpoint here
// TODO: replace with your Asterisk instance: host, user and secret
client.connect('http://localhost:8088', 'asterisk', 'asterisk',
/**
* Setup event listeners and start application.
*
* @callback connectCallback
* @memberof originate-example
* @param {Error} err - error object if any, null otherwise
* @param {module:ari-client~Client} ari - ARI client
*/
function (err, ari) {
// Use once to start the application to ensure this listener will only run
// for the incoming channel
ari.once('StasisStart',
/**
* Once the incoming channel has entered Stasis, answer it and originate
* call to the endpoint (outgoing channel).
*
* @callback incomingStasisStartCallback
* @memberof originate-example
* @param {Object} event - the full event object
* @param {module:resources~Channel} incoming -
* the incoming channel entering Stasis
*/
function (event, incoming) {
incoming.answer(function (err) {
originate(incoming);
});
});
/**
* Originate the outgoing channel
*
* @function originate
* @memberof originate-example
* @param {module:resources~Channel} incoming - the incoming channel that
* will originate the call to the endpoint
*/
function originate (incoming) {
incoming.once('StasisEnd',
/**
* If the incoming channel ends, hangup the outgoing channel.
*
* @callback incomingStasisEndCallback
* @memberof originate-example
* @param {Object} event - the full event object
* @param {module:resources~Channel} channel -
* the incoming channel leaving Stasis
*/
function (event, channel) {
outgoing.hangup(function (err) {});
});
var outgoing = ari.Channel();
outgoing.once('ChannelDestroyed',
/**
* If the endpoint rejects the call, hangup the incoming channel.
*
* @callback outgoingChannelDestroyedCallback
* @memberof originate-example
* @param {Object} event - the full event object
* @param {module:resources~Channel} channel -
* the channel that was destroyed
*/
function (event, channel) {
incoming.hangup(function (err) {});
});
outgoing.once('StasisStart',
/**
* When the outgoing channel enters Stasis, create a mixing bridge
* and join the channels together.
*
* @callback outgoingStasisStartCallback
* @memberof originate-example
* @param {Object} event - the full event object
* @param {module:resources~Channel} outgoing -
* the outgoing channel entering Stasis
*/
function (event, outgoing) {
var bridge = ari.Bridge();
outgoing.once('StasisEnd',
/**
* If the outgoing channel ends, clean up the bridge.
*
* @callback outgoingStasisEndCallback
* @memberof originate-example
* @param {Object} event - the full event object
* @param {module:resources~Channel} channel -
* the outgoing channel leaving Stasis
*/
function (event, channel) {
bridge.destroy(function (err) {});
});
outgoing.answer(
/**
* Once the outgoing channel has been answered, create a mixing
* bridge and add the incoming and outgoing channels to it.
*
* @callback outgoingAnswerCallback
* @memberof originate-example
* @param {Error} err - error object if any, null otherwise
*/
function (err) {
bridge.create({type: 'mixing'},
/**
* Once the mixing bridge has been created, join the incoming and
* outgoing channels to it.
*
* @callback bridgeCreateCallback
* @memberof originate-example
* @param {Error} err - error object if any, null otherwise
* @param {module:resources~Bridge} bridge - the newly created
* mixing bridge
*/
function (err, bridge) {
bridge.addChannel(
{channel: [incoming.id, outgoing.id]},
function (err) {}
);
});
});
});
var playback = ari.Playback();
incoming.play({media: 'sound:vm-dialout'}, playback, function (err) {});
// Originate call from incoming channel to endpoint
outgoing.originate(
{endpoint: ENDPOINT, app: 'originate-example', appArgs: 'dialed'},
function (err, channel) {}
);
}
// can also use ari.start(['app-name'...]) to start multiple applications
ari.start('originate-example');
});
我正在努力研究 ARI 和 Asterisk,我的目标是从一个分机拨号到另一个分机。我从分机 5002 拨了 5001。现在在 stasisStart 函数中,我想创建一个新频道,并使用拨打的号码 (5001) 并将 'PJSIP/5001' 传递给端点。如何获取拨打的号码?
拨号方案:
exten => _500Z, 1, Stasis(test-app)
测试-app.js
function stasisStart(event, channel) {
// I want to dial 'PJSIP/5001' (the dialed number)
client.channels.originate({ endpoint: 'PJSIP/5001', app: 'test-app' },)
.then(function (originatedChannel) {
console.log(' originated ');
console.log(util.format('originated channel id %s - name: %s', originatedChannel.id, originatedChannel.name));
})
.catch(function (err) { console.log('error happened'); });
}
Channel
id: "1603812843.140"
name: "PJSIP/5002-0000004e"
state: "Ring"
caller: {"name":"","number":"5002"}
connected: {"name":"","number":""}
accountcode: ""
dialplan: {"context":"internal","exten":"5001","priority":1,"app_name":"Stasis","app_data":"test-app"}
creationtime: "2020-10-27T11:34:03.251-0400"
language: "en"
我发现此示例有效。我想我对端点感到困惑,我以为我必须在拨号方案中指定它,但不是。
/**
* This example shows how a call can be originated from a channel entering a
* Stasis application to an endpoint. The endpoint channel will then enter the
* Stasis application and the 2 channels will be placed into a mixing bridge.
* exten => _500Z,1,NoOp()
* same => n,Stasis(originate-example)
* same => n,Hangup()
*/
'use strict';
var client = require('ari-client');
var ENDPOINT = 'PJSIP/5001'; // TODO: enter your endpoint here
// TODO: replace with your Asterisk instance: host, user and secret
client.connect('http://localhost:8088', 'asterisk', 'asterisk',
/**
* Setup event listeners and start application.
*
* @callback connectCallback
* @memberof originate-example
* @param {Error} err - error object if any, null otherwise
* @param {module:ari-client~Client} ari - ARI client
*/
function (err, ari) {
// Use once to start the application to ensure this listener will only run
// for the incoming channel
ari.once('StasisStart',
/**
* Once the incoming channel has entered Stasis, answer it and originate
* call to the endpoint (outgoing channel).
*
* @callback incomingStasisStartCallback
* @memberof originate-example
* @param {Object} event - the full event object
* @param {module:resources~Channel} incoming -
* the incoming channel entering Stasis
*/
function (event, incoming) {
incoming.answer(function (err) {
originate(incoming);
});
});
/**
* Originate the outgoing channel
*
* @function originate
* @memberof originate-example
* @param {module:resources~Channel} incoming - the incoming channel that
* will originate the call to the endpoint
*/
function originate (incoming) {
incoming.once('StasisEnd',
/**
* If the incoming channel ends, hangup the outgoing channel.
*
* @callback incomingStasisEndCallback
* @memberof originate-example
* @param {Object} event - the full event object
* @param {module:resources~Channel} channel -
* the incoming channel leaving Stasis
*/
function (event, channel) {
outgoing.hangup(function (err) {});
});
var outgoing = ari.Channel();
outgoing.once('ChannelDestroyed',
/**
* If the endpoint rejects the call, hangup the incoming channel.
*
* @callback outgoingChannelDestroyedCallback
* @memberof originate-example
* @param {Object} event - the full event object
* @param {module:resources~Channel} channel -
* the channel that was destroyed
*/
function (event, channel) {
incoming.hangup(function (err) {});
});
outgoing.once('StasisStart',
/**
* When the outgoing channel enters Stasis, create a mixing bridge
* and join the channels together.
*
* @callback outgoingStasisStartCallback
* @memberof originate-example
* @param {Object} event - the full event object
* @param {module:resources~Channel} outgoing -
* the outgoing channel entering Stasis
*/
function (event, outgoing) {
var bridge = ari.Bridge();
outgoing.once('StasisEnd',
/**
* If the outgoing channel ends, clean up the bridge.
*
* @callback outgoingStasisEndCallback
* @memberof originate-example
* @param {Object} event - the full event object
* @param {module:resources~Channel} channel -
* the outgoing channel leaving Stasis
*/
function (event, channel) {
bridge.destroy(function (err) {});
});
outgoing.answer(
/**
* Once the outgoing channel has been answered, create a mixing
* bridge and add the incoming and outgoing channels to it.
*
* @callback outgoingAnswerCallback
* @memberof originate-example
* @param {Error} err - error object if any, null otherwise
*/
function (err) {
bridge.create({type: 'mixing'},
/**
* Once the mixing bridge has been created, join the incoming and
* outgoing channels to it.
*
* @callback bridgeCreateCallback
* @memberof originate-example
* @param {Error} err - error object if any, null otherwise
* @param {module:resources~Bridge} bridge - the newly created
* mixing bridge
*/
function (err, bridge) {
bridge.addChannel(
{channel: [incoming.id, outgoing.id]},
function (err) {}
);
});
});
});
var playback = ari.Playback();
incoming.play({media: 'sound:vm-dialout'}, playback, function (err) {});
// Originate call from incoming channel to endpoint
outgoing.originate(
{endpoint: ENDPOINT, app: 'originate-example', appArgs: 'dialed'},
function (err, channel) {}
);
}
// can also use ari.start(['app-name'...]) to start multiple applications
ari.start('originate-example');
});