Generating a Response

Old forums and posts related to unsupported version of Syn Virtual Assistant.

Generating a Response

Postby Leslie » Thu Oct 02, 2014 6:05 pm

A good option in Syn Engine is that for every arbitrary phrase that is mapped to a discrete function the developer has an option to attach a Response whenever the function is executed.

Messages and Responses have different meanings in different scenarios. If the Caller sends a message to the Engine its a Message. If a function wants to reply to the Caller then its a Response. But a Response from a function is a message to a Caller. Alright lets crack the confusion here.

Lets say you have a function that adds 2 numbers. Your application can actually just add the 2 numbers internally and output the result via any mechanism or it can just Reply|Respond to the Caller itself.

Sample code to respond to a Caller inside a function. Say Add function that takes 2 numbers.

CSharp Code

//Function with signaure "Add [number] [number]" Example Sentence "Add 1 and 2"
public IntegerData AddNumber(MathAddTerm cmd, IntegerData firstNumber, IntegerData secondNumber)
{
IntegerData toReturn = new IntegerData();
toReturn.Value = firstNumber.Value + secondNumber.Value;
toReturn.Data.Response.Message = "The result of addition is " + toReturn.Value.ToString();
return toReturn;
}


The above just demonstrates how you can add a message to the Response property of a SynData class. Note that the function above does not return the Response as an object instead it returns an IntegerData with a message added to its Response property.

To take more control of the Response lets see a sample code.

CSharp Code

IntegerData toReturn = new IntegerData();
toReturn.Value = firstNumber.Value + secondNumber.Value;

SynResponse response = new SynResponse();
response.Message = "The result of subtraction is " + toReturn.Value.ToString();
response.Speak = true;
response.ToSpeak = toReturn.Value.ToString();
response.Priority = SynResponse.ResponsePriority.High;
toReturn.Data.Response = response;

return toReturn;


In the code above we create a new SynResponse object and manipulate its properties. The Message property is what you wish to display to the user. Speak property is to ensure that the Message is actually spoken. ( Depends on your Speech Synthesizer ). ToSpeak property is the text that is actually meant to be Synthesized. Finally the Priority is a good option to keep in check how messages are displayed.

SynResponse has dozens of properties that can be manipulated to suit the needs of a developer.

So in the above code we are creating a response for a function that actually returns a value but what if your function doesn't return a value ? How will you then send information about function executions ?. That is exactly when the concept of Callers come in handy.

Sample code to return a response from a void function.

CSharp Code

public void AddNumber(MathAddTerm cmd, IntegerData firstNumber, IntegerData secondNumber)
{
int value = firstNumber.Value + secondNumber.Value;
SynResponse response = new SynResponse();
response.Message = "The result of addition is " + value.ToString();
cmd.Data.Caller.Respond(response);
}


In the above code as our function is a void function we generate a response and then send the response to the Caller itself. The Caller is accessed from the Data property of a SynData class and the Respond function is used to send a Response to the Caller.

The response can then be picked by creating a event handler for the ResponseReceived event that is raised by the Caller.
User avatar
Leslie
Lead Software Architect
 
Posts: 287
Joined: Fri Sep 14, 2012 12:20 pm

Return to Archived (Old Posts)

Who is online

Users browsing this forum: No registered users and 1 guest
cron