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

走远了吗. 提交于 2019-12-11 12:09:28

问题


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.


回答1:


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.

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

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.

Example request body:

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; }



回答2:


The help informs you about one more argument to get the secrets.

PS C:\> az bot directline show --help

Command
    az bot directline show : Get details of the Directline Channel on a bot.

Arguments
    --name -n           [Required] : The resource name of the bot.
    --resource-group -g [Required] : Name of resource group. You can configure the default group
                                     using `az configure --defaults group=<name>`.
    --with-secrets                 : Show secrets in response for the channel.  Allowed values:
                                     false, true.

Global Arguments
    --debug                        : Increase logging verbosity to show all debug logs.
    --help -h                      : Show this help message and exit.
    --output -o                    : Output format.  Allowed values: json, jsonc, table, tsv, yaml.
                                     Default: json.
    --query                        : JMESPath query string. See http://jmespath.org/ for more
                                     information and examples.
    --subscription                 : Name or ID of subscription. You can configure the default
                                     subscription using `az account set -s NAME_OR_ID`.
    --verbose                      : Increase logging verbosity. Use --debug for full debug logs.

With the following command, you can get the secrets of your directline channel:

az bot directline show -n "{botId}" -g "{resourceGroupName}" --with-secrets --subscription "{subscriptionId}"

I tested it, it worked successfully.




回答3:


I had also a look into the Python source code of Azure Cli - BotService here: https://github.com/Azure/azure-cli/blob/dev/src/command_modules/azure-cli-botservice/azure/cli/command_modules/botservice/_client_factory.py. As I saw that they use azure.mgmt.botservice library, I searched for the source code about it and I found this file https://github.com/Azure/azure-sdk-for-python/blob/master/azure-mgmt-botservice/azure/mgmt/botservice/operations/channels_operations.py in which you can find all the possible channels operations.

If you don't want to use Azure Cli commands, you can also get the secret keys of your channel with the following request:

GET https&colon;//management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.BotService/botServices/{botId}/channels/{channelName}/listChannelWithKeys?api-version=2018-07-12

I also tested it and it worked successfully for DirectLine, WebChat, and so on...

.../{channelName}/... parameter should be: .../DirectLineChannel/... or .../WebChatChannel/...

To make it to work, you also need to add an Access Bearer Token into the Authorization key header of the request.



来源:https://stackoverflow.com/questions/51518712/how-can-i-programmatically-obtain-the-directline-secret-of-a-microsoft-bot-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!