问题
I have poured over the documentation and read the related posts but I am still unable to successfully construct the Json for an Action array containing multiple elements.
The first element is a "Remember" action containing several elements of its own. The second Action element is a "Collect" action.
I am able to add the first element but the second element eludes me. The code I am posting contains errors. I am unable to add the second "Collect" element. Am I close or way off? Any guidance is greatly appreciated.
[HttpPost]
[Route("AcceptTask")]
internal string BuildAcceptCall(UserData ud)
{
log.Debug("Entering BuildAcceptCall");
var j = new
{
actions = new[]
{
new
{
remember = new
{
ud.AccountId,
EngineId = ud.EngineId,
ResidentTelephone = ud.ResidentTelephone,
OutboundCallerId = ud.OutboundCallerId,
UnitNumber = ud.UnitNumber,
BuildingNumber = ud.BuildingNumber,
Pets = ud.Pets,
Alarm = ud.Alarm,
EmergencyId = ud.EmergencyId,
CallDate = ud.CallDate,
WorkOrder = ud.WorkOrder,
CurrentLocation = ud.CurrentLocation
}, //close remember element
collect = new
{
name = "DidTechAcceptCall",
questions = new[]
{
new
{
question = "This is a maintenance call from Spring Meadows. will you accept the call?",
name = "OffferCallToTech",
type="Twilio.YES_NO"
}
},
on_complete = new
{
redirect = new
{
uri = @"https://8.40.212.131/MedsDispatch/api/Dispatch/EntryPoint_AutoPilot",
method = "post"
}
}
}//close the collect element
} //close the new
}; //close the array
string theObject = Newtonsoft.Json.JsonConvert.SerializeObject(j); ;
return Newtonsoft.Json.JsonConvert.SerializeObject(j);
}
回答1:
I highly recommend that you do one or all of the following
- Use Twilio SDK for C# and .NET and Docs for creating a
Taskin C#. - Any kind of static task can be easily built off Twilio's AutoPilot Console
Anyways, here is the code
var j = new { actions = new object[]{
new{
remember = new{
data = "data"
}
},
new{
collect = new{
name = "DidTechAcceptCall",
questions = new[]
{
new
{
question = "This is a maintenance call from Spring Meadows. will you accept the call?",
name = "OffferCallToTech",
type="Twilio.YES_NO"
}
},
on_complete = new
{
redirect = new
{
uri = @"https://8.40.212.131/MedsDispatch/api/Dispatch/EntryPoint_AutoPilot",
method = "post"
}
}
}
}
}
};
string theObject = Newtonsoft.Json.JsonConvert.SerializeObject(j);
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(j));
that produces
{
"actions": [
{
"remember": {
"data": "data"
}
},
{
"collect": {
"name": "DidTechAcceptCall",
"questions": [
{
"question": "This is a maintenance call from Spring Meadows. will you accept the call?",
"name": "OffferCallToTech",
"type": "Twilio.YES_NO"
}
],
"on_complete": {
"redirect": {
"uri": "https://8.40.212.131/MedsDispatch/api/Dispatch/EntryPoint_AutoPilot",
"method": "post"
}
}
}
}
]
}
来源:https://stackoverflow.com/questions/56305379/returning-json-in-autopilot-with-c-sharp