c#中Twilio Enqueue的语法

本文关键字:语法 Enqueue Twilio | 更新日期: 2023-09-27 18:01:42

我正在使用Twilio可编程语音,我的目标是让来电者在等待代理接听电话时收听在线音乐。

我相信Enqueue方法是可行的,因为我们在数据库中管理代理,它们可能会改变(所以Task Worker不适合)。我很清楚如何将调用者放在队列中(见下文)。但是,我需要拨出到一个外部号码,让电话上的代理将该呼叫从队列中取出,我找不到c#语法如何实现这一点。

我的计划是让每个调用者都在他们自己唯一标识的队列中。但如果这让它变得更困难,我想我愿意把调用者放在同一个队列中。

public ActionResult Index()
        {
            var response = new TwilioResponse(); 
            response.Enqueue("Queue 23123412414124", new
            {
                action = Url.Action("LeaveQueue"),       //url to call when the call is dequeued
                waitUrl = Url.Action("WaitInQueue")    //url to call while the call waits
            });      
            return TwiML(response);
        }

谢谢!

(更新1)以下是我在TestController中使用ngrok进行本地测试的内容。我不太清楚我是否得到了一个干净的挂机,但它确实有效!

        public ActionResult Index()
        {
            var response = new TwilioResponse();

            var Twil = new TwilioRestClient("TWILIO_SID", "TWILIO_PWD");
            Twil.InitiateOutboundCall(new CallOptions
            {
                To = "+17205551212",
                From = "+17205551212",
                Url = "http://701cfc2f.ngrok.io/Test/ContactAgent"
            });
            response.Enqueue("Demo Queue", new
            {
                action = Url.Action("LeaveQueue"),       //url to call when the call is dequeued
                waitUrl = Url.Action("WaitInQueue")    //url to call while the call waits
            });            
            return TwiML(response);
        }
        public ActionResult ContactAgent()
        {
            var response = new TwilioResponse();
            Twilio.TwiML.Queue q = new Twilio.TwiML.Queue("Demo Queue");
            response.Dial(q);
            return TwiML(response);
        }
        public ActionResult WaitInQueue(string CurrentQueueSize, string QueuePosition)
        {
            var response = new TwilioResponse();
            response.Say(string.Format("You are number {0} in the queue.  Please hold.", QueuePosition));
            response.Say("Play Background Music Here with Play Verb and loop it!");
            return TwiML(response);
        }
        public ActionResult LeaveQueue(string QueueSid)
        {
            var response = new TwilioResponse();
            var Twil = new TwilioRestClient("TWILIO_SID", "TWILIO_PWD");
            Twil.HangupCall(QueueSid, HangupStyle.Completed);
            return TwiML(response);
        }

c#中Twilio Enqueue的语法

根据上面的代码片段,我怀疑您已经知道,如果您的代理连接到呼叫将会容易得多。

https://www.twilio.com/docs/api/twiml/guides/queues代理

如果您需要在调用代理时播放等待音乐,您可以使用TwiML和REST API的组合。请参阅下面的示例,并参考此处的语法。

1) main.cs -你需要为这个文件分配一个URL到你的Twilio电话号码。此功能将使传入呼叫排队,然后通过Rest API启动一个新的调用分支来调用您的Agent,并将两个调用分支连接到一个会话中。

// Download the twilio-csharp library from twilio.com/docs/csharp/install
using System;
using Twilio;
class Example 
{
  static void Main(string[] args) 
  {
    // Find your Account Sid and Auth Token at twilio.com/user/account
    string AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    string AuthToken = "your_auth_token";
    var twilio = new TwilioRestClient(AccountSid, AuthToken);
    var options = new CallOptions();
    options.Url = "http://URL/contactAgent.cs?queueid=NAME_YOUR_QUEUE";
    options.To = "+1415XXXXXXX"; // Agent phone 
    options.From = "+1415XXXXXXX";// Twilio phone 
    var call = twilio.InitiateOutboundCall(options);
    var response = new TwilioResponse();
    response.Enqueue("Queue 23123412414124", new { action="http://URL/terminate_childcall.php?childsid=YOUR_CALL_SID_IN_QUEUE_ID", waitURL="wait.xml"})
  }
}

2) contactAgent.cs -该文件包含等效的TwiML,它将在您的代理的电话上执行,当一个来电被接受。然后,它会将两个呼叫腿连接到一个对话中。

<Response>
    <Say>You will be connected to an incoming call</Say>
    <Dial><Queue>YOUR_QUEUE_ID</Queue></Dial>

3) terminate_childcall.cs -由于通过Rest API发起的呼叫分支是绝对独立的呼叫分支,我们需要在发起传入呼叫的客户将是第一个终止呼叫的客户时终止它。

  // Download the twilio-csharp library from twilio.com/docs/csharp/install
    using System;
    using Twilio;
    class Example 
    {
      static void Main(string[] args) 
      {
        // Find your Account Sid and Auth Token at twilio.com/user/account
        string AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        string AuthToken = "your_auth_token";
        var twilio = new TwilioRestClient(AccountSid, AuthToken);
        // Get an object from its sid. If you do not have a sid,
       // check out the list resource examples on this page
       twilio.HangupCall("CALL_SID", HangupStyle.Completed);
      }
    }

如果有帮助请告诉我。