Created
February 17, 2018 17:08
-
-
Save chemax/40ed8de4892e33e1f1e81b172d020960 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let config = require('./config'); | |
let ami = new require('asterisk-manager')( | |
config.get('ami:port'), | |
config.get('ami:host'), | |
config.get('ami:username'), | |
config.get('ami:password'), | |
true); | |
let AGIServer = require('ding-dong'); | |
let fn = require('./fn'); | |
ami.on('managerevent', evt => { | |
// if (!['DialBegin', 'DialEnd', 'DialState'].includes(evt.event)) return; | |
if (['RTCPReceived', 'RTCPSent'].includes(evt.event)) return; | |
console.log(evt.event); | |
// if(['DialBegin'].includes(evt.event)) | |
// { | |
// // this.currentCall.destchannel = evt.destchannel; | |
// console.log(evt); | |
// this.currentCall.destchannel = evt.destchannel; | |
// } | |
}) | |
module.exports = class Dialler { | |
constructor() { | |
let agi = new AGIServer(this.handler); | |
agi.start(3000); | |
ami.keepConnected(); | |
this.currentCall = {destchannel: ""}; | |
ami.on('dialbegin', evt => { | |
this.currentCall.destchannel = evt.destchannel; | |
}) | |
this.decide = vars => { | |
if ((vars.agi_callerid === this.currentCall.a) && (vars.agi_dnid === this.currentCall.b)) { | |
return true; | |
} | |
return false; | |
} | |
} | |
setParams(callArr) { | |
this.a = callArr.a || false; | |
this.b = callArr.b || false; | |
this.soundA = callArr.soundA || config.get('default:soundA'); | |
this.soundB = callArr.soundB || config.get('default:soundB'); | |
} | |
startOriginate() { | |
ami.action(this.getOriginateParams(), function (err, res) { | |
if (!err) this.currentCall = ""; | |
// console.log(res); | |
}); | |
} | |
getOriginateParams(dataset) { | |
// console.log(dataset); | |
this.currentCall = {}; | |
let b = this.cutRandomElement(this.b); | |
let a = this.cutRandomElement(this.a); | |
this.currentCall.a = a[0]; | |
this.currentCall.b = b[0]; | |
console.log('currentCall ', this.currentCall); | |
console.log(dataset); | |
let originate = { | |
'action': 'originate', | |
'channel': 'SIP/192.168.113.45/' + b, | |
'context': 'default', | |
'exten': 1234, | |
'priority': 1, | |
'CallerID': a, | |
'Application': 'Playback', | |
'Data': 'demo-echotest', | |
'variable': { | |
'name1': 'value1', | |
'name2': 'value2' | |
} | |
} | |
return originate | |
} | |
cutRandomElement(items) { | |
let index = Math.floor(Math.random() * items.length); | |
return items.splice(index, 1); | |
} | |
handler(context) { | |
console.log('hello, AGI'); | |
context.onEvent('variables') | |
.then(function (vars) { | |
// console.log('agi vars', vars); | |
console.log('fucking test'); | |
if (decide) { | |
console.log('this is parta'); | |
} | |
return context.answer() | |
}) | |
.then(function (vars) { | |
return context.playback('demo-echotest') | |
}) | |
.then(function (result) { | |
return context.end(); | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment