更改Twiml响应消息<say>

时间:2018-06-01 08:03:32

标签: c# twilio twiml

我创建了应用程序,我正在使用twilio来拨打电话。但是,无论何时我打电话,我都有与静态Hello相同的XML文档,您的帐户将被删除。但这次我想在其中添加参数。例如,您好,您的帐户{accountnumber}已被删除。 我的代码如下: -

public void call()
{
    // Find your Account Sid and Token at twilio.com/console
    const string accountSid = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    const string authToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

    TwilioClient.Init(accountSid, authToken);
    try
    {


        var call = CallResource.Create(
                    method: Twilio.Http.HttpMethod.Get,
                    url: new Uri("https://automatecondominium.com/Services/Twilio/VoiceMessages/twiliomessage.xml"),
                    to: new Twilio.Types.PhoneNumber("+917018244303"),
                    from: new Twilio.Types.PhoneNumber("+15206197315")
                    );


    }
    catch (Exception)
    {

        throw;
    }


}

1 个答案:

答案 0 :(得分:0)

您不应该将大型参数作为URL的一部分发送。

您将语言,语音和消息参数作为网址的一部分。您应该将这些值生成为代码的一部分,它们不应作为参数出现。您应该只有一个参数值,您可以根据该参数值生成所有这些值,并使用TwiML发送。

考虑一下。

public void MakeCall()
{
    const string accountSid = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    const string authToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    TwilioClient.Init(accountSid, authToken);
    try
    {
        //Generate and store message data in some datastore and 
        //Create and identifier to get that data later.
        // GenerateMessageData method does the same thing.
        var messageId = GenerateMessageData();

        //Pass the messageId as URL parameter.
        var call = CallResource.Create(
                    method: Twilio.Http.HttpMethod.Get,
                    url: new Uri("https://automatecondominium.com/twilio/twiml?p="+messageId),
                    to: new Twilio.Types.PhoneNumber("+917018244303"),
                    from: new Twilio.Types.PhoneNumber("+15206197315")
                    );

    }
    catch (Exception)
    {
         throw;
    }
}

private string GenerateMessageData()
    {
        var messageId = Guid.NewGuid().ToString();

        var messageContent = "Some Message"; // This could be any message you want.
        var language = "Somelanguage"; //this could be any language you want.
        var voice = "SomeVoice"; // Male or Female whichever you want. 
        var messageData = new MessageData {MessageId = messageId, Message =  messageContent, Language = language, Voice = voice };

        //Save messageData to database or some data store so that you can retrieve it later.

        return messageId;
    }

以下是MessageData类。

public class MessageData
{
    public string MessageId {get;set;}
    public string Voice {get;set;}
    public string Language {get;set;}
    public string Message {get;set;}       
}

现在我需要为twilio/twiml?p创建一个服务器请求comint的控制器操作。

在我的TwilioController课程中考虑使用控制器操作方法。

[Route("~/twiml/{p}")]
    [HttpPost]
    public IActionResult GetTwiml(string p)
    {
        //Get the messagedata from the datastore based on the messageId retrieved in request.
        var messageData = GetMessageData(p);
        //initializing Voice Response For creating XML
        var response = new VoiceResponse();

        // I have no idea why you have lot of comparison of Voice with string.empty and "0" 
        // so I am not changing it.
        // I am just replacing the Voice, Language and Message variables 
        //with the property values from messageDat object.
        if ((messageData.Voice != string.Empty && messageData.Voice != "0") && (messageData.Voice == "0"))
        {
            //Combining dynamic Message and selecting voice for reading message
            response.Say(messageData.Message, voice: messageData.Voice);
            var XML = new TwiMLResult(response.ToString());
            return XML;
        }
        if ((messageData.Voice != string.Empty && messageData.Voice != "0") && (messageData.Voice != string.Empty && messageData.Voice != "0"))
        {
            //Combining dynamic Message and selecting voice for reading message
            response.Say(messageData.Message, voice: messageData.Voice, language: messageData.Language);
            var XML = new TwiMLResult(response.ToString());
            return XML;
        }
        if ((messageData.Voice == string.Empty || messageData.Voice == "0") && (messageData.Voice == string.Empty || messageData.Voice == "0"))
        {
            //Combining dynamic Message and selecting voice for reading message
            response.Say(messageData.Message);
            var XML = new TwiMLResult(response.ToString());
            return XML;
        }
        return null;
    }

private MessageData GetMessageData(string messageId)
{
    MessageData messageData ;
    //retrieve message data based on the messageId and return;

    return messageData;
}

通过这种方式,您可以缩短网址,并为语音通话生成动态内容。

在上面的代码中,MessageData类就是例如。根据您的要求,您可能有完全不同的场景。