Google Business Messages 功能將於 2024 年 7 月 31 日終止服務。請按
這裡瞭解詳情。
驗證使用者
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
但代理程式不瞭解與使用者互動的使用者
擷取使用者在對話中提供的資訊不過,服務專員可以
使用 OAuth 驗證使用者
確認使用者身分、打造個人化對話,以及代表使用者執行動作
使用者數量。
在這個對話流程中,使用者選擇將抵免額存入自己的帳戶,且
代理程式會提示使用者登入以驗證使用者身分,擷取
並輸入抵免額。

- 使用者開始和代理程式對話。
使用者開始輸入回覆後,系統會將輸入事件傳送到
代理程式。
{
"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",
}
使用者傳送:「嗨,我想將 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",
}
服務專員會傳送「我可以協助你解決問題,登入帳戶即可
繼續回答。」以及驗證要求建議。
clientId
和 scopes
是由 OAuth 供應商定義。虛擬服務專員
系統會根據 OAuth 供應商的codeChallenge
Google Cloud 就是最佳選擇
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'
}
}"
使用者收到訊息後輕觸驗證要求建議,
並登入帳戶
代理程式會收到訊息,其中包含
「authenticationResponse.code
」欄位。
{
"agent": "brands/1111/agents/2222",
"conversationId": "3333",
"customAgentId": "oauth",
"requestId": "1234567895",
"authenticationResponse": {
"code": "fkhoe541658sfk684d135j186mngkjdrt5425415a2s1dfa68s4edf",
},
}
"sendTime": "2020-10-02T15:01:24.045123456Z",
}
代理程式會與 OAuth 供應商通訊,交換授權
存取權杖並擷取使用者的帳戶資訊。
服務專員傳送「感謝您登入。你目前有 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'
}
}"
使用者輕觸「是」建議回覆。
{
"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",
}
代理程式會使用 OAuth 存取權杖處理交易,然後傳送
「太好了。我們剛剛在帳戶中存入了 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'
}
}"
使用者傳送「不用了,謝謝」。
{
"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",
}
服務專員傳送「感謝您與我們聯絡。祝你有美好的一天!」
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'
}
}"
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-09-10 (世界標準時間)。
[null,null,["上次更新時間:2024-09-10 (世界標準時間)。"],[[["\u003cp\u003eBusiness Messages agents can authenticate users with OAuth to verify their identity and access account information.\u003c/p\u003e\n"],["\u003cp\u003eOAuth authentication allows agents to personalize conversations and perform actions on behalf of users, such as processing transactions.\u003c/p\u003e\n"],["\u003cp\u003eIn this example, a user initiates a credit purchase, the agent requests authentication, and upon successful login, the agent completes the transaction.\u003c/p\u003e\n"],["\u003cp\u003eAgents leverage the OAuth authorization code to obtain an access token and interact with the user's account securely.\u003c/p\u003e\n"]]],[],null,["# Authenticate the user\n\nAgents typically have little insight into the users they communicate with aside\nfrom information users give during a conversation. However, agents can\n[authenticate users with OAuth](/business-communications/business-messages/guides/how-to/integrate/oauth)\nto confirm user identity, personalize conversations, and take actions on behalf\nof users.\n\nIn this conversation flow, the user chooses to add credits to their account, and\nthe agent prompts the user to sign in to validate the user's identity, fetch\nknown information about the user, and place an order for the credits.\n\n1. The user begins the conversation with the agent.\n2. Once the user begins typing a response, they send a typing event to the\n agent.\n\n {\n \"agent\": \"brands/1111/agents/2222\",\n \"conversationId\": \"3333\",\n \"customAgentId\": \"oauth\",\n \"requestId\": \"1234567890\",\n \"userStatus\": {\n \"isTyping\": \"true\",\n \"createTime\": \"2020-10-02T15:01:23.045123456Z\",\n },\n \"sendTime\": \"2020-10-02T15:01:24.045123456Z\",\n }\n\n3. The user sends \"Hi, I'd like to add 5 credits to my account\" as a message.\n\n {\n \"agent\": \"brands/1111/agents/2222\",\n \"conversationId\": \"3333\",\n \"customAgentId\": \"oauth\",\n \"requestId\": \"123123123\",\n \"message\": {\n \"messageId\": \"4444\",\n \"name\": \"conversations/12345/messages/67890\",\n \"text\": \"Hi, I'd like to add 5 credits to my account\",\n \"createTime\": \"2020-10-02T15:05:23.045123456Z\",\n },\n \"context\": {\n \"entryPoint\": \"PLACESHEET\",\n \"userInfo\": {\n \"displayName\": \"Michael\",\n \"userDeviceLocale\": \"en\",\n },\n \"resolvedLocale\": \"en\",\n }\n \"sendTime\": \"2020-10-02T15:05:24.045123456Z\",\n }\n\n4. The agent sends \"I can help you with that. Sign into your account to\n continue.\" along with an Authentication request suggestion.\n\n `clientId` and `scopes` are defined by the OAuth provider. The agent\n generates the `codeChallenge` value according to the OAuth provider's\n requirements. \n\n curl -X POST \"https://businessmessages.googleapis.com/v1/conversations/3333/messages\" \\\n -H \"Content-Type: application/json\" \\\n -H \"`oauth2l header --json path/to/service/account/key.json businessmessages`\" \\\n -d \"{\n 'messageId': '5555',\n 'text': 'I can help you with that. Sign into your account to continue.',\n 'suggestions': [\n {\n 'AuthenticationRequest': {\n 'clientId': 'oauth_client_id_1234567890',\n 'codeChallenge': 'code_challenge',\n 'scopes': [\n 'account',\n 'billing',\n ],\n },\n },\n ],\n 'representative': {\n 'avatarImage': 'https://oauth.agent/bot-avatar.jpg',\n 'displayName': 'OAuth Agent',\n 'representativeType': 'BOT'\n }\n }\"\n\n5. The user receives the message, taps the authentication request suggestion,\n and signs into their account.\n\n6. The agent receives a message with the OAuth authorization code in the\n `authenticationResponse.code` field.\n\n {\n \"agent\": \"brands/1111/agents/2222\",\n \"conversationId\": \"3333\",\n \"customAgentId\": \"oauth\",\n \"requestId\": \"1234567895\",\n \"authenticationResponse\": {\n \"code\": \"fkhoe541658sfk684d135j186mngkjdrt5425415a2s1dfa68s4edf\",\n },\n }\n \"sendTime\": \"2020-10-02T15:01:24.045123456Z\",\n }\n\n7. The agent communicates with the OAuth provider, exchanging the authorization\n code for an access token and fetching the user's account information.\n\n8. The agent sends \"Thanks for signing in. You currently have 2 credits. To\n confirm, you'd like me to add 5 credits to your account and bill your\n default payment method?\" as a message with \"Yes\" and \"No\" as suggested replies.\n\n curl -X POST \"https://businessmessages.googleapis.com/v1/conversations/3333/messages\" \\\n -H \"Content-Type: application/json\" \\\n -H \"`oauth2l header --json path/to/service/account/key.json businessmessages`\" \\\n -d \"{\n 'messageId': '7777',\n '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?',\n 'suggestions': [\n {\n 'reply': {\n 'text': 'Yes',\n 'postbackData': 'process-transaction',\n },\n },\n {\n 'reply': {\n 'text': 'No',\n 'postbackData': 'cancel-transaction',\n },\n },\n ],\n 'representative': {\n 'avatarImage': 'https://oauth.agent/bot-avatar.jpg',\n 'displayName': 'OAuth Agent',\n 'representativeType': 'BOT'\n }\n }\"\n\n9. The user taps the \"Yes\" suggested reply.\n\n {\n \"agent\": \"brands/1111/agents/2222\",\n \"conversationId\": \"3333\",\n \"customAgentId\": \"oauth\",\n \"requestId\": \"1234567898\",\n \"suggestionResponse\": {\n \"message\": \"conversations/333/messages/8888\",\n \"postbackData\": \"process-transaction\",\n \"createTime\": \"2020-10-02T15:01:26.045123456Z\",\n \"text\": \"Yes\",\n \"suggestionType\": \"REPLY\",\n }\n \"sendTime\": \"2020-10-02T15:01:27.045123456Z\",\n }\n\n10. The agent processes the transaction using the OAuth access token, then sends\n the message \"Great. I just added 5 credits to your account. Is there\n anything else I can help you with?\".\n\n curl -X POST \"https://businessmessages.googleapis.com/v1/conversations/3333/messages\" \\\n -H \"Content-Type: application/json\" \\\n -H \"`oauth2l header --json path/to/service/account/key.json businessmessages`\" \\\n -d \"{\n 'messageId': '9999',\n 'text': 'Great. I just added 5 credits to your account. Is there anything else I can help you with?',\n 'representative': {\n 'avatarImage': 'https://oauth.agent/bot-avatar.jpg',\n 'displayName': 'OAuth Agent',\n 'representativeType': 'BOT'\n }\n }\"\n\n11. The user sends \"No, thanks\".\n\n {\n \"agent\": \"brands/1111/agents/2222\",\n \"conversationId\": \"3333\",\n \"customAgentId\": \"oauth\",\n \"requestId\": \"123123133\",\n \"message\": {\n \"messageId\": \"4444\",\n \"name\": \"conversations/12345/messages/101010\",\n \"text\": \"No, thanks\",\n \"createTime\": \"2020-10-02T15:05:23.045123456Z\",\n },\n \"context\": {\n \"entryPoint\": \"PLACESHEET\",\n \"userInfo\": {\n \"displayName\": \"Michael\",\n \"userDeviceLocale\": \"en\",\n },\n \"resolvedLocale\": \"en\",\n }\n \"sendTime\": \"2020-10-02T15:05:28.045123456Z\",\n }\n\n12. The agent sends \"Thanks for contacting us. Have a great day!\"\n\n curl -X POST \"https://businessmessages.googleapis.com/v1/conversations/3333/messages\" \\\n -H \"Content-Type: application/json\" \\\n -H \"`oauth2l header --json path/to/service/account/key.json businessmessages`\" \\\n -d \"{\n 'messageId': '11111111',\n 'text': 'Thanks for contacting us. Have a great day!',\n 'representative': {\n 'avatarImage': 'https://oauth.agent/bot-avatar.jpg',\n 'displayName': 'OAuth Agent',\n 'representativeType': 'BOT'\n }\n }\""]]