Exemple #1
0
        private Request PrepareCallRequest( CallRequest request, out Call call)
        {
            call = new Call(){AccountSid=request.AccountSid, CallTo=request.To, AnswerUrl = request.AnswerUrl, Direction="outbound-api", CallerId=request.From, Timeout=request.TimeLimit};
            Queue<DialString> dialStrings = new Queue<DialString>();
            System.Text.StringBuilder args_list = new System.Text.StringBuilder();
            string sched_hangup_id = string.Empty;
            // don't allow "|" and "," in 'to' (destination) to avoid call injection
            request.To = request.To.Split(',', '|')[0];
            //set hangup_on_ring
            int hangup = 0;
            if (!int.TryParse(request.HangupOnRing, out hangup))
                hangup = -1;
            if (hangup == 0)
                args_list.Append("execute_on_ring='hangup ORIGINATOR_CANCEL',");
            else if (hangup > 0)
                args_list.Append(string.Format("execute_on_ring='sched_hangup +{0} ORIGINATOR_CANCEL',", hangup));
            // set send_digits
            if (!string.IsNullOrEmpty(request.SendDigits))
                args_list.Append(string.Format("execute_on_answer='send_dtmf {0}',", request.SendDigits));
            //set time_limit
            int time = 0;
            if (!int.TryParse(request.TimeLimit, out time))
                time = -1;
            if (time > 0)
            {
                //create sched_hangup_id
                sched_hangup_id = System.Guid.NewGuid().ToString();
                args_list.Append(string.Format("api_on_answer='sched_api {0} +{1} hupall ALLOTTED_TIMEOUT agbara_callSid {2}',", sched_hangup_id, request.TimeLimit, call.Sid));
                args_list.Append(string.Format("agbara_sched_hangup_id={0}", sched_hangup_id));
            }

            if (!string.IsNullOrEmpty(request.StatusCallbackUrl))
            {
                args_list.Append(string.Format("agbara_statuscallback_url={0},", request.StatusCallbackUrl));
                args_list.Append(string.Format("agbara_statuscallbackmethod={0},", request.StatusCallbackMethod));
            }

            if (!string.IsNullOrEmpty(request.FallbackUrl))
            {
                args_list.Append(string.Format("agbara_fallbackrul={0},", request.FallbackUrl));
                args_list.Append(string.Format("agbara_fallbackmethod={0},", request.FallbackMethod));
            }

            if (!string.IsNullOrEmpty(request.ApplicationSid))
            {
                args_list.Append(string.Format("agbara_applicationsid={0},", request.ApplicationSid));
            }

            args_list.Append(string.Format("agbara_callsid={0},", call.Sid));
            args_list.Append(string.Format("agbara_accountsid={0},", call.AccountSid));
            args_list.Append(string.Format("agbara_answer_url={0},", request.AnswerUrl));

            args_list.Append(string.Format("origination_caller_id_number={0},", request.From));

            //session_heartbeat
            args_list.Append(string.Format("enable_heartbeat_events={0},", 60));
            //Set Direction
            args_list.Append(string.Format("agbara_call_direction={0}", CallDirection.outboundapi));

            foreach (Gateway gate in _fsService.GetGatewaysForNumber(call.CallTo,FS.Sid))
            {
                for (int i = 1; i < int.Parse(gate.GatewayRetry); i++)
                {
                    DialString dialString = new DialString(call.Sid, request.To, gate.GatewayString, gate.GatewayCodec, gate.GatewayTimeout.ToString(), args_list.ToString());
                    dialStrings.Enqueue(dialString);
                }
            }
            Request call_req = new Request(call.Sid, dialStrings, request.AnswerUrl, request.StatusCallbackUrl, request.StatusCallbackMethod);
            return call_req;
        }
Exemple #2
0
        public void ON_BACKGROUND_JOB(Event ev)
        {
            Request call_req = new Request(null, null, null, null, null);

            string status = string.Empty;
            string reason = string.Empty;
            //Capture Job Event
            // Capture background job only for originate and ignore all other jobs
            string job_cmd = ev.GetHeader("Job-Command");
            string job_uuid = ev.GetHeader("Job-UUID");
            if (job_cmd == "originate" && string.IsNullOrEmpty(job_uuid))
            {
                try
                {
                    string data = ev.GetBody();
                    int pos = data.IndexOf(' ');
                    if (pos != -1)
                    {
                        status = data.Substring(0, pos).Trim();
                        reason = data.Substring(pos + 1).Trim();
                    }
                }
                catch (Exception ex)
                {
                    return;
                }
                string CallSid = BackgroundJobs[job_uuid];
                if (string.IsNullOrEmpty(CallSid))
                {
                    return;
                }
                try
                {
                    call_req = CallRequest[CallSid];
                }
                catch (Exception ex)
                {
                    return;
                }
                //Handle failure case of originate
                //This case does not raise a on_channel_hangup event.
                //All other failures will be captured by on_channel_hangup
                if (!status.Contains("OK"))
                    //In case ring/early state done, just warn releasing call request will be done in hangup event
                    if (call_req.state_flag == "Ringing" || call_req.state_flag == "EarlyMedia")
                    {
                        return;
                    }
                    //If no more gateways, release call request
                    else if (call_req.gateways.Count == 0)
                    {
                        //set an empty call_uuid
                        string call_uuid = "";
                        string hangup_url = call_req.hangup_url;
                        SetHangUpComplete(CallSid, call_uuid, reason, ev, hangup_url);
                        return;
                    }
                    //If there are gateways and call request state_flag is not set
                    //try again a call
                    else if (call_req.gateways.Count > 0)
                        Originate(CallSid);

            }
        }