Search code examples
botframeworkazure-cliazure-bot-servicedirect-line-botframework

How can I programmatically obtain the DirectLine secret of a Microsoft Bot Framework chatbot application?


I'm trying to automate the process of creation and deploy of chatbot applications using the Microsoft Bot Framework and the Azure Bot Service.

I have a custom template that speaks to a service of mine, and I just need to customize the Web.config file for each chatbot to be deployed. I also want to use the default.htm to host a basic web chat that uses the DirectLine secret of the deployed chatbot.

I was able to create a WebApp Chatbot application using the Azure CLI 2.0, as well as integrate that chatbot with the DirectLine channel. However I wasn't able to get the DirectLine key using the Azure CLI 2.0.

I used the following instructions to integrated the chatbot I created via CLI with the DirectLine channel:

az bot directline create --name
                         --resource-group
                         [--add-disabled {false, true}]
                         [--disablev1 {false, true}]
                         [--disablev3 {false, true}]
                         [--site-name]

However when I use the show command I don't get the secret that I need to add to the web chat in the default.htm file:

az bot directline show --name
                       --resource-group

Can I achieve this using the Azure CLI or the .NET SDK? I'm using the Azure CLI for testing, but in the end I want to go with the .NET SDK in order to create a REST web service that creates the chatbot (based on my custom template) and returns the URL to the caller. When the caller goes to the URL I want the default.htm to be hosting the webchat.


Solution

  • wasn't able to get the DirectLine key using the Azure CLI 2.0

    Based on my test, the command az bot directline show would make the following request to retrieve details about directline channel.

    GET https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resourcegroup_name}/providers/Microsoft.BotService/botServices
    /{bot_id}/channels/DirectLineChannel?api-version=2017-12-01
    

    But the key and key2 always be null in the returned response through GET.

    enter image description here

    To return/get the key and key2 in az bot cli, we can use create command:

    az bot directline create --name MyBotName  --resource-group MyResourceGroup --site-name site2
    

    enter image description here

    Besides, to manage botservice in .NET application, you can try to use Microsoft Azure Management Bot Service Library.

    And you can also use Azure management api in your .NET application to retrieve the directline secret keys of botservice. The following example request is for your reference.

    enter image description here

    Example request body:

    enter image description here

    Note:

    In Microsoft.Azure.Management.BotService.Models.DirectLineSite, we can find: Gets primary (secondary) key. Value only returned through POST to the action Channel List API, otherwise empty.

        //
        // Summary:
        //     Gets primary key. Value only returned through POST to the action Channel List
        //     API, otherwise empty.
        [JsonProperty(PropertyName = "key")]
        public string Key { get; }
        //
        // Summary:
        //     Gets secondary key. Value only returned through POST to the action Channel List
        //     API, otherwise empty.
        [JsonProperty(PropertyName = "key2")]
        public string Key2 { get; }