收發訊息和事件是代理程式與使用者之間的通訊核心。代理程式會透過 Business Messages API 傳送訊息、事件和要求給使用者,但會在 JSON 中接收使用者建立的訊息和事件 (JSON)。
以下對話流程範例使用範例資料,說明訊息、事件和要求如何產生實用且有意義的互動。
在這個範例中,使用者開始對話,並傳送「Hello, I'm World!」訊息,代理程式會回應「Hello, World」。
- 使用者開始與代理程式對話。
使用者開始輸入回應後,系統會將輸入事件傳送給代理程式。
{ "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", }
使用者傳送「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", }
服務專員以訊息形式傳送「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' } }"
郵件送達後,使用者的裝置就會傳回傳送回條。
{ "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", }