Intents
In Oscova an intent is any action that the bot is supposed to execute when the user message matches an expression. An intent is a void
method with a public
modifier. The name of the intent is taken directly from the method name.
Declaration
[Expression("How is the weather?")]
public void Weather(Context context, Result result)
{
//Do something.
}
An intent method must always be a member of a class that derives from the Dialog
class and must have atleast 1 Expression attribute attached to it. By convention an Intent must take 2 parameters. The first being Context
and the second being the Result
.
Parameter | Description |
---|---|
Context | The Context provides information regarding the current user conversation context and also enables adding and removing context and relative parameters. Apart from that the Context instance also has a SharedData property for the developer to store and retrieve shared objects throughout a user session. |
Result | The Result is the evaluated result that Oscova generated for this particular user message. Its Entities property contains all the recognized global and parametric entities. Its CurrentIntent property represents the current intent that is being invoked. |
Parameter Sequence
Developers are not restricted to specifying 2 parameters in all intents. All of the following method signatures are valid intents. During invocation, Oscova will pass the right arguments to the intent method.
[Expression("How is the weather?")]
public void Weather()
[Expression("How is the weather?")]
public void Weather(Context context)
[Expression("How is the weather?")]
public void Weather(Result result)
[Expression("How is the weather?")]
public void Weather(Context context, Result result)
Name
By default the name of an Intent is the combination of the name of the dialog and the method name. For example, the name of an intent with the method name FindHotel present within the HotelDialog class will be HotelDialog.FindHotel. If you do not wish to include the dialog name in intent names, you may set the value of UseFullIntentName
to false
in bot configuration.
Moreover, if you wish to add a custom name to an intent you may use the Intent
attribute as shown below.
[Expression("Find a hotel nearby")]
[Intent(Name="nearestHotel")]
public void FindHotel(Context context, Result result)
As an Intent
attribute has been used, the name of the above intent during serialization will be nearestHotel
.
Note
Value of the UseFullIntentName
property in bot configuration gets ignored if a developer explicitly specifies an intent name using the Intent
attribute.
Default Intent
A Default
intent is an intent that is invoked if the user input doesn't match any existing regular intent. If none of the added Dialogs have a default Intent, Oscova adds an internal default intent with no action.
To create a Default intent for a Dialog. Create an Intent and decorate it with an empty Fallback
attribute as shown below.
[Fallback]
public void DefaultFallback(Context context, Result result)
{
result.Send("Can you please rephrase that for me?");
}
Note
Only 1 default intent can be added to Oscova.
Force minimum Intent score
Sometimes certain intents may be required to have higher accuracy before certain critical actions are executed on intent invocation. To handle such scenarios OSCOVA offers the MinScore
property on IntentAttribute
.
[Expression("@sys.positive")]
[Intent(MinScore = 0.7)]
public void CommitToDatabase(Context context, Result result)
{
//User said "Yes". Do something essential here.
}
In the code above the intent CommitToDatabase
will only be part of EvaluationResult
if and only if the intent score is 0.7
or above.
Note
Specifying a MinScore
has a global effect and the minimum score applies even when a new implementation of IScoreEngine
is provided. The value of MinScore
must always be greater than the value specified in OscovaBot.Configuration.Scoring.MinimumScore
property.
Explicit Intent Disabling
In rare cases developers may want to ensure that certain intents are disabled for particular users after an intent is invoked. For this each users has an DisabledIntents
property to disable specified intents permanently or for a particular number of user requests.
[Trait(Type = "#feedback")]
public void Feedback(Context context, Result result)
{
//Do something and then...
//Disable the specified intent for the next 2 requests.
result.User.DisabledIntents.Add("IntentA", 2);
//Disable the specified intent permanently for this user.
result.User.DisabledIntents.Add("IntentB");
}