0

When calling MVC controller action from twilio API view is not rendering. But when calling same controller action method from browser, view is loading properly.

I have updated controller action URL in Twilio webhook to call the controller action method.

public ActionResult ReceiveWhatsApp(SmsRequest incomingMessage)
    {
     WhatsAppTo = incomingMessage.From;
     msg = incomingMessage.Body;
     ViewBag.JavaScriptFunction = string.Format("Display('{0}');", msg);
     return View("Index");
    }


function Display(msg) {
    var conversationDiv = document.getElementById('conversation');
    var requestPara = document.createEle`enter code here`ment("P");
    requestPara.className = 'userRequest';
    requestPara.appendChild(document.createTextNode(msg));
    requestPara.appendChild(requestPara);
    conversationDiv.scrollTop = conversationDiv.scrollHeight;
    };

I want the end user's message to be display to the agent's view i.e. message window. But when the end user sending some message it's not being displayed to the agent's message window.

  • In times when the view doesn't render properly, is there an apostrophe in the `incomingMessage`? – DBro Jun 05 '19 at 11:50
  • Our view isn't rendering at all let alone apostrophe. – manoj pathak Jun 05 '19 at 14:26
  • Sorry, I should've been clearer with my question. If the string represented by `incomingMessage.Body` contains an apostrophe, that may result in a javascript error, and nothing being rendered, if the string isn't encoded. Have you placed a breakpoint on the line where you set `ViewBag.JavaScriptFunction`, and see what the value of `msg` is? – DBro Jun 05 '19 at 17:06
  • Side note: Generating javascript code from your server-side controller method is a bad practice to get into. Separation of concerns, and all. Instead, send the `incomingMessage.Body` text to the `View` using a `Model`, letting the `View` accept that `Model` and decide how to render it. – DBro Jun 05 '19 at 17:10
  • Have you placed a breakpoint on the line where you set ViewBag.JavaScriptFunction, and see what the value of msg is Yes I have tried this and getting the message exactly as sent by user in the string format. Just to be more clear the code working as expected when being called with the browser request but same is not true in case of Twilio API request. – manoj pathak Jun 06 '19 at 08:10

0 Answers0