Autenticare l'utente

Gli agenti in genere hanno pochi approfondimenti sugli utenti con cui comunicano, oltre alle informazioni che forniscono durante una conversazione. Tuttavia, gli agenti possono autenticare gli utenti con OAuth per confermare l'identità dell'utente, personalizzare le conversazioni e compiere azioni per suo conto.

In questa procedura di conversazione, l'utente sceglie di aggiungere crediti al proprio account e l'agente richiede all'utente di accedere per convalidare l'identità dell'utente, recuperare le informazioni note sull'utente ed effettuare un ordine per i crediti.

  1. L'utente inizia la conversazione con l'agente.
  2. Quando l'utente inizia a digitare una risposta, invia un evento di digitazione all'agente.

    {
      "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. L'utente invia "Ciao, vorrei aggiungere 5 crediti al mio account" come messaggio.

    {
      "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. L'agente ti invia il "Posso aiutarti. Accedi al tuo account per continuare. Insieme a un suggerimento di richiesta di autenticazione.

    clientId e scopes sono definiti dal provider OAuth. L'agente genera il valore codeChallenge in base ai requisiti del provider OAuth.

    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. L'utente riceve il messaggio, tocca il suggerimento della richiesta di autenticazione e accede al proprio account.

  6. L'agente riceve un messaggio con il codice di autorizzazione OAuth nel campo authenticationResponse.code.

    {
      "agent": "brands/1111/agents/2222",
      "conversationId": "3333",
      "customAgentId": "oauth",
      "requestId": "1234567895",
      "authenticationResponse": {
        "code": "fkhoe541658sfk684d135j186mngkjdrt5425415a2s1dfa68s4edf",
        },
      }
      "sendTime": "2020-10-02T15:01:24.045123456Z",
    }
    
  7. L'agente comunica con il provider OAuth, scambiando il codice di autorizzazione con un token di accesso e recuperando i dati dell'account dell'utente.

  8. L'agente invia "Grazie per aver eseguito l'accesso. Attualmente hai 2 crediti. Per confermare, vorresti aggiungere 5 crediti al tuo account e fatturare il tuo metodo di pagamento predefinito?" come messaggio con "Sì" e "No" come risposte suggerite.

    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. L'utente tocca la risposta suggerita "Sì".

    {
      "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. L'agente elabora la transazione utilizzando il token di accesso OAuth, quindi invia il messaggio "Bene. Ho appena aggiunto 5 crediti al tuo account. Posso esserle d'aiuto in altro modo?

    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. L'utente invia "No, grazie".

    {
      "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. L'agente invia un "Grazie per averci contattato. Buona giornata."

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