从Twilio API调用操作方法时,会话值会丢失,但通过浏览器请求进行调用时,会话值将按预期工作。
public ActionResult ReceiveWhatsApp(SmsRequest incomingMessage)
{
System.Web.HttpContext.Current.Session["WhatsAppTo"] = "whatsapp:+917503317910"; //incomingMessage.From;
System.Web.HttpContext.Current.Session["WhatsAppMSG"] = incomingMessage.Body;
ViewBag.JavaScriptFunction = string.Format("Display('{0}');", Convert.ToString(System.Web.HttpContext.Current.Session["WhatsAppMSG"]));
return View("Index");
}
public JsonResult ReplyOnWhatsApp(string sms)
{
try
{
if (!string.IsNullOrEmpty(Convert.ToString(System.Web.HttpContext.Current.Session["WhatsAppTo"])))
{
string To = System.Web.HttpContext.Current.Session["WhatsAppTo"].ToString();
string accountSid = ConfigurationManager.AppSettings["WhatsAppAccountSid"].ToString();
string authToken = ConfigurationManager.AppSettings["WhatsAppAuthToken"].ToString();
TwilioClient.Init(accountSid, authToken);
var message = MessageResource.Create(
body: sms,
from: new Twilio.Types.PhoneNumber("whatsapp:+14155238886"),
to: new Twilio.Types.PhoneNumber(To)
);
status.isSuccess = true;
}
else
{
status.isSuccess = false;
status.message = "User not connected";
}
return Json(status, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
status.isSuccess = false;
return Json(status, JsonRequestBehavior.AllowGet);
}
}