RBM 代理通过发布/订阅关系接收消息和事件 Cloud Pub/Sub。 当用户向代理发送消息或生成 event、 他们的即时通讯应用会将信息发送到代理的 Pub/Sub 订阅, 这样您的客服人员就可以访问相应消息或活动请参阅 接收消息。
代理的 Pub/Sub 订阅类型决定了代理接收消息的方式 因此您需要先配置 Pub/Sub 订阅,然后再配置代理 可以接收消息。 RBM 代理同时支持拉取和推送订阅。
拉取订阅
通过拉取订阅,您的代理联系 Cloud Pub/Sub 并 消息、事件和其他请求。
前提条件
开始之前,需要 RBM agent。
设置
- 打开 Business Communications Developer(商业通信开发者) 控制台 使用您的 RBM Google 账号登录,然后点击您的代理。
- 在左侧导航栏中,点击集成。
- 点击修改订阅。
- 选择拉取,然后点击保存。
- 将您的代理配置为使用拉取订阅: <ph type="x-smartling-placeholder">
查找您的项目 ID
某些拉取订阅机制需要您指定代理的 Google
Cloud 项目 (GCP) 项目 ID。您的代理的项目 ID 已嵌入到拉取机制中
订阅名称遵循project/
。
- 打开 Business Communications Developer(商业通信开发者) 控制台 使用您的 RBM Google 账号登录,然后点击您的代理。
- 在左侧导航栏中,点击集成。
- 找到代理的订阅名称。
- 找到
project/
和下一个/
之间的文本段。这是 代理的项目 ID。 例如,如果订阅名称为projects/rbm-growing-tree-bank-nbdjkl6t/subscriptions/rbm-agent-subscription
, 代理的项目 ID 为rbm-growing-tree-bank-nbdjkl6t
。
C#
private async void InitPullMessages(string projectId, string jsonPath) { GoogleCredential googleCredential = null; using (var jsonStream = new FileStream(jsonPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { googleCredential = GoogleCredential.FromStream(jsonStream) .CreateScoped(SubscriberServiceApiClient.DefaultScopes); } SubscriptionName subscriptionName = new SubscriptionName(projectId, subscriptionId); SubscriberClient subscriber = new SubscriberClientBuilder { SubscriptionName = subscriptionName, GoogleCredential = googleCredential }.Build(); // setup listener for pubsub messages await subscriber.StartAsync( async (PubsubMessage message, CancellationToken cancel) => { string text = System.Text.Encoding.UTF8.GetString(message.Data.ToArray()); JObject jsonObject = JObject.Parse(jsonAsString); string userResponse = GetResponseText(jsonObject); string eventType = (string)jsonObject["eventType"]; // check if the message is a user response message if ((userResponse.Length > 0) && (eventType == null)) { string messageId = (string)jsonObject["messageId"]; string msisdn = (string)jsonObject["senderPhoneNumber"]; // let the user know their message has been read kitchenSinkBot.SendReadMessage(messageId, msisdn); HandleUserResponse(userResponse, msisdn); } await Console.Out.WriteLineAsync( $"Message {message.MessageId}: {jsonAsString}"); return SubscriberClient.Reply.Ack; }); } }
推送订阅
通过推送订阅,Cloud Pub/Sub 可推送消息、事件和其他 发送到您指定的网络钩子网址。
前提条件
在开始之前,您需要做好以下准备:
- RBM 代理
- 一个实时 Webhook 端点网址,支持
<ph type="x-smartling-placeholder">
- </ph>
- 具有有效 SSL 证书的 HTTPS
POST
请求- 回显参数以响应验证请求的功能
设置
- 打开 Business Communications Developer(商业通信开发者) 控制台 使用您的 RBM Google 账号登录,然后点击您的代理。
- 在左侧导航栏中,点击集成。
- 点击修改订阅。
- 选择推送。
- 在网络钩子端点网址部分,输入 webhook 的网址,以“https://”开头。
将 webhook 配置为接受具有指定
clientToken
参数的POST
请求,并发送包含secret
参数的值的200 OK
响应。例如,如果您的 webhook 收到包含以下正文的 POST 请求 内容
{ "clientToken":"SJENCPGJESMGUFPY", "secret":"1234567890" }
网络钩子应确认
clientToken
值;如果clientToken
为 正确,会返回正文为secret: 1234567890
的200 OK
响应。在控制台中,点击验证。
RBM 平台验证您的 webhook 后,配置您的 webhook 对话框关闭。
点击保存。
将代理配置为从 webhook 接收消息:
- 如果您使用示例代理 请使用推送订阅,请按照示例 README 文件。
- 如果您不使用示例代理,请配置您的基础架构, 从 webhook 发送到代理的消息。
Node.js
let requestBody = req.body; if ((requestBody.hasOwnProperty('clientToken')) && (requestBody.hasOwnProperty('secret'))) { console.log('RBM webhook verification request'); // Confirm that the clientToken is the one we are seeing in the RBM console if (requestBody.clientToken == CLIENT_TOKEN) { console.log('Tokens match, returning secret'); res.status(200).send('secret: ' + requestBody.secret); } else { // Client tokens did not match - sending permission denied console.log('Tokens do not match'); res.sendStatus(403); } }
验证收到的邮件
由于网络钩子可以接收来自任何发件人的消息,因此您应验证 Google 先发送收到的消息,然后处理消息内容。
要验证 Google 是否向您发送了邮件,请按以下步骤操作:
- 提取邮件的
X-Goog-Signature
标头。这是一个经过哈希处理、 消息正文载荷的 base64 编码副本。 - 对请求的
message.body
元素中的 RBM 载荷进行 Base-64 解码。 - 使用网络钩子的客户端令牌(您在设置 推送订阅)作为密钥,请创建字节的 SHA512 HMAC 并对结果进行 base64 编码。
- 将
X-Goog-Signature
哈希与您创建的哈希进行比较。- 如果哈希值一致,则表示您已确认该邮件是 Google 发送的。
如果哈希值不匹配,请基于已知良好的密钥检查您的哈希处理过程 消息。
如果您的哈希处理过程正常运行,并且收到了 邮件,请联系 us。
Node.js
if ((requestBody.hasOwnProperty('message')) && (requestBody.message.hasOwnProperty('data'))) { // Validate the received hash to ensure the message came from Google RBM let userEventString = Buffer.from(requestBody.message.data, 'base64'); let hmac = crypto.createHmac('sha512', CLIENT_TOKEN); let data = hmac.update(userEventString); let genHash = data.digest('base64'); let headerHash = req.header('X-Goog-Signature'); if (headerHash === genHash) { let userEvent = JSON.parse(userEventString); console.log('userEventString: ' + userEventString); handleMessage(userEvent); } else { console.log('hash mismatch - ignoring message'); } } res.sendStatus(200);
后续步骤
配置订阅并设置代理以与之通信后 Cloud Pub/Sub,您的代理可以接收消息 在测试设备中进行测试。 发送消息 验证您的设置。