Created
August 11, 2011 18:49
-
-
Save mheadd/1140416 to your computer and use it in GitHub Desktop.
C# /ASP.NET code behind showing how to process and use Tropo Result object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using TropoCSharp.Tropo; | |
| namespace TropoDemo | |
| { | |
| public partial class _Default : System.Web.UI.Page | |
| { | |
| protected void Page_Load(object sender, EventArgs e) | |
| { | |
| // Create a new instance of the Tropo object. | |
| Tropo tropo = new Tropo(); | |
| // A prompt to use with the Ask. | |
| Say say = new Say("This is a Tropo test application. Please enter a number 0 through 9."); | |
| // Choices for the Ask. | |
| Choices choices = new Choices("[1 DIGIT]"); | |
| // Build the Ask object. | |
| tropo.Ask(3, false, choices, null, "test", true, say, 30); | |
| // Set the handler for the continue event, fired when the caller enters input. | |
| tropo.On("continue", "Result.aspx", null); | |
| // Render the dialog JSON for Tropo to consume. | |
| Response.Write(tropo.RenderJSON()); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| using Newtonsoft.Json; | |
| using Newtonsoft.Json.Linq; | |
| using TropoCSharp.Tropo; | |
| namespace TropoDemo | |
| { | |
| public partial class TropoResult : System.Web.UI.Page | |
| { | |
| private string GetJSON(StreamReader reader) | |
| { | |
| return reader.ReadToEnd(); | |
| } | |
| private JObject parseActions(string actions) | |
| { | |
| return JObject.Parse(actions); | |
| } | |
| protected void Page_Load(object sender, EventArgs e) | |
| { | |
| using (StreamReader sr = new StreamReader(Request.InputStream)) | |
| { | |
| // Get the JSON submitted from Tropo. | |
| string resultJSON = GetJSON(sr); | |
| Tropo tropo = new Tropo(); | |
| try | |
| { | |
| // Create a new Result object and pass it the JSON from Tropo. | |
| Result tropoResult = new Result(resultJSON); | |
| // Parse the Actions object and get the value property. | |
| JObject Actions = parseActions(tropoResult.Actions.ToString()); | |
| string Value = (string)Actions["value"]; | |
| // Say the value to the caller. | |
| tropo.Say("You entered, " + Value); | |
| } | |
| catch (JsonReaderException ex) | |
| { | |
| tropo.Say("Sorry, there was an error with the JSON."); | |
| } | |
| catch (Exception ex) | |
| { | |
| tropo.Say("Sorry, An error occured."); | |
| } | |
| finally | |
| { | |
| // Render the JSON for Tropo to consume. | |
| tropo.Hangup(); | |
| Response.Write(tropo.RenderJSON()); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment