Actions
In OSCOVA, actions are standalone functions that are called when an expression is matched against user input and the resultant intent invokes the action via an action ID. It is usually declared to create an abstraction layer between matched expressions and the target function that is to be executed. All actions are globally available to each and every intent via an action ID.
When you explicitly declare an intent using the IntentAttribute
attribute it is internally mapped to an action that encapsulates the body of the intent method.
Creating an Action
To declare an action in OSCOVA, create a function that takes Context
, Result
or both as parameters and decorate it with the ActionAttribute
and a unique ID.
class AppActionsDialog : Dialog
{
[Action(ID = "display.record.action")]
public void DisplayRecord(Context context, Result result)
{
//Do something here.
}
}
Note
Action ID
must be a globally unique identifier throughout the bot. 2 actions, even in different dialogs, with similar IDs are invalid.
Action Parameters
From version 6.0
, developers can make use of ActionParameters
in Context
to retrieve any data passed (usually by Composer) to the Action call.
public class BrowserWindowDialog : Dialog
{
private static VirtualAssistant Assistant => VirtualAssistant.Instance;
[Action(ID = "browser.open")]
public void Launch(Context context)
{
var websiteUrl = context.ActionParameters["url"].Value;
var openMode = context.ActionParameters["mode"].ValueAs<BrowserOpenType>();
if (openMode == BrowserOpenType.Show)
{
{
var browserWindow = new BrowserWindow();
browserWindow.Browser.Navigate(websiteUrl);
browserWindow.Show();
}
else if (openMode == BrowserOpenType.External)
{
Process.Start(websiteUrl);
}
}
}
}
In the example code above we extract values by specifying the name of the parameters. To retrieve a values as a particular datatype you can use the ValueAs
function and provide a datatype. Oscova can also map string values to enumerations
when specified in ValueAs
method.
Note
When a parameter does not exist the ValueAs
method will return either ActionParameter.Empty
or in case of enumerations, it will return the default enumeration value.
Invoking Actions from Intents
From version 2.9.0
onwards OSCOVA allows invoking of stored actions via their IDs by exposing the OscovaBot.Actions
property. This reduces redundancy of creating the same Expression list for intents present in different contexts.
[Action(ID = "request-coupon")]
public void RequestCouponAction(Context context, Result result)
{
result.SendResponse("Please enter your coupon code.");
context.Add("wait-coupon");
}
//If user says "Yes" that he has a coupon code then call action.
[Expression("@sys.positive")]
[Context("confirm-coupon")]
public void ConfirmCoupon(Context context, Result result)
{
result.Bot.Actions["request-coupon"].Invoke(context, result);
}
Note
Actions can also make calls to other stored actions from within their body.
Referencing an action via SIML
According to the example code below, when the user submits Display all records. the action with the ID display.record.action
gets invoked.
<Siml>
<Dialog Name="action-dialog">
<Intent Name="action-intent">
<Expression Value="Display all records"/>
<Action ID="display.record.action"/>
</Intent>
</Dialog>
</Siml>