

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用篩選規則篩選 Amazon Chime SDK 訊息的訊息
<a name="filter-msgs"></a>

Amazon Chime SDK 支援在應用程式執行個體使用者的頻道成員資格上設定篩選規則，以限制他們將接收的訊息。篩選條件規則是在頻道成員資格上設定，並根據訊息屬性映射執行。訊息屬性映射必須是字串索引鍵對字串值的映射。篩選條件規則支援包含和排除與確切的字串比對。

**重要**  
Amazon Chime SDK 僅支援逸出的 JSON 字串做為篩選條件規則。
通知管道的所有成員都會收到推播通知，包括訊息寄件者。若要防止這種情況發生，請參閱下列第一個範例規則。

若要設定頻道成員資格的篩選規則，請使用 [https://docs.aws.amazon.com/chime-sdk/latest/APIReference/API_messaging-chime_PutChannelMembershipPreferences.html](https://docs.aws.amazon.com/chime-sdk/latest/APIReference/API_messaging-chime_PutChannelMembershipPreferences.html) API。您可以在頻道訊息中包含訊息屬性，做為 [https://docs.aws.amazon.com/chime-sdk/latest/APIReference/API_messaging-chime_SendChannelMessage.html](https://docs.aws.amazon.com/chime-sdk/latest/APIReference/API_messaging-chime_SendChannelMessage.html) API 呼叫的一部分。

**Topics**
+ [篩選規則類型](#filter-rule-types)
+ [篩選規則限制](#filter-rule-limits)
+ [範例篩選條件規則](#example-preference-rule)

## 篩選規則類型
<a name="filter-rule-types"></a>

Amazon Chime SDK 支援以下類型的篩選規則：
+ 包含確切字串比對
+ 獨佔的確切字串比對
+ 使用 AND 或 OR 的多個篩選規則

## 篩選規則限制
<a name="filter-rule-limits"></a>

Amazon Chime SDK 對篩選規則實施下列限制：
+ 我們僅支援確切的字串比對。
+ 總篩選條件規則大小為 2KB。
+ 訊息屬性大小總計為 1KB。
+ OR 篩選條件規則內最多有五 (5) 個不同的限制條件。
+ 整個篩選規則的最大複雜性為 20。*複雜度*的計算方式為篩選規則中索引鍵和值的數量總和：

  例如，此篩選規則的複雜性為 4。

  ```
  "FilterRule": "{\"type\":[{\"anything-but\": [\"Room\"]}],\"mention\":[\"Bob\"]}
  ```

  我們會計算該值，如下所示：

  ```
  Keys = “type” and “mention” - Complexity 2
  Values = "Room" and "Bob" -   Complexity 2
  
                        Total complexity = 4
  ```

## 範例篩選條件規則
<a name="example-preference-rule"></a>

下列範例顯示數種使用頻道成員偏好設定和篩選規則的方式。

**防止訊息傳送至寄件者**  
此篩選規則會將訊息傳送給訊息寄件者以外的所有頻道成員。

```
{
    "Preferences": {
        "PushNotifications": {
            "FilterRule": "{\"type\":[{\"anything-but\": [\"USER_ARN\"]}]}",
            "AllowNotifications": "FILTERED"
        }
    }
}
```

具有上述偏好設定的應用程式執行個體使用者會收到具有下列屬性的頻道訊息：

```
"MessageAttributes": {
    "senderId": {
        "StringValues": ["USER_ARN"]
    }
}
```

**包含字串比對**  
 此篩選規則允許具有訊息屬性索引鍵「mention」和值「Bob」的任何訊息。

```
{
    "Preferences": {
        "PushNotifications": {
            "FilterRule": "{\"mention\":[\"Bob\"]}",
            "AllowNotifications": "FILTERED"
        }
    }
}
```

具有上述偏好設定的應用程式執行個體使用者會收到具有下列訊息屬性的頻道訊息：

```
"MessageAttributes": {
    "mention": {
        "StringValues": ["Bob", "Alice"]
    }
}
```

不過，應用程式執行個體使用者不會收到具有下列屬性的頻道訊息：

```
"MessageAttributes": {
    "mention": {
        "StringValues": ["Tom"]
    }
}
```

**獨佔字串比對**  
 此篩選規則允許包含屬性索引鍵「類型」和值「房間」以外的任何訊息。

```
{
    "Preferences": {
        "PushNotifications": {
            "FilterRule": "{\"type\":[{\"anything-but\": [\"Room\"]}]}",
            "AllowNotifications": "FILTERED"
        }
    }
}
```

具有這些偏好設定的應用程式執行個體使用者會收到具有下列訊息屬性的頻道訊息：

```
"MessageAttributes": {
    "type": {
        "StringValues": ["Conversation"]
    }
}
```

不過，應用程式執行個體使用者不會看到具有下列屬性的頻道訊息：

```
"MessageAttributes": {
    "type": {
        "StringValues": ["Room"]
    }
}
```

**具有 AND 邏輯的多個篩選條件規則**  
當您將篩選條件規則與 AND 邏輯結合時，訊息必須符合篩選條件套用的所有篩選條件條件。

```
{
    "Preferences": {
        "PushNotifications": {
            "FilterRule": "{\"type\":[{\"anything-but\": [\"Room\"]}],\"mention\":[\"Bob\"]}",
            "AllowNotifications": "FILTERED"
        }
    }
}
```

具有上述偏好設定的應用程式執行個體使用者會收到具有下列訊息屬性的頻道訊息：

```
"MessageAttributes": {
    "mention": {
        "StringValues": ["Bob"]
    },
    "type": {
        "StringValues": ["Conversation"]
    }
}
```

**具有 OR 邏輯的多個篩選條件規則**  
您可以使用 `$or` 將篩選條件規則與 OR 邏輯結合。當您使用 OR 邏輯時，訊息必須符合篩選條件套用的其中一個條件。

```
{
    "Preferences": {
        "PushNotifications": {
            "FilterRule": "{\"$or\":[{\"mention\":[\"Bob\"]},{\"type\":[{\"anything-but\": [\"Room\"]}]}]}",
            "AllowNotifications": "FILTERED"
        }
    }
}
```

具有上述偏好設定的應用程式執行個體使用者會收到具有下列訊息屬性的頻道訊息：

```
"MessageAttributes": {
    "mention": {
        "StringValues": ["Bob"]
    }
}
```

具有上述偏好設定的應用程式執行個體使用者會收到具有下列訊息屬性的頻道訊息：

```
"MessageAttributes": {
    "type": {
        "StringValues": ["Conversation"]
    }
}
```