驗證使用者

除了在對話期間提供資訊外,代理程式在深入瞭解通訊對象時通常幾乎沒有其他相關資訊。不過,代理程式可以使用 OAuth 驗證使用者,藉此確認使用者身分、自訂對話內容,以及代表使用者執行操作。

在這個對話流程中,使用者選擇在帳戶中新增抵免額,代理程式提示使用者登入,以驗證使用者的身分、擷取使用者已知資訊,並訂購抵免額。

  1. 使用者開始與代理程式對話。
  2. 使用者開始輸入回應後,系統會將輸入事件傳送給代理程式。

    {
      "agent": "brands/1111/agents/2222",
      "conversationId": "3333",
      "customAgentId": "oauth",
      "requestId": "1234567890",
      "userStatus": {
        "isTyping": "true",
        "createTime": "2020-10-02T15:01:23.045123456Z",
      },
      "sendTime": "2020-10-02T15:01:24.045123456Z",
    }
    
  3. 使用者傳送「Hello, I'」想要將 5 個抵免額加入我的帳戶中。

    {
      "agent": "brands/1111/agents/2222",
      "conversationId": "3333",
      "customAgentId": "oauth",
      "requestId": "123123123",
      "message": {
        "messageId": "4444",
        "name": "conversations/12345/messages/67890",
        "text": "Hi, I'd like to add 5 credits to my account",
        "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. 服務專員會傳送「我可以協助你」。如要繼續操作,請登入帳戶以繼續。

    clientIdscopes 是由 OAuth 供應商定義。代理程式會根據 OAuth 供應商的要求產生 codeChallenge 值。

    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': 'I can help you with that. Sign into your account to continue.',
      'suggestions': [
        {
          'AuthenticationRequest': {
            'clientId': 'oauth_client_id_1234567890',
            'codeChallenge': 'code_challenge',
            'scopes': [
              'account',
              'billing',
            ],
          },
        },
      ],
      'representative': {
        'avatarImage': 'https://oauth.agent/bot-avatar.jpg',
        'displayName': 'OAuth Agent',
        'representativeType': 'BOT'
      }
    }"
    
  5. 使用者收到訊息、輕觸驗證要求建議並登入自己的帳戶。

  6. 代理程式會在 authenticationResponse.code 欄位中收到含有 OAuth 授權碼的訊息。

    {
      "agent": "brands/1111/agents/2222",
      "conversationId": "3333",
      "customAgentId": "oauth",
      "requestId": "1234567895",
      "authenticationResponse": {
        "code": "fkhoe541658sfk684d135j186mngkjdrt5425415a2s1dfa68s4edf",
        },
      }
      "sendTime": "2020-10-02T15:01:24.045123456Z",
    }
    
  7. 代理程式會與 OAuth 供應商通訊,將存取授權碼交換成存取權杖,並擷取使用者的帳戶資訊。

  8. 代理程式會傳送登入問題。你目前有 2 個抵免額。為了確認,您要求我在帳戶中新增 5 個抵免額,並使用預設付款方式部署項目;以「&是」和「否」表示訊息做為建議回覆。

    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': '7777',
      'text': 'Thanks for signing in. You currently have 2 credits. To confirm, you'd like to me to add 5 credits to your account and bill your default payment method?',
      'suggestions': [
        {
          'reply': {
            'text': 'Yes',
            'postbackData': 'process-transaction',
          },
        },
        {
          'reply': {
            'text': 'No',
            'postbackData': 'cancel-transaction',
          },
        },
      ],
      'representative': {
        'avatarImage': 'https://oauth.agent/bot-avatar.jpg',
        'displayName': 'OAuth Agent',
        'representativeType': 'BOT'
      }
    }"
    
  9. 使用者輕觸「是」建議回覆。

    {
      "agent": "brands/1111/agents/2222",
      "conversationId": "3333",
      "customAgentId": "oauth",
      "requestId": "1234567898",
      "suggestionResponse": {
        "message": "conversations/333/messages/8888",
        "postbackData": "process-transaction",
        "createTime": "2020-10-02T15:01:26.045123456Z",
        "text": "Yes",
        "suggestionType": "REPLY",
      }
      "sendTime": "2020-10-02T15:01:27.045123456Z",
    }
    
  10. 代理程式會使用 OAuth 存取權杖處理交易,然後傳送訊息「Great」。我剛在帳戶中存入了 5 個抵免額。請問還有什麼需要我幫忙的嗎?

    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': '9999',
      'text': 'Great. I just added 5 credits to your account. Is there anything else I can help you with?',
      'representative': {
        'avatarImage': 'https://oauth.agent/bot-avatar.jpg',
        'displayName': 'OAuth Agent',
        'representativeType': 'BOT'
      }
    }"
    
  11. 使用者傳送「不,謝謝」

    {
      "agent": "brands/1111/agents/2222",
      "conversationId": "3333",
      "customAgentId": "oauth",
      "requestId": "123123133",
      "message": {
        "messageId": "4444",
        "name": "conversations/12345/messages/101010",
        "text": "No, thanks",
        "createTime": "2020-10-02T15:05:23.045123456Z",
      },
      "context": {
        "entryPoint": "PLACESHEET",
        "userInfo": {
          "displayName": "Michael",
          "userDeviceLocale": "en",
        },
        "resolvedLocale": "en",
      }
      "sendTime": "2020-10-02T15:05:28.045123456Z",
    }
    
  12. 服務專員已與我們聯絡,感謝您與我們聯絡。祝你有個愉快的一天!

    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': '11111111',
      'text': 'Thanks for contacting us. Have a great day!',
      'representative': {
        'avatarImage': 'https://oauth.agent/bot-avatar.jpg',
        'displayName': 'OAuth Agent',
        'representativeType': 'BOT'
      }
    }"