Skip to content

How to: Write and Consume a Custom Action (C#)

lauren-mills edited this page Apr 23, 2021 · 1 revision

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:

  1. Create a custom action
  2. Create the .schema file for your action
  3. Register your action as a BotComponent
  4. Reference your library in your Composer project
  5. Run dialog:merge to add your action to the Composer schema
  6. Access your action from within Composer

1. Create a custom action

  1. Create a ClassLibrary and add a new class that inherits from IDialog
  2. Add a Kind field and assign your custom action a type
  3. Add your properties
    • Add a ResultProperty to store the result of your custom action
    • Add any additional input properties
  4. Implement the OnBeginDialogAsync method

2. Create the .schema file for your action

  1. Add a .schema file with the same name as your Kind field in step 1a.
  2. 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

  1. In your custom action project, create a class that inherits from BotComponent
  2. 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

  1. 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

  1. In your Composer Bot project folder, run the following script .\schemas\update-schema.ps1

6. Access your action from within Composer

  1. Open your bot in Composer
  2. In the Add Action menu you should now see a tab labelled "Custom Actions"
  3. Verify your action is visible and add it to your project
  4. Provide the necessary inputs
  5. Happy developing :)