我的 webhook 充满了来自 whatsapp cloud api v20.0 的大量 read 通知。我想禁用我的 webhook 接收 read 和 delivered 通知。如何做到这一点?
示例已读通知。
{"object":"whatsapp_business_account","entry":[{"id":"xxx","changes":[{"value":{"messaging_product":"whatsapp","metadata":{"display_phone_number":"xxx","phone_number_id":"xxx"},"statuses":[{"id":"wamid.xxx","status":"read","timestamp":"1725094296","recipient_id":"xxx"}]},"field":"messages"}]}]}
要禁用通知,您需要配置 Webhook 以允许过滤更具干扰性的特定类型的通知。
{
"object": "whatsapp_business_account",
"entry": [
{
"id": "xxx",
"changes": [
{
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "xxx",
"phone_number_id": "xxx"
},
"statuses": [
{
"id": "wamid.xxx",
"status": "read",
"timestamp": "1725094296",
"recipient_id": "xxx"
}
]
},
"field": "messages"
}
]
}
]
}
现在在 webhook 中忽略
"read"
和 "delivered"
的通知
// psudo code
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class WebhookHandler {
public static void main(String[] args) throws IOException {
// Simulate getting the incoming request data (this would typically come from an HTTP POST request)
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringBuilder jsonStringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
jsonStringBuilder.append(line);
}
// Convert the incoming JSON string to a JSONObject
String jsonString = jsonStringBuilder.toString();
JSONObject data = new JSONObject(jsonString);
// Check if the notification is a status update
if (data.has("entry")) {
JSONArray entryArray = data.getJSONArray("entry");
JSONObject entry = entryArray.getJSONObject(0);
if (entry.has("changes")) {
JSONArray changesArray = entry.getJSONArray("changes");
JSONObject change = changesArray.getJSONObject(0);
JSONObject value = change.getJSONObject("value");
if (value.has("statuses")) {
JSONArray statusesArray = value.getJSONArray("statuses");
for (int i = 0; i < statusesArray.length(); i++) {
JSONObject status = statusesArray.getJSONObject(i);
String statusType = status.getString("status");
// Check if the status is 'read' or 'delivered'
if ("read".equals(statusType) || "delivered".equals(statusType)) {
// Skip processing this notification
continue;
}
// Process other statuses (e.g., 'sent', 'failed', etc.)
processStatus(status); } } } }
// Continue with your webhook processing for other notifications
}
private static void processStatus(JSONObject status) {
// Your code to handle the status (e.g., 'sent', 'failed', etc.)
System.out.println("Processing status: " + status);
} }