【问题标题】:How can I programmatically obtain the DirectLine secret of a Microsoft Bot Framework chatbot application?如何以编程方式获取 Microsoft Bot Framework 聊天机器人应用程序的 DirectLine 机密?
【发布时间】:2019-01-02 06:17:19
【问题描述】:

我正在尝试使用 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 Web 服务(基于我的自定义模板)并将 URL 返回给调用者。当调用者转到 URL 时,我希望 default.htm 成为 托管 网络聊天。

【问题讨论】:

    标签: botframework azure-cli azure-bot-service direct-line-botframework


    【解决方案1】:

    我还在这里查看了 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。当我看到他们使用 azure.mgmt.botservice 库时,我搜索了有关它的源代码,并找到了这个文件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/...

    要使其正常工作,您还需要在请求的授权密钥标头中添加一个访问承载令牌。

    【讨论】:

    • 使用 GET 请求返回“UnsupportedAction”错误。但是将方法更改为 POST 对我有用……这是我从@Fei Han 的回答中得到的线索。
    【解决方案2】:

    帮助会告知您获取秘密的另一个参数。

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

    我测试过,成功了。

    【讨论】:

      【解决方案3】:

      无法使用 Azure CLI 2.0 获取 DirectLine 密钥

      根据我的测试,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
      

      但是通过GET返回的响应中keykey2总是为空。

      要在 az bot cli 中返回/获取 keykey2,我们可以使用 create 命令:

      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 返回到操作 Channel List 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; }
      

      【讨论】:

      • 谢谢。我真的很感激我的努力,并希望我能不止一次地对此表示赞同,因为我在相当长的时间内都在努力实现这一目标。我真的很感激!
      • 将方法设置为 POST 成功了...谢谢!
      猜你喜欢
      • 1970-01-01
      • 2018-06-23
      • 2020-10-27
      • 1970-01-01
      • 2019-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多