Beispiel #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;
        }
Beispiel #2
0
        public bool Originate(string CallSid)
        {
            Request CallReq;
            bool ret = false;
            string outbound_str = string.Empty;
            DialString gw = new DialString(null, null, null, null, null, null);
            try
            {
                CallReq = CallRequest[CallSid];
            }
            catch (Exception ex)
            {
                return false;
            }

            try
            {
                gw = CallReq.gateways.Dequeue();
            }
            catch (Exception ex)
            {
                CallRequest.Remove(CallSid);
                return false;
            }

            StringBuilder _options = new StringBuilder();
            //Set agbara app flag
            _options.Append("agbara_app=true,");
            if (!string.IsNullOrEmpty(gw.timeout))
            {
                _options.Append(string.Format("originate_timeout={0},", gw.timeout));
            }
            _options.Append("ignore_early_media=true,");
            if (!string.IsNullOrEmpty(gw.codecs))
            {
                _options.Append(string.Format("absolute_codec_string={0}", gw.codecs));
            }

            if (!string.IsNullOrEmpty(FSOutboundAddress))
            {
                outbound_str = string.Format("'socket:{0} async full' inline", FSOutboundAddress);
            }
            else
            {
                outbound_str = "&park()";
            }

            string dial_str = string.Format("originate {0}{1},{2}{3}{4}/{5} {6}", "{", gw.extra_dial_string, _options, "}", gw.gw, gw.to, outbound_str);
            BgApiResponse ApiResponse = (BgApiResponse)Task<Event>.Factory.StartNew(() => BgAPICommand(dial_str)).Result;

            if (ApiResponse.IsSuccess())
            {
                string job_uuid = ApiResponse.GetJobUUID();
                if (!string.IsNullOrEmpty(job_uuid))
                {

                    BackgroundJobs[job_uuid] = CallSid;
                    ret = true;

                }
                else
                {
                    ret = false;
                }
            }
            return ret;
        }