Actually I have two use cases
-
When the call is answered then a playback will be played to him, the caller can interrupt on that playback and talk to the receiver after the playback is interrupted (This was achieved through stasis)
-
The second is basically to detect if a human or machine has picked up call if human has picked up the call the above thing will happen and when machine has picked up the call then a voicemail will be left to that number.
the issue is when it moves from stasis to dialplan it hangs up the channel, although when it works without moving out of the stasis application it works perfectly fine
I have done what you said and also removed all the hangup events in ari just to ensure that the channels don’t leave the bridge but still facing the same issue of leaving the bridge these are the logs:
-- Channel SIP/twilio-0000005a left 'simple_bridge' stasis-bridge <aeef1555-6316-4487-ba39-79e06bf0f693>
-- Executing [s@amdcheck:1] AMD("SIP/twilio-0000005a", "") in new stack
-- AMD: SIP/twilio-0000005a 923313355188 (N/A) (Fmt: ulaw)
-- AMD: initialSilence [2500] greeting [1500] afterGreetingSilence [800] totalAnalysisTime [5000] minimumWordLength [120] betweenWordsSilence [50] maximumNumberOfWords [3] silenceThreshold [256] maximumWordLength [5000]
Retransmitting #1 (NAT) to 54.172.60.0:5060:
INVITE sip:172.25.4.248:5060 SIP/2.0
Via: SIP/2.0/UDP 115.186.169.3:5060;branch=z9hG4bK06ec52fb;rport
Route: <sip:54.172.60.0;lr;twnat=sip:115.186.169.3:45204>
Max-Forwards: 70
From: <sip:923313355188@115.186.169.3>;tag=as2ca2fc8e
To: <sip:+19312594692@dialerqlu.pstn.twilio.com>;tag=22482593_c3356d0b_03398364-6662-4e59-a24b-06f392646973
Contact: <sip:923313355188@115.186.169.3:5060>
Call-ID: 6d59143016bb3ed06dd4e7022c9b7a42@115.186.169.3:5060
CSeq: 104 INVITE
User-Agent: Asterisk PBX 16.30.1
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
Remote-Party-ID: "923313355188" <sip:923313355188@115.186.169.3>;party=calling;privacy=off;screen=no
Content-Type: application/sdp
Content-Length: 240
v=0
o=root 842495852 842495854 IN IP4 115.186.169.3
s=Asterisk PBX 16.30.1
c=IN IP4 115.186.169.3
t=0 0
m=audio 11602 RTP/AVP 0 101
a=rtpmap:0 PCMU/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=maxptime:150
a=sendrecv
---
<--- SIP read from UDP:54.172.60.0:5060 --->
SIP/2.0 100 trying -- your call is important to us
Via: SIP/2.0/UDP 115.186.169.3:5060;rport=45204;received=115.186.169.3;branch=z9hG4bK06ec52fb
From: <sip:923313355188@115.186.169.3>;tag=as2ca2fc8e
To: <sip:+19312594692@dialerqlu.pstn.twilio.com>;tag=22482593_c3356d0b_03398364-6662-4e59-a24b-06f392646973
Call-ID: 6d59143016bb3ed06dd4e7022c9b7a42@115.186.169.3:5060
CSeq: 104 INVITE
Server: Twilio Gateway
Content-Length: 0
<------------->
--- (8 headers 0 lines) ---
<--- SIP read from UDP:54.172.60.0:5060 --->
SIP/2.0 200 OK
CSeq: 104 INVITE
Call-ID: 6d59143016bb3ed06dd4e7022c9b7a42@115.186.169.3:5060
From: <sip:923313355188@115.186.169.3>;tag=as2ca2fc8e
To: <sip:+19312594692@dialerqlu.pstn.twilio.com>;tag=22482593_c3356d0b_03398364-6662-4e59-a24b-06f392646973
Via: SIP/2.0/UDP 115.186.169.3:5060;rport=45204;received=115.186.169.3;branch=z9hG4bK06ec52fb
Server: Twilio
Contact: <sip:172.25.4.248:5060>
Record-Route: <sip:54.172.60.0;lr;twnat=sip:115.186.169.3:45204>
X-Twilio-CallSid: CAbbacc39bbfcc97d090849fb7cabcf851
Content-Type: application/sdp
Content-Length: 250
v=0
o=root 484011852 484011853 IN IP4 54.172.60.22
s=Twilio Media Gateway
c=IN IP4 54.172.60.22
t=0 0
m=audio 15842 RTP/AVP 0 101
a=rtpmap:0 PCMU/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=ptime:20
a=maxptime:150
a=sendrecv
<------------->
--- (12 headers 12 lines) ---
Comparing SDP version 484011852 -> 484011853 and unique parts [root 484011852 IN IP4 54.172.60.22] -> [root 484011852 IN IP4 54.172.60.22]
Found RTP audio format 0
Found RTP audio format 101
Found audio description format PCMU for ID 0
Found audio description format telephone-event for ID 101
Capabilities: us - (ulaw|alaw), peer - audio=(ulaw)/video=(nothing)/text=(nothing), combined - (ulaw)
Non-codec capabilities (dtmf): us - 0x1 (telephone-event|), peer - 0x1 (telephone-event|), combined - 0x1 (telephone-event|)
> 0x7f8cdc02cd20 -- Strict RTP learning after remote address set to: 54.172.60.22:15842
Peer audio RTP is at port 54.172.60.22:15842
Transmitting (NAT) to 54.172.60.0:5060:
ACK sip:172.25.4.248:5060 SIP/2.0
Via: SIP/2.0/UDP 115.186.169.3:5060;branch=z9hG4bK213e5974;rport
Route: <sip:54.172.60.0;lr;twnat=sip:115.186.169.3:45204>
Max-Forwards: 70
From: <sip:923313355188@115.186.169.3>;tag=as2ca2fc8e
To: <sip:+19312594692@dialerqlu.pstn.twilio.com>;tag=22482593_c3356d0b_03398364-6662-4e59-a24b-06f392646973
Contact: <sip:923313355188@115.186.169.3:5060>
Call-ID: 6d59143016bb3ed06dd4e7022c9b7a42@115.186.169.3:5060
CSeq: 104 ACK
User-Agent: Asterisk PBX 16.30.1
Content-Length: 0
---
<--- SIP read from UDP:54.172.60.0:5060 --->
SIP/2.0 200 OK
CSeq: 104 INVITE
Call-ID: 6d59143016bb3ed06dd4e7022c9b7a42@115.186.169.3:5060
From: <sip:923313355188@115.186.169.3>;tag=as2ca2fc8e
To: <sip:+19312594692@dialerqlu.pstn.twilio.com>;tag=22482593_c3356d0b_03398364-6662-4e59-a24b-06f392646973
Via: SIP/2.0/UDP 115.186.169.3:5060;rport=45204;received=115.186.169.3;branch=z9hG4bK06ec52fb
Server: Twilio
Contact: <sip:172.25.4.248:5060>
Record-Route: <sip:54.172.60.0;lr;twnat=sip:115.186.169.3:45204>
X-Twilio-CallSid: CAbbacc39bbfcc97d090849fb7cabcf851
Content-Type: application/sdp
Content-Length: 250
v=0
o=root 484011852 484011853 IN IP4 54.172.60.22
s=Twilio Media Gateway
c=IN IP4 54.172.60.22
t=0 0
m=audio 15842 RTP/AVP 0 101
a=rtpmap:0 PCMU/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=ptime:20
a=maxptime:150
a=sendrecv
<------------->
--- (12 headers 12 lines) ---
Transmitting (NAT) to 54.172.60.0:5060:
ACK sip:172.25.4.248:5060 SIP/2.0
Via: SIP/2.0/UDP 115.186.169.3:5060;branch=z9hG4bK539f5e1b;rport
Route: <sip:54.172.60.0;lr;twnat=sip:115.186.169.3:45204>
Max-Forwards: 70
From: <sip:923313355188@115.186.169.3>;tag=as2ca2fc8e
To: <sip:+19312594692@dialerqlu.pstn.twilio.com>;tag=22482593_c3356d0b_03398364-6662-4e59-a24b-06f392646973
Contact: <sip:923313355188@115.186.169.3:5060>
Call-ID: 6d59143016bb3ed06dd4e7022c9b7a42@115.186.169.3:5060
CSeq: 104 ACK
User-Agent: Asterisk PBX 16.30.1
Content-Length: 0
---
-- AMD: Channel [SIP/twilio-0000005a]. No audio data received in [5000] seconds.
-- AMD: Channel [SIP/twilio-0000005a]. Too long...
-- Executing [s@amdcheck:2] NoOp("SIP/twilio-0000005a", "AMD result: NOTSURE") in new stack
-- Executing [s@amdcheck:3] GotoIf("SIP/twilio-0000005a", "0?machine:human") in new stack
-- Goto (amdcheck,s,6)
-- Executing [s@amdcheck:6] Stasis("SIP/twilio-0000005a", "bridge-dial,HUMAN") in new stack
-- Channel SIP/twinkleuser-00000059 left 'simple_bridge' stasis-bridge <aeef1555-6316-4487-ba39-79e06bf0f693>
-- Channel SIP/twinkleuser-00000059 joined 'simple_bridge' stasis-bridge <5fb8b0ad-6543-428a-942b-c5d5f354d7ae>
-- Channel SIP/twinkleuser-00000059 left 'simple_bridge' stasis-bridge <5fb8b0ad-6543-428a-942b-c5d5f354d7ae>
-- Channel SIP/twinkleuser-00000059 joined 'simple_bridge' stasis-bridge <cab007d9-4dc4-418e-8450-fe7b56875978>
-- Channel SIP/twilio-0000005a joined 'simple_bridge' stasis-bridge <5fb8b0ad-6543-428a-942b-c5d5f354d7ae>
-- Channel SIP/twilio-0000005a left 'simple_bridge' stasis-bridge <5fb8b0ad-6543-428a-942b-c5d5f354d7ae>
-- Channel SIP/twilio-0000005a joined 'simple_bridge' stasis-bridge <cab007d9-4dc4-418e-8450-fe7b56875978>
-- Channel SIP/twilio-0000005a left 'simple_bridge' stasis-bridge <cab007d9-4dc4-418e-8450-fe7b56875978>
-- Executing [s@amdcheck:1] AMD("SIP/twilio-0000005a", "") in new stack
-- AMD: SIP/twilio-0000005a 923313355188 (N/A) (Fmt: ulaw)
-- AMD: initialSilence [2500] greeting [1500] afterGreetingSilence [800] totalAnalysisTime [5000] minimumWordLength [120] betweenWordsSilence [50] maximumNumberOfWords [3] silenceThreshold [256] maximumWordLength [5000]
[Sep 21 18:24:01] WARNING[87723]: res_http_websocket.c:559 ws_safe_read: Web socket closed abruptly
[Sep 21 18:24:01] WARNING[87723]: ari/ari_websockets.c:126 ast_ari_websocket_session_read: WebSocket read error: Success
Deactivating Stasis app 'bridge-dial'
== WebSocket connection from '192.168.18.23:51336' closed
-- AMD: Channel [SIP/twilio-0000005a]. No audio data received in [5000] seconds.
-- AMD: Channel [SIP/twilio-0000005a]. Too long...
-- Executing [s@amdcheck:2] NoOp("SIP/twilio-0000005a", "AMD result: NOTSURE") in new stack
-- Executing [s@amdcheck:3] GotoIf("SIP/twilio-0000005a", "0?machine:human") in new stack
-- Goto (amdcheck,s,6)
-- Executing [s@amdcheck:6] Stasis("SIP/twilio-0000005a", "bridge-dial,HUMAN") in new stack
[Sep 21 18:24:06] ERROR[87749][C-00000033]: res_stasis.c:1351 stasis_app_exec: Stasis app 'bridge-dial' not active
-- Executing [s@amdcheck:7] Return("SIP/twilio-0000005a", "") in new stack
[Sep 21 18:24:06] ERROR[87749][C-00000033]: app_stack.c:390 return_exec: Return without Gosub: stack is unallocated
and this is the new code:
function clientLoaded(err, client) {
if (err) {
throw err;
}
function stasisStart(event, channel) {
var dialed = event.args[0] === 'dialed';
if (!dialed && !callInitiated) {
callInitiated = true;
channel.answer(function (err) {
if (err) {
throw err;
}
console.log('Channel %s has entered our application', channel.name);
originate(channel);
});
} else {
// After returning from AMD check
channel.getChannelVar({ variable: 'AMDSTATUS' }, function (err, variable) {
if (err) {
console.error("Error fetching AMDSTATUS:", err);
return;
}
if (variable.value === 'HUMAN') {
var playback = client.Playback();
channel.play({ media: 'sound:tt-monkeys' },
playback, function (err, playback) {
if (err) {
throw err;
}
});
}
});
}
}
function originate(channel) {
var dialed = client.Channel();
// channel.on('StasisEnd', function (event, channel) {
// hangupDialed(channel, dialed);
// });
// dialed.on('ChannelDestroyed', function (event, dialed) {
// hangupOriginal(channel, dialed);
// });
dialed.on('StasisStart', function (event, dialed) {
joinMixingBridge(channel, dialed);
dialed.on('StasisStart', function (event, dialed) {
joinMixingBridge(channel, dialed);
// Transferring dialed channel for AMD check.
dialed.continueInDialplan({
context: 'amdcheck',
exten: 's',
priority: 1
}, function (err) {
if (err) {
throw err;
}
});
});
});
// console.log("channelc,", channel)
var exten = channel.dialplan.exten;
console.log('EXTEN value:', exten);
dialed.originate({
endpoint: `SIP/twilio/${exten}`,
app: 'bridge-dial',
callerId: '923313355188',
context: 'webrtc_context',
exten: '+_19.',
priority: 1,
timeout: 30
}, function (err, originateResult) {
if (err) {
throw err;
}
console.log("Originated channel id:", originateResult.id, "originated name", originateResult.name);
});
}
// handler for original channel hanging up so we can hangup the other end
// function hangupDialed(channel, dialed) {
// console.log(
// 'Channel %s left our application, hanging up dialed channel %s',
// channel.name, dialed.name);
// callInitiated = false;
// dialed.hangup(function (err) {
// if (err) {
// console.error("Error hanging up dialed channel:", err);
// }
// });
// }
// function hangupOriginal(channel, dialed) {
// console.log('Dialed channel %s has been hung up, hanging up channel %s',
// dialed.name, channel.name);
// callInitiated = false;
// channel.hangup(function (err) {
// if (err) {
// console.error("Error hanging up original channel:", err);
// }
// });
// }
// handler for dialed channel entering Stasis
function joinMixingBridge(channel, dialed) {
var bridge = client.Bridge();
dialed.answer(function (err) {
if (err) {
throw err;
}
dialed.continueInDialplan({
context: 'amdcheck',
exten: 's', // Use 's' as the extension to start in the dialplan
priority: 1
}, function (err) {
if (err) {
throw err;
}
});
// var playback = client.Playback();
// dialed.play({ media: 'sound:tt-monkeys' }, playback, function (err, playback) {
// console.log("the id of playback sound is:::: ", playback.id)
// // wss.clients.forEach(function each(client) {
// // if (client.readyState === WebSocket.OPEN) {
// // client.send('PlaybackID: ' + playback.id);
// // }
// // });
// if (err) {
// throw err;
// }
// });
});
bridge.create({ type: 'mixing' }, function (err, bridge) {
if (err) {
throw err;
}
console.log('Created bridge %s', bridge.id);
addChannelsToBridge(channel, dialed, bridge);
});
// dialed.on('StasisEnd', function (event, dialed) {
// dialedExit(dialed, bridge);
// });
}
// handler for the dialed channel leaving Stasis
// function dialedExit(dialed, bridge) {
// console.log(
// 'Dialed channel %s has left our application, destroying bridge %s',
// dialed.name, bridge.id);
// bridge.destroy(function (err) {
// if (err) {
// throw err;
// }
// });
// }
// handler for new mixing bridge ready for channels to be added to it
function addChannelsToBridge(channel, dialed, bridge) {
console.log('Adding channel %s and dialed channel %s to bridge %s',
channel.name, dialed.name, bridge.id);
bridge.addChannel({ channel: [channel.id, dialed.id] }, function (err) {
if (err) {
throw err;
}
});
}
client.on('StasisStart', stasisStart);
client.start('bridge-dial');
}