if((requestBody.hasOwnProperty('message'))&&(requestBody.message.hasOwnProperty('data'))){// Validate the received hash to ensure the message came from Google RBMletuserEventString=Buffer.from(requestBody.message.data,'base64');lethmac=crypto.createHmac('sha512',CLIENT_TOKEN);letdata=hmac.update(userEventString);letgenHash=data.digest('base64');letheaderHash=req.header('X-Goog-Signature');if(headerHash===genHash){letuserEvent=JSON.parse(userEventString);console.log('userEventString: '+userEventString);handleMessage(userEvent);}else{console.log('hash mismatch - ignoring message');}}res.sendStatus(200);
[null,null,["上次更新時間:2025-04-03 (世界標準時間)。"],[[["\u003cp\u003eA webhook is a URL where the RBM platform sends messages and events via HTTPS POST requests, serving as a secure endpoint for data delivery.\u003c/p\u003e\n"],["\u003cp\u003eYou can configure webhooks at either the partner level, affecting all agents, or at the individual agent level, allowing for distinct behaviors per agent, with agent-level webhooks taking priority.\u003c/p\u003e\n"],["\u003cp\u003eTo ensure message authenticity, it's essential to verify that incoming messages are from Google by comparing the \u003ccode\u003eX-Goog-Signature\u003c/code\u003e header with a generated hash using your webhook's client token.\u003c/p\u003e\n"],["\u003cp\u003eWebhooks should return a \u003ccode\u003e200 OK\u003c/code\u003e response to acknowledge receipt; any other response triggers a retry mechanism that increases the wait time up to 600 seconds and continues for 7 days before dropping the message.\u003c/p\u003e\n"],["\u003cp\u003eWhen implementing agent-level webhooks, be aware that the failure of one webhook can affect message delivery to other webhooks due to the backoff and retry mechanism, emphasizing the need to code for message acceptance and queuing.\u003c/p\u003e\n"]]],[],null,["# Webhooks\n\nA webhook is a partner-specified URL where the RBM platform posts\n[messages](/business-communications/rcs-business-messaging/guides/build/messages/receive)\nand [events](/business-communications/rcs-business-messaging/guides/build/events).\nThis URL acts as an endpoint that receives HTTPS POST requests containing data\nabout the events. This means that data is sent to your application securely over\nHTTPS.\n\nA webhook URL might look something like this:\n`https://[your company name].com/api/rbm-events`.\nOnce you configure your webhook, you can start receiving messages and events.\n\nPartner webhooks and agent webhooks\n-----------------------------------\n\nYou can configure your webhook either at the partner level or at the agent\nlevel.\n\n- Your partner webhook applies to every agent you maintain. If your agents have similar behavior, or if you only have one agent, use the [partner webhook](/business-communications/rcs-business-messaging/guides/get-started/partner-account#configure_your_partner_webhook).\n- Agent webhooks apply to individual agents. If you operate multiple agents with distinct behavior, you can [set a different webhook for each agent](/business-communications/rcs-business-messaging/guides/integrate/webhooks#configure_an_agent_webhook).\n\nIf you've configured both a partner webhook and an agent webhook, the agent\nwebhook takes precedence on its specific agent, while the partner webhook\napplies to any agents that don't have their own webhook.\n\nConfigure an agent webhook\n--------------------------\n\n| **Note:** To create a webhook integration using the RBM Management API, refer to the [Create a webhook integration](/business-communications/rcs-business-messaging/guides/management-api/webhooks#create_a_webhook_integration) documentation.\n\nYou receive messages sent to your agent at your partner webhook. If you want\nmessages for a specific agent to arrive at a different webhook instead, set an\nagent webhook.\n\n1. Open the [Business Communications Developer Console](https://business-communications.cloud.google.com/?utm_source=/business-communications/business-messages/guides/how-to/agents&utm_medium=devsite&utm_campaign=rcs-business-messaging) and sign in with your RBM partner Google Account.\n2. Click your agent.\n3. Click **Integrations**.\n4. For **Webhook** , click **Configure**.\n5. For **Webhook endpoint URL**, enter your webhook URL beginning with \"https://\".\n6. Note your `clientToken` value. You need it to [verify that messages you receive are coming from Google](/business-communications/rcs-business-messaging/guides/integrate/webhooks#verify_incoming_messages).\n7. Configure your webhook to accept a `POST` request with the specified\n `clientToken` parameter and send a `200 OK` response with the plain text value\n of the `secret` parameter as the response body.\n\n For example, if your webhook receives a `POST` request with the following\n body content \n\n {\n \"clientToken\":\"SJENCPGJESMGUFPY\",\n \"secret\":\"1234567890\"\n }\n\n then your webhook should confirm the `clientToken` value and, if\n `clientToken` is correct, return a `200 OK` response with `1234567890` as\n the response body: \n\n // clientToken from Configure\n const myClientToken = \"SJENCPGJESMGUFPY\";\n\n // Example endpoint\n app.post(\"/rbm-webhook\", (req, res) =\u003e {\n const msg = req.body;\n if (msg.clientToken === myClientToken) {\n res.status(200).send(msg.secret);\n return;\n }\n res.send(400);\n });\n\n8. In the Developer Console, click **Verify**. When RBM verifies your webhook,\n the dialog closes.\n\nVerify incoming messages\n------------------------\n\nBecause webhooks can receive messages from any senders, you should verify that\nGoogle sent incoming messages before processing message content.\n\nTo verify that Google sent a message you received, follow these steps:\n\n1. Extract the message's `X-Goog-Signature` header. This is a hashed, base64-encoded copy of the message body payload.\n2. Base-64-decode the RBM payload in the `message.body` element of the request.\n3. Using your webhook's client token (which you specified when you set up your webhook) as a key, create a SHA512 HMAC of the bytes of the base-64 decoded message payload and base64-encode the result.\n4. Compare the `X-Goog-Signature` hash with the hash you created.\n - If the hashes match, you've confirmed that Google sent the message.\n - If the hashes don't match, check your hashing process on a known-good\n message.\n\n If your hashing process is working correctly and you receive a\n message that you believe was fraudulently sent to you,\n [contact us](https://support.google.com/messages/contact/contact_us).\n\n### Node.js\n\n```javascript\n if ((requestBody.hasOwnProperty('message')) && (requestBody.message.hasOwnProperty('data'))) {\n // Validate the received hash to ensure the message came from Google RBM\n let userEventString = Buffer.from(requestBody.message.data, 'base64');\n let hmac = crypto.createHmac('sha512', CLIENT_TOKEN);\n let data = hmac.update(userEventString);\n let genHash = data.digest('base64');\n let headerHash = req.header('X-Goog-Signature');\n\n if (headerHash === genHash) {\n let userEvent = JSON.parse(userEventString);\n\n console.log('userEventString: ' + userEventString);\n handleMessage(userEvent);\n } else {\n console.log('hash mismatch - ignoring message');\n }\n }\n\n res.sendStatus(200);\n \n```\n\nMessage handling\n----------------\n\nReturning anything other than `200 OK` from a webhook is considered a delivery\nfailure.\n\nDevelopers must be mindful that sending messages at high rates will\ngenerate webhook notifications at high rates and must design their code to\nhandle notifications at the expected rate. It is important for developers to\nconsider situations that may cause failure responses - including `500` responses\nfrom their web container, timeouts, or upstream failures. Things to consider\ninclude:\n\n- Verify that your DDoS protections are configured to handle the expected rate of webhook notifications.\n- Confirm that resources such as database connection pools don't run out and produce timeouts or `500` responses.\n\nDevelopers should design their systems so the processing of RBM events occurs\nasynchronously and doesn't prevent the webhook from returning `200 OK`.\n\n\nIt is important to **not** process the RBM event within the webhook itself. Any\nerror or delay during processing may impact the webhook return code:\n\n\n### Behavior on delivery failure\n\nRBM uses a backoff and retry mechanism when it receives a response other than\n`200 OK` from a webhook call. RBM will increase the time it waits between\nretries up to a maximum of 600 seconds. Retries will continue for **7 days**,\nafter which the message will be dropped.\n\n### Implications of agent-level webhooks\n\nRBM queues messages for a partner on one queue. Where a partner is using\nagent-level webhooks, it is important to bear in mind that the failure of one\nwebhook will impact delivery to other webhooks. Webhooks belonging to other\nagents will be called during the backoff period of a failed message.\nHowever, as failed messages queue up for retry, overall delivery rates will fall\nand other agents will be impacted.\n\nIt is important that developers understand this model and code accordingly - as\nfar as possible, accepting messages and queueing them for processing to\nminimize the opportunity of returning a failure.\n\n### Next steps\n\nOnce you configure your webhook, your agent can\n[receive messages](/business-communications/rcs-business-messaging/guides/build/messages/receive)\nfrom your\n[test devices](/business-communications/rcs-business-messaging/guides/build/test).\n[Send a message](/business-communications/rcs-business-messaging/guides/build/messages/send)\nto validate your setup."]]