Link Search Menu Expand Document Documentation Menu

通知 API

如果您想以编程方式定义用于版本控制和重用的通知渠道和源,可以使用通知 REST API 来定义、配置和删除通知渠道并发送测试消息。


目录

  1. 列出支持的渠道配置
  2. 列出所有通知渠道
  3. 列出所有通知配置
  4. 创建渠道配置
  5. 获取渠道配置
  6. 更新渠道配置
  7. 删除渠道配置
  8. 发送测试通知

列出支持的渠道配置

要检索所有支持的通知配置类型列表,请向 features 资源发送 GET 请求。

请求示例

GET /_plugins/_notifications/features

示例响应

{
  "allowed_config_type_list" : [
    "slack",
    "chime",
    "webhook",
    "email",
    "sns",
    "ses_account",
    "smtp_account",
    "email_group"
  ],
  "plugin_features" : {
    "tooltip_support" : "true"
  }
}

列出所有通知渠道

要检索所有通知渠道列表,请向 channels 资源发送 GET 请求。

请求示例

GET /_plugins/_notifications/channels

示例响应

{
  "start_index" : 0,
  "total_hits" : 2,
  "total_hit_relation" : "eq",
  "channel_list" : [
    {
      "config_id" : "sample-id",
      "name" : "Sample Slack Channel",
      "description" : "This is a Slack channel",
      "config_type" : "slack",
      "is_enabled" : true
    },
    {
      "config_id" : "sample-id2",
      "name" : "Test chime channel",
      "description" : "A test chime channel",
      "config_type" : "chime",
      "is_enabled" : true
    }
  ]
}

列出所有通知配置

要检索所有通知配置列表,请向 configs 资源发送 GET 请求。

请求示例

GET _plugins/_notifications/configs

示例响应

{
  "start_index" : 0,
  "total_hits" : 2,
  "total_hit_relation" : "eq",
  "config_list" : [
    {
      "config_id" : "sample-id",
      "last_updated_time_ms" : 1652760532774,
      "created_time_ms" : 1652760532774,
      "config" : {
        "name" : "Sample Slack Channel",
        "description" : "This is a Slack channel",
        "config_type" : "slack",
        "is_enabled" : true,
        "slack" : {
          "url" : "https://sample-slack-webhook"
        }
      }
    },
    {
      "config_id" : "sample-id2",
      "last_updated_time_ms" : 1652760735380,
      "created_time_ms" : 1652760735380,
      "config" : {
        "name" : "Test chime channel",
        "description" : "A test chime channel",
        "config_type" : "chime",
        "is_enabled" : true,
        "chime" : {
          "url" : "https://sample-chime-webhook"
        }
      }
    }
  ]
}

要筛选此请求返回的通知配置类型,您可以使用以下可选路径参数细化查询。

参数 描述
config_id 指定渠道标识符。
config_id_list 指定一个逗号分隔的渠道 ID 列表。
from_index 开始搜索的起始索引。
max_items 请求中返回的最大项目数。
sort_order 指定结果的排序方向。有效选项为 ascdesc
sort_field 用于排序结果的字段。
last_updated_time_ms 渠道上次更新时的 Unix 时间(毫秒)。
created_time_ms 渠道创建时的 Unix 时间(毫秒)。
is_enabled 指示渠道是否已启用。
config_type 渠道类型。有效选项为 snsslackchimewebhooksmtp_accountses_accountemail_groupemail
名称 渠道名称。
description 渠道描述。
email.email_account_id 渠道使用的发件人电子邮件地址。
email.email_group_id_list 渠道使用的电子邮件组。
email.recipient_list 渠道收件人列表。
email_group.recipient_list 渠道电子邮件收件人群组列表。
smtp_account.method 电子邮件加密方法。
slack.url Slack 渠道 URL。
chime.url Amazon Chime 连接 URL。
webhook.url Webhook URL。
smtp_account.host SMTP 账户的域。
smtp_account.from_address 电子邮件账户的发件人地址。
smtp_account.method SMTP 账户的加密方法。
sns.topic_arn Amazon Simple Notification Service (SNS) 主题的 ARN。
sns.role_arn Amazon SNS 主题的角色 ARN。
ses_account.region Amazon Simple Email Service (SES) 账户的 AWS 区域。
ses_account.role_arn Amazon SES 账户的角色 ARN。
ses_account.from_address Amazon SES 账户的发件人电子邮件地址。

创建渠道配置

要创建通知渠道配置,请向 configs 资源发送 POST 请求。

请求示例

POST /_plugins/_notifications/configs/
{
  "config_id": "sample-id",
  "name": "sample-name",
  "config": {
    "name": "Sample Slack Channel",
    "description": "This is a Slack channel",
    "config_type": "slack",
    "is_enabled": true,
    "slack": {
      "url": "https://sample-slack-webhook"
    }
  }
}

创建渠道 API 操作在其请求正文中接受以下字段:

字段 数据类型 描述 必需
config_id 字符串 配置的自定义 ID。
config 对象 包含所有相关信息,如渠道名称、配置类型和插件源。
名称 字符串 渠道名称。
description 字符串 渠道描述。
config_type 字符串 通知的目标。有效选项为 snsslackchimewebhooksmtp_accountses_accountemail_groupemail
is_enabled 布尔型 指示渠道是否启用发送和接收通知。默认为 true

创建渠道操作接受多个 config_types 作为可能的通知目标,因此请按照您首选的 config_type 的格式操作。

"sns": {
  "topic_arn": "<arn>",
  "role_arn": "<arn>" //optional
}
"slack": {
  "url": "https://sample-chime-webhoook"
}
"chime": {
  "url": "https://sample-amazon-chime-webhoook"
}
"webhook": {
      "url": "https://custom-webhook-test-url.com:8888/test-path?params1=value1&params2=value2"
}
"smtp_account": {
  "host": "test-host.com",
  "port": 123,
  "method": "start_tls",
  "from_address": "test@email.com"
}
"ses_account": {
  "region": "us-east-1",
  "role_arn": "arn:aws:iam::012345678912:role/NotificationsSESRole",
  "from_address": "test@email.com"
}
"email_group": { //Email recipient group
  "recipient_list": [
    {
      "recipient": "test-email1@test.com"
    },
    {
      "recipient": "test-email2@test.com"
    }
  ]
}
"email": { //The channel that sends emails
  "email_account_id": "<smtp or ses account config id>",
  "recipient_list": [
    {
      "recipient": "custom.email@test.com"
    }
  ],
  "email_group_id_list": []
}

以下示例演示如何使用电子邮件作为 config_type 创建渠道

POST /_plugins/_notifications/configs/
{
  "id": "sample-email-id",
  "name": "sample-name",
  "config": {
    "name": "Sample Email Channel",
    "description": "Sample email description",
    "config_type": "email",
    "is_enabled": true,
    "email": {
      "email_account_id": "<email_account_id>",
      "recipient_list": [
        "sample@email.com"
      ]
    }
  }
}

示例响应

{
  "config_id" : "<config_id>"
}

获取渠道配置

要通过 config_id 获取渠道配置,请发送 GET 请求并指定 config_id 作为路径参数。

请求示例

GET _plugins/_notifications/configs/<config_id>

示例响应

{
  "start_index" : 0,
  "total_hits" : 1,
  "total_hit_relation" : "eq",
  "config_list" : [
    {
      "config_id" : "sample-id",
      "last_updated_time_ms" : 1652760532774,
      "created_time_ms" : 1652760532774,
      "config" : {
        "name" : "Sample Slack Channel",
        "description" : "This is a Slack channel",
        "config_type" : "slack",
        "is_enabled" : true,
        "slack" : {
          "url" : "https://sample-slack-webhook"
        }
      }
    }
  ]
}

更新渠道配置

要更新渠道配置,请向 configs 资源发送 POST 请求并指定渠道的 config_id 作为路径参数。在请求正文中指定新的配置详细信息。

请求示例

PUT _plugins/_notifications/configs/<config_id>
{
  "config": {
    "name": "Slack Channel",
    "description": "This is an updated channel configuration",
    "config_type": "slack",
    "is_enabled": true,
    "slack": {
      "url": "https://hooks.slack.com/sample-url"
    }
  }
}

示例响应

{
  "config_id" : "<config_id>"
}

删除渠道配置

要删除渠道配置,请向 configs 资源发送 DELETE 请求并指定 config_id 作为路径参数。

请求示例

DELETE /_plugins/_notifications/configs/<config_id>

示例响应

{
  "delete_response_list" : {
  "<config_id>" : "OK"
  }
}

您还可以提交一个逗号分隔的要删除的渠道 ID 列表,OpenSearch 将删除所有指定的通知渠道。

请求示例

DELETE /_plugins/_notifications/configs/?config_id_list=<config_id1>,<config_id2>,<config_id3>...

示例响应

{
  "delete_response_list" : {
  "<config_id1>" : "OK",
  "<config_id2>" : "OK",
  "<config_id3>" : "OK"
  }
}

发送测试通知

要发送测试通知,请向 /feature/test/ 发送 GET 请求并指定渠道配置的 config_id 作为路径参数。

请求示例

GET _plugins/_notifications/feature/test/<config_id>

示例响应

{
  "event_source" : {
    "title" : "Test Message Title-0Jnlh4ABa4TCWn5C5H2G",
    "reference_id" : "0Jnlh4ABa4TCWn5C5H2G",
    "severity" : "info",
    "tags" : [ ]
  },
  "status_list" : [
    {
      "config_id" : "0Jnlh4ABa4TCWn5C5H2G",
      "config_type" : "slack",
      "config_name" : "sample-id",
      "email_recipient_status" : [ ],
      "delivery_status" : {
        "status_code" : "200",
        "status_text" : """<!doctype html>
<html>
<head>
</head>
<body>
<div>
    <h1>Example Domain</h1>
    <p>Sample paragraph.</p>
    <p><a href="sample.example.com">TO BE OR NOT TO BE, THAT IS THE QUESTION</a></p>
</div>
</body>
</html>
"""
      }
    }
  ]
}