【问题标题】:CURL POST Request to Bot Framework对 Bot 框架的 CURL POST 请求
【发布时间】:2017-05-05 20:04:37
【问题描述】:

我使用 Microsoft Bot Framework 创建了一个 Bot,并且能够通过模拟器和网络聊天连接到它。我的用户界面中有用户登录,一旦用户登录,我需要将用户名和其他一些详细信息发送到 Bot 端点。这样 Bot 就可以直接从我发布的信息中问候用户并进行聊天。我创建了 CURL 发布请求,但它没有达到那个终点。任何人,请在这里指出我的问题。 CURL POST 请求的示例将很有帮助。 谢谢

【问题讨论】:

    标签: curl botframework


    【解决方案1】:

    如果您想通过帖子请求进行聊天...这是方法。

    1. 通过 post call 获取 conversationId。

      1. 设置 Directline 聊天并复制 Direct Line Secret。
      2. 如下进行后调用

        curl -X POST --header 'Accept: application/json 'https://directline.botframework.com/api/conversations/'

        带有这个标题

        Authorization: BotConnector "Your Direct Line Secret Here"

        响应是一个带有 conversationId 字段的 json。从这里开始的任何对话都需要它

    2. 使用邮寄电话发送消息

      1. 发送带有相同标题的帖子调用,并带有Content-Type: application/json的额外字段

        curl -X POST --header 'Content-Type: application/json' -d '{"text": "Hi"}' 'https://directline.botframework.com/api/conversations/1234/messages'

      2. 正文将包含 api 参考中提到的 json

    查看Direct Line API Reference 了解更多信息。请记住重新生成访问令牌,因为它将在 30 分钟后过期。我仍然更喜欢连接器客户端方法。保留上下文更容易,并且无需重新生成令牌。

    【讨论】:

    • 感谢您的指点。我正在使用网络聊天,但我不会使用 Microsoft 网络聊天 IFRAME。因为你对那里没有适当的控制。我正在寻找的是直接连接 Bot 后端而没有 directline.botframework.comwebchat.botframework.com。 MS Emulator 能够直接命中 Bot Rest End 点。我正在尝试在自定义 Web UI 中复制相同的内容
    【解决方案2】:

    我认为这是不可能的。您只能通过模拟器外的连接器客户端与机器人交谈,除非您设置 Direct Line API,否则无法发送 POST 请求,而后者反过来调用连接器客户端。您可以使用连接器客户端将用户详细信息设置到机器人用户数据中,并在其他地方使用它。这是执行此操作的示例 C# 代码。

    var client = new ConnectorClient(appId, appSecret);
    var getData = await client.Bots.GetUserDataAsync(appId, userid);
    getData.Data = "Your User Details in some form as a string (I'd prefer a serialized JSON)";
    await client.Bots.SetUserDataAsync(Constants.botId, userid, getData);
    

    设置后,您始终可以使用message.botuserdata 访问它。注意:userid可以message.From.Id访问

    【讨论】:

      【解决方案3】:

      我在内部 Web 服务器上部署了一个机器人,并且我有一个客户端应用程序(不是 Web 客户端)非常愉快地使用 REST API 调用与它交谈。

      我不知道如果您使用机器人的 Azure URL(而不是 Bot Framework 中的 URL),同样的概念是否会起作用,但它可能值得一试。如果 Bot Emulator 可以与 Azure 对话,那么任何 REST API 都应该可以工作。

      您需要一个合适的 JSON 字符串作为消息正文发送。我复制了您在 Bot Emulator 中看到的 JSON,并将其用作基础。

          Dim client As RestClient = New RestClient(_localAddress) 'Address of the Bot
          Dim request As RestRequest = New RestRequest("api/messages", Method.POST)
      
          Dim byteArray = System.Text.Encoding.ASCII.GetBytes(_appID & ":" & _appsecret) ' Appropriate application secret and ID
          request.AddHeader("Authorization", "Basic " & Convert.ToBase64String(byteArray))
          'messageDetails is my message class dervived from the Json from the Bot Emulator
          messageDetails.text = messageToRelay
      
          request.AddJsonBody(messageDetails)
          ' response is then deserialised to a suitable BotReply class, again based on the Json you can see in Bot Emulator 
          Dim response = client.Execute(request)
      

      您需要管理相关类中的一些变量以维护消息链,但是通过检查您可以在 Bot Emulator 中看到的 Json 响应,您应该能够将其挑选出来

      我很欣赏答案不是基于 CURL,但希望它可能会有所帮助,您可以根据需要进行调整

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-23
        • 2015-05-13
        • 1970-01-01
        • 2012-08-17
        • 2019-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多