How to: Write and Consume a Custom Action (C#) - microsoft/botframework-components GitHub Wiki
You can use custom actions in order to extend your bot with code in Composer. At a high level, these are the steps to creating and consuming a custom action in your Composer bot:
- Create a custom action
- Create the .schema file for your action
- Register your action as a BotComponent
- Reference your library in your Composer project
- Run dialog:merge to add your action to the Composer schema
- Access your action from within Composer
1. Create a custom action
- Create a ClassLibrary and add a new class that inherits from IDialog
- Add a Kind field and assign your custom action a type
- Add your properties
- Add a ResultProperty to store the result of your custom action
- Add any additional input properties
- Implement the OnBeginDialogAsync method
2. Create the .schema file for your action
- Add a .schema file with the same name as your Kind field in step 1a.
- Your schema should follow the following structure:
{
"$schema": "https://schemas.botframework.com/schemas/component/v1.0/component.schema",
"$role": "implements(Microsoft.IDialog)",
"type": "object",
"title": "<Your action title>",
"description": "<Your action description>",
"required": [
<your required properties>
],
"properties": {
"id": {
"type": "string",
"title": "Id",
"description": "Optional id for the dialog"
},
"resultProperty": {
"$ref": "schema:#/definitions/stringExpression",
"title": "Result property",
"description": "",
"examples": [
]
},
<Your additional properties>
}
}
3. Register your action as a BotComponent
- In your custom action project, create a class that inherits from BotComponent
- Implement the ConfigureServices method and add the following code for each custom action you are registering as follows:
public class MyBotComponent : BotComponent
{
public override void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
services.AddSingleton<DeclarativeType>((sp) => new DeclarativeType<MyCustomAction>(MyCustomAction.Kind));
}
}
4. Reference your library in your Composer project
- Add a project reference to your Custom Action library in your Composer Bot project file using your code editor of choice
5. Run dialog:merge to add your action to the Composer schema
- In your Composer Bot project folder, run the following script
.\schemas\update-schema.ps1
6. Access your action from within Composer
- Open your bot in Composer
- In the Add Action menu you should now see a tab labelled "Custom Actions"
- Verify your action is visible and add it to your project
- Provide the necessary inputs
- Happy developing :)