如何以编程方式获取 Microsoft Bot Framework 聊天机器人应用程序的 DirectLine 机密?

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

我正在尝试使用 Microsoft Bot FrameworkAzure Bot Service 自动创建和部署聊天机器人应用程序.

我有一个与我的服务对话的自定义模板,我只需要为每个要部署的聊天机器人自定义 Web.config 文件。我还想使用 default.htm 来主持一个使用已部署聊天机器人的 DirectLine 秘密的基本网络聊天。

我能够使用 Azure CLI 2.0 创建 WebApp Chatbot 应用程序,并将该聊天机器人与 DirectLine 频道。但是,我无法使用 Azure CLI 2.0 获取 DirectLine 密钥。

我使用以下说明将我通过 CLI 创建的聊天机器人与 DirectLine 通道集成:

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

然而,当我使用 show 命令时,我没有得到我需要添加到 default.htm 文件中的网络聊天的秘密:

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

我可以使用 Azure CLI.NET SDK 来实现吗?我正在使用 Azure CLI 进行测试,但最后我想使用 .NET SDK 来创建 REST 网络创建聊天机器人(基于我的自定义模板)和 returns URL 给呼叫者的服务。当来电者转到 URL 时,我希望 default.htm 托管 网络聊天。

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

根据我的测试,命令 az bot directline show 会发出以下请求以检索有关直线信道的详细信息。

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

但是keykey2在通过GET.

返回的响应中始终为空

要return/get az bot cli中的keykey2,我们可以使用创建命令:

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

此外,要在.NET 应用程序中管理botservice,您可以尝试使用Microsoft Azure Management Bot Service Library

并且您还可以在您的 .NET 应用程序中使用 Azure 管理 api 来检索 botservice 的直线密钥。以下示例请求供您参考。

示例请求正文:

注:

Microsoft.Azure.Management.BotService.Models.DirectLineSite中,我们可以发现:获取主(副)键。值仅通过 POST 返回到操作频道列表 API,否则为空.

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

帮助告诉你关于获取秘密的另一种说法。

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.

通过以下命令,您可以获得您的直线频道的秘密:

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

我测试了一下,成功了。

我还查看了 Azure Cli - BotService 的 Python 源代码: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 您可以在其中找到所有可能的通道操作。

如果您不想使用 Azure Cli 命令,您也可以通过以下请求获取您频道的密钥:

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

我也对其进行了测试,它成功地用于 DirectLine、WebChat 等...

.../{channelName}/...参数应为:.../DirectLineChannel/...或.../WebChatChannel/...

要使其正常工作,您还需要将访问承载令牌添加到请求的授权密钥 header 中。