Example #1
0
        private void _run()
        {
            connect();
            myevents();
            Resume();
            //Linger to get all remaining events before closing
            linger();
            myevents();
            if (_iseventjson)
            {
                EventJson("CUSTOM conference::maintenance agbara::dial");
            }
            else
            {
                EventPlain("CUSTOM conference::maintenance agbara::dial");
            }

            //get channel unique Id
            this.channel_uuid = get_channel_unique_id();
            AccountSid        = get_channel().GetHeader("variable_agbara_accountsid");
            CallSid           = get_channel().GetHeader("variable_agbara_callsid");
            ApiVersion        = get_channel().GetHeader("variable_agbara_apiversion");
            //Set agbara app flag
            set("agbara_app=true");
            //Don"t hangup after bridge
            set("hangup_after_bridge=false");
            CommandResponse channel   = get_channel();
            string          call_uuid = get_channel_unique_id();
            string          called_no = channel.GetHeader("Caller-Destination-Number");
            string          from_no   = channel.GetHeader("Caller-Caller-ID-Number");

            //Set To to Session Params
            session_params.Add("To", called_no.TrimStart(new Char['+']));
            //Set From to Session Params
            session_params.Add("From", from_no.TrimStart(new Char['+']));

            string aleg_uuid         = "";
            string aleg_request_uuid = "";
            string sched_hangup_id   = channel.GetHeader("variable_agbara_sched_hangup_id");
            string forwarded_from;
            string variable_sip_h_Diversion = channel.GetHeader("variable_sip_h_Diversion");

            if (string.IsNullOrEmpty(variable_sip_h_Diversion))
            {
                forwarded_from = "";
            }
            else
            {
                int startindex = variable_sip_h_Diversion.IndexOf(':');
                int length     = variable_sip_h_Diversion.IndexOf('@') - startindex;
                forwarded_from = variable_sip_h_Diversion.Substring(startindex, length);
            }
            var direction = channel.GetHeader("Call-Direction");

            if (direction == "outbound")
            {
                //# Look for variables in channel headers
                aleg_uuid         = channel.GetHeader("Caller-Unique-ID");
                aleg_request_uuid = channel.GetHeader("variable_agbara_request_uuid");
                // Look for target url in order below :
                // get agbara_transfer_url from channel var
                //  get agbara_answer_url from channel var
                string xfer_url   = channel.GetHeader("variable_agbara_transfer_url");
                string answer_url = channel.GetHeader("variable_agbara_answer_url");
                if (CurrentElement == "Dial")
                {
                    session_params.Add("Direction", CallDirection.outbounddial);
                }
                else
                {
                    session_params.Add("Direction", CallDirection.outboundapi);
                }

                if (!string.IsNullOrEmpty(xfer_url))
                {
                    this.target_url = xfer_url;
                }
                else if (!string.IsNullOrEmpty(answer_url))
                {
                    this.target_url = answer_url;
                }
                else
                {
                    return;
                }
                //Look for a sched_hangup_id

                //Don"t post hangup in outbound direction because it is handled by inboundsocket
                default_hangup_url = "";
                hangup_url         = "";
                //Set CallStatus to Session Params
                session_params.Add("CallStatus", CallStatus.inprogress);
            }
            else
            {
                session_params.Add("Direction", CallDirection.inbound);
                //Look for target url in order below :
                //get agbara_transfer_url from channel var
                //get agbara_answer_url from channel var
                //get default answer_url from config
                string xfer_url   = this.GetVar("agbara_transfer_url");
                string answer_url = this.GetVar("agbara_answer_url");
                if (!string.IsNullOrEmpty(xfer_url))
                {
                    this.target_url = xfer_url;
                }
                else if (!string.IsNullOrEmpty(answer_url))
                {
                    this.target_url = answer_url;
                }
                else if (!string.IsNullOrEmpty(default_answer_url))
                {
                    this.target_url = default_answer_url;
                }
                else
                {
                    return;
                }
            }

            //Look for a sched_hangup_id
            sched_hangup_id = GetVar("agbara_sched_hangup_id", get_channel_unique_id());
            //Set CallStatus to Session Params
            //this.session_params.Add("CallStatus", CallStatus.ringing);

            if (string.IsNullOrEmpty(sched_hangup_id))
            {
                sched_hangup_id = "";
            }

            //Add more Session Params
            session_params.Add("AccountSid", channel.GetHeader("variable_agbara_accountsid"));
            session_params.Add("CallSid", channel.GetHeader("variable_agbara_callsid"));
            if (!string.IsNullOrEmpty(forwarded_from))
            {
                session_params.Add("ForwardedFrom", forwarded_from.TrimStart(new Char[] { '+' }));
            }

            // Remove sched_hangup_id from channel vars
            if (!string.IsNullOrEmpty(sched_hangup_id))
            {
                unset("agbara_sched_hangup_id");
            }

            // Run application
            try
            {
                Console.WriteLine("Processing call");
                ProcessCall();
            }
            catch (Exception ex)
            {
                //except RESTHangup:
                //    this.log.warn("Channel has hung up, breaking Processing Call")
                //except Exception, e:
                //    this.log.error("Processing Call Failure !")
                //    # If error occurs during xml parsing
                //    # log exception and break
                //    this.log.error(str(e))
                //    [ this.log.error(line) for line in \
                //                traceback.format_exc().splitlines() ]
                //this.log.info("Processing Call Ended")
            }
        }