0

I have created application and i am using twilio to make outbound calls. But, whenever i make call i have same XML document with me that has static Hello, your account is deleted. but this time i want to add parameters in it too. for example Hello, your account {accountnumber} is deleted. My code is as follow :-

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;
    }


}
  • You need to have an API endpoint in your web application which will be called by twilio to get the TwiML for making calls. The API end point should generate the TwiML dynamically and return it to Twilio. The API should have some parameter which it will use to identify the username or account number and fill it in the TwiML. And you should generate API url with that parameter and set it to url when doing `CallResource.Create` – Chetan Jun 01 '18 at 08:09
  • does that mean i need to create a method in my application that returns code dynamically ? that can return Twiml(response) ? – Arshdeep Singh Jun 01 '18 at 08:23
  • just like this [HttpPost] public ActionResult Index() { var response = new VoiceResponse(); response.Say("hello world!", voice: "alice"); return TwiML(response); } – Arshdeep Singh Jun 01 '18 at 08:24
  • Yes.. Correct .. That's what you need to do. – Chetan Jun 01 '18 at 08:42
  • Thankyou @ChetanRanpariya – Arshdeep Singh Jun 01 '18 at 09:36
  • how can i post parameters if my method is post ? That is:- var call = CallResource.Create( method: Twilio.Http.HttpMethod.Post, url: new System.Uri(URL), to: new Twilio.Types.PhoneNumber(To), from: new Twilio.Types.PhoneNumber("XXXxXXX") ); – Arshdeep Singh Jun 07 '18 at 10:55
  • You can not pass anything extra when you call `CallResource.Create`. You should have all the necessary information in the Url itself in the form of query string parameters. – Chetan Jun 07 '18 at 11:04
  • https://www.twilio.com/docs/voice/tutorials/how-to-make-outbound-phone-calls-csharp – Chetan Jun 07 '18 at 11:07
  • Ah, thats not a good new for me, because i had a long message to send to my endpoint function. but unfortunately i cannot do that using Post methods. i am now using Get method and passing paramters in url. but the thing is that i cannot send extended message as Url get to long and returns 400 bad request – Arshdeep Singh Jun 07 '18 at 11:22

1 Answers1

0

You should not be sending having large parameters as part of the URL.

You are having language, voice and message parameters as part of the URL. You should be generating those values as part of the code, they should not be coming as parameters. You should only have a parameter value based on which you can generate all these values and send in TwiML.

Consider following.

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;
    }

Following is the MessageData class.

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

Now I need to create a controller action to server request comint to twilio/twiml?p.

Consider following controller action method in my TwilioController class.

[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;
}

This way you can keep your URL shorter and still generate dynamic content for Voice Call.

In above code, MessageData class is there for example. You might have complete different scenario based on your requirements.

Chetan
  • 6,711
  • 3
  • 22
  • 32