Hello, World

发送及接收信息和事件是代理与用户间沟通的核心环节。代理通过 Business Messages API 向用户发送信息、事件和请求,但通过网络钩子以 JSON 格式接收用户创建的信息和事件。

下面是一个对话流程示例,它使用示例数据来说明信息、事件和请求如何形成有用且有意义的互动。

在此示例中,用户发起对话并发送“Hello, I'm World!”信息,然后代理回复“Hello, World”。

  1. 用户发起与代理的对话。
  2. 用户开始输入回复后,会向代理发送输入事件。

    {
      "agent": "brands/1111/agents/2222",
      "conversationId": "3333",
      "customAgentId": "hello-world-bot",
      "requestId": "1234567890",
      "userStatus": {
        "isTyping": "true",
        "createTime": "2020-10-02T15:01:23.045123456Z",
      },
      "sendTime": "2020-10-02T15:01:24.045123456Z",
    }
    
  3. 用户发送“Hello, I'm World!”信息。

    {
      "agent": "brands/1111/agents/2222",
      "conversationId": "3333",
      "customAgentId": "hello-world-bot",
      "requestId": "123123123",
      "message": {
        "messageId": "4444",
        "name": "conversations/12345/messages/67890",
        "text": "Hello! I'm World!",
        "createTime": "2020-10-02T15:05:23.045123456Z",
      },
      "context": {
        "entryPoint": "PLACESHEET",
        "userInfo": {
          "displayName": "Michael",
          "userDeviceLocale": "en",
        },
        "resolvedLocale": "en",
      }
      "sendTime": "2020-10-02T15:05:24.045123456Z",
    }
    
  4. 代理发送“Hello, World”信息。

    curl -X POST "https://businessmessages.googleapis.com/v1/conversations/3333/messages" \
    -H "Content-Type: application/json" \
    -H "`oauth2l header --json path/to/service/account/key.json businessmessages`" \
    -d "{
        'messageId': '5555',
        'text': 'Hello, World',
        'representative': {
          'avatarImage': 'https://hello.world/avatar.jpg',
          'displayName': 'Hello World Agent',
          'representativeType': 'BOT'
      }
    }"
    
  5. 信息送达后,用户的设备会返回递送回执。

    {
      "agent": "brands/1111/agents/2222",
      "conversationId": "3333",
      "customAgentId": "hello-world-bot",
      "receipts" : {
        "receipts": [
          {
            "message": "conversations/3333/messages/5555",
            "receiptType": "DELIVERED",
          }
        ],
        "createTime": "2020-10-02T16:01:23.045123456Z",
      },
      "sendTime": "2020-10-02T16:01:24.045123456Z",
    }