設定 Cloud Pub/Sub

RBM 代理程式會透過 Cloud Pub/Sub 的發布/訂閱關係接收訊息和事件。使用者傳送訊息至您的代理程式或產生事件時,他們的訊息應用程式會將資訊傳送至代理程式的 Pub/Sub 訂閱項目,讓您的代理程式可以存取訊息或事件。請參閱「接收訊息」一文。

使用者傳送訊息給代理程式

代理程式的 Pub/Sub 訂閱類型會決定代理程式接收訊息的方式,因此您必須先設定 Pub/Sub 訂閱項目,代理程式才能接收訊息。RBM 代理程式支援pullpush訂閱。

提取訂閱項目

啟用提取式訂閱項目後,代理程式會與 Cloud Pub/Sub 聯絡,並擷取訊息、事件和其他要求。

必要條件

在開始之前,您需要使用 RBM 代理程式

設定

  1. 開啟 Business Communications Developer Console,使用您的 RBM Google 帳戶登入,然後點選您的代理程式。
  2. 在左側導覽面板中按一下「Integrations」
  3. 按一下「編輯訂閱項目」
  4. 選擇「提取」,然後按一下「儲存」
  5. 將代理程式設定為使用提取訂閱項目:
    • 如果您使用提取訂閱功能的範例代理程式,請按照範例的 README 檔案中的指示操作。
    • 如果未使用範例代理程式,請參閱使用提取功能接收訊息一文中的程式碼,以便讓代理程式使用提取訂閱項目。視程式設計語言而定,您可能需要代理程式的專案 ID

找出專案 ID

部分提取訂閱機制會要求您指定代理程式的 Google Cloud 專案 (GCP) 專案 ID。代理程式的專案 ID 會嵌入 project/ 後方的提取訂閱項目名稱中。

  1. 開啟 Business Communications Developer Console,使用您的 RBM Google 帳戶登入,然後點選您的代理程式。
  2. 在左側導覽面板中按一下「Integrations」
  3. 找出代理程式的訂閱項目名稱
  4. 找出 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;
    });
  }
}
這是 RBM 範例代理程式摘錄的內容。

推送訂閱

使用推送訂閱項目時,Cloud Pub/Sub 會將訊息、事件和其他要求推送至您指定的 Webhook 網址。

必要條件

在開始之前,您必須備妥以下項目:

  • RBM 代理程式
  • 支援
    • 具備有效 SSL 憑證的 HTTPS
    • POST 要求
    • 能夠回應參數以回應驗證要求

設定

  1. 開啟 Business Communications Developer Console,使用您的 RBM Google 帳戶登入,然後點選您的代理程式。
  2. 在左側導覽面板中按一下「Integrations」
  3. 按一下「編輯訂閱項目」
  4. 選擇「推送」
  5. 在「Webhook 端點網址」部分,輸入 Webhook 網址開頭的「https://」。
  6. 將 Webhook 設為接受具有指定 clientToken 參數的 POST 要求,並傳送內含 secret 參數值的 200 OK 回應。

    例如,如果您的 Webhook 收到含有下列主體內容的 POST 要求

    {
      "clientToken":"SJENCPGJESMGUFPY",
      "secret":"1234567890"
    }
    

    Webhook 應確認 clientToken 值,如果 clientToken 正確,就會傳回主體為 secret: 1234567890200 OK 回應。

  7. 在控制台中按一下「驗證」

    當 RBM 平台驗證您的 Webhook 時,「Configure your Webhook」對話方塊會關閉。

  8. 點按「儲存」

  9. 將代理程式設定為接收來自 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);
  }
}

驗證收到的郵件

由於 Webhook 可以接收任何寄件者傳送的訊息,您應在處理訊息內容之前,確認 Google 已傳送傳入的訊息。

如要確認 Google 是否會傳送您收到的訊息,請按照下列步驟操作:

  1. 擷取郵件的 X-Goog-Signature 標頭。這是訊息主體酬載的雜湊 Base64 編碼副本。
  2. 對要求的 message.body 元素中的 RBM 酬載進行 Base-64 解碼。
  3. 使用 Webhook 的用戶端憑證 (您在設定推送訂閱項目時指定) 做為金鑰,建立採用 Base-64 解碼訊息酬載的位元組 SHA512 HMAC,以及使用 Base64 編碼結果。
  4. 比較 X-Goog-Signature 雜湊與您建立的雜湊。
    • 如果雜湊相符,就表示你已確認 Google 傳送訊息。
    • 如果雜湊不相符,請檢查雜湊程序是否含有已知的良好訊息。

      如果雜湊程序運作正常,且收到疑似詐欺性的訊息,請與我們聯絡

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 通訊後,代理程式就可以從您的測試裝置接收訊息傳送訊息來驗證設定。