AMD with stasis

Hey everyone!
I am facing issue with AMD detection with stasis, as far as I have researched there is no way to detect AMD with stasis application, Basically I am doing everything like originating call and adding channels on bridge and 2 way communication through stasis, using Twilio as SIP trunk provider.
Now what I am trying to do is moving from stasis application to dialplan for AMD detection and then moving back to stasis from dialplan with appropriate AMDSTATUS, and then in stasis they both will be handled accordingly.
But the issue is when I move from stasis to dialplan I see this:

-- Channel SIP/twinkleuser-00000033 left 'simple_bridge' stasis-bridge <ef1a18fc-cb0f-45a8-983b-744541241271>
    -- Executing [s@amdcheck:1] AMD("SIP/twinkleuser-00000033", "") in new stack

and it then abruptly hangsup.
Will appreciate help,
TIA

You haven’t provided the dialplan you’ve sent the call to.

this is my dialplan:

[amdcheck]
exten => s,1,AMD()
 same => n,NoOp(AMD result: ${AMDSTATUS})
 same => n,GotoIf($["${AMDSTATUS}"="MACHINE"]?machine:human)
 same => n(machine),Stasis(bridge-dial,MACHINE)
 same => n,Return()
 same => n(human),Stasis(bridge-dial,HUMAN)
 same => n,Return()

[default]
exten => _+92X.,1,Set(CALLERID(num)=923313355188)
same => n,Dial(SIP/twilio/${EXTEN})
same => n,Hangup()

[webrtc_context]

exten => _+19X.,1,Set(CALLERID(num)=*********)
same => n,Set(EXTEN=${EXTEN})
same => n,Stasis(bridge-dial,${EXTEN})

and this is my stasis application:

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: '**********',
            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;
            }
            channel.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');
}

How about the COMPLETE console output for it as well?

these are my asterisk’s logs:

---
    -- SIP/twilio-00000036 answered
       > Launching Stasis(bridge-dial) on SIP/twilio-00000036
    -- Channel SIP/twinkleuser-00000035 joined 'simple_bridge' stasis-bridge <2595db70-f617-4c8d-914a-3dbf6a8b4114>
    -- Channel SIP/twilio-00000036 joined 'simple_bridge' stasis-bridge <2595db70-f617-4c8d-914a-3dbf6a8b4114>
Audio is at 13322
Adding codec ulaw to SDP
Adding non-codec 0x1 (telephone-event) to SDP
Reliably Transmitting (NAT) to 54.172.60.0:5060:
INVITE sip:172.25.16.133:5060 SIP/2.0
Via: SIP/2.0/UDP 115.186.169.3:5060;branch=z9hG4bK6ceb423a;rport
Route: <sip:54.172.60.0;lr;twnat=sip:115.186.169.3:44427>
Max-Forwards: 70
From: <sip:923313355188@115.186.169.3>;tag=as091ed55f
To: <sip:+19312594692@dialerqlu.pstn.twilio.com>;tag=53800317_c3356d0b_1c91b1e1-7085-490a-9e11-7374bbda29cf
Contact: <sip:923313355188@115.186.169.3:5060>
Call-ID: 78db87d9192970c621a68965049d8e17@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: 238

v=0
o=root 88443416 88443418 IN IP4 115.186.169.3
s=Asterisk PBX 16.30.1
c=IN IP4 115.186.169.3
t=0 0
m=audio 13322 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

---
    -- Channel SIP/twinkleuser-00000035 left 'simple_bridge' stasis-bridge <2595db70-f617-4c8d-914a-3dbf6a8b4114>
    -- Executing [s@amdcheck:1] AMD("SIP/twinkleuser-00000035", "") in new stack
    -- AMD: SIP/twinkleuser-00000035 twinkleuser (N/A) (Fmt: ulaw)
    -- AMD: initialSilence [2500] greeting [1500] afterGreetingSilence [800] totalAnalysisTime [5000] minimumWordLength [120] betweenWordsSilence [50] maximumNumberOfWords [5] silenceThreshold [256] maximumWordLength [5000] 
    -- <SIP/twilio-00000036> Playing 'tt-monkeys.gsm' (language 'en')
[Sep 21 15:03:31] WARNING[81381]: res_stasis_playback.c:280 playback_final_update: dcebe7e8-d7d8-48f7-bca5-5fda6fa77874: Playback failed for sound:tt-monkeys
    -- Channel SIP/twilio-00000036 left 'simple_bridge' stasis-bridge <2595db70-f617-4c8d-914a-3dbf6a8b4114>
Scheduling destruction of SIP dialog '78db87d9192970c621a68965049d8e17@115.186.169.3:5060' in 17088 ms (Method: INVITE)
    -- AMD: Channel [SIP/twinkleuser-00000035]. HANGUP
Scheduling destruction of SIP dialog 'icpf97gpngc7hk66rovg' in 6400 ms (Method: ACK)
Reliably Transmitting (NAT) to 192.168.18.23:43924:
BYE sip:17sug2ep@ij7fru1abcu5.invalid;transport=ws;ob SIP/2.0
Via: SIP/2.0/WS 192.168.18.23:5060;branch=z9hG4bK7399ebe8;rport
Max-Forwards: 70
From: <sip:+19312594692@192.168.100.15>;tag=as5219db49
To: <sip:twinkleuser@192.168.100.15>;tag=s5ussgt6jl
Call-ID: icpf97gpngc7hk66rovg
CSeq: 102 BYE
User-Agent: Asterisk PBX 16.30.1
Proxy-Authorization: Digest username="r8fn3krj", realm="asterisk", algorithm=MD5, uri="sip:192.168.100.15", nonce="3c8a80c7", response="f34bc5e1c511173b714e82f717be784f"
X-Asterisk-HangupCause: Normal Clearing
X-Asterisk-HangupCauseCode: 16
Content-Length: 0


and these are the logs of my stasis app:

EXTEN value: +19312594692
Originated channel id: dcebe7e8-d7d8-48f7-bca5-5fda6fa77874 originated name SIP/twilio-00000036
Error fetching AMDSTATUS: Error: {
  "message": "Provided variable was not found"
}
    at SwaggerRequest.swaggerError [as errorCallback] (/home/maha/hello-world/node_modules/ari-client/lib/resources.js:949:19)
    at Object.error (/home/maha/hello-world/node_modules/swagger-client/lib/swagger.js:1077:24)
    at EventEmitter.error (/home/maha/hello-world/node_modules/swagger-client/lib/swagger.js:1296:19)
    at EventEmitter.emit (node:events:514:28)
    at emit (/home/maha/hello-world/node_modules/shred/lib/shred/request.js:454:21)
    at /home/maha/hello-world/node_modules/shred/lib/shred/request.js:473:9
    at setBodyAndFinish (/home/maha/hello-world/node_modules/shred/lib/shred/response.js:103:7)
    at IncomingMessage.<anonymous> (/home/maha/hello-world/node_modules/shred/lib/shred/response.js:120:7)
    at IncomingMessage.emit (node:events:526:35)
    at endReadableNT (node:internal/streams/readable:1359:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
Created bridge 2595db70-f617-4c8d-914a-3dbf6a8b4114
Adding channel SIP/twinkleuser-00000035 and dialed channel SIP/twilio-00000036 to bridge 2595db70-f617-4c8d-914a-3dbf6a8b4114
Channel SIP/twinkleuser-00000035 left our application, hanging up dialed channel undefined
the id of playback sound is::::  6753466b-fca9-45c4-be74-893b7ad2d654
Dialed channel SIP/twilio-00000036 has left our application, destroying bridge 2595db70-f617-4c8d-914a-3dbf6a8b4114
Dialed channel SIP/twilio-00000036 has been hung up, hanging up channel SIP/twinkleuser-00000035

It is not reading the AMDSTATUS variable but the issue is not in that right now as it does not have that value in the start

I would suggest creating a test application to verify a single channel first, to rule out explicit logic in your existing ARI application. For example you have logic to hangup channels, which despite not being in Stasis, could/would hangup twinkleuser.

Actually I have two use cases

  1. 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)

  2. 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');
}

It’s not the same issue. It’s a completely different result. AMD was successfully executed but didn’t get any audio (so check audio) so it was not sure. It then tried to go back to your ARI application but it had disconnected so it couldn’t and then hung up.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.