WordPress Salient パンくずリストの導入

パンくずリスト、好きですか?

ウェブサイトによくある
「TOP > すごいカテゴリー > イケてる記事」
みたいな階層構造を示しているあれです。便利ですね。

僕は特に好きでも嫌いでもないです。

人が見て記事のカテゴリーなどがわかりやすいだけでなく、検索エンジンがサイトをクローリングするときにも都合がよくてSEO的にも良いらしいです。

WordPressサイトにパンくずリストを入れたときの業務メモみたいなものを記事にしました。

プラグイン選定

パンくずリストを実現するプラグインは、「breadcrumb」で検索すると無数に出てきます。

今回の扱うWordPressサイトは、テーマに「Salient」を使っていたのですが、導入してもテーマとの相性が悪く動かない(表示されない)プラグインなどもありました。

プラグインの設定画面ぽちぽちするだけで使えそうな良さげなやつとして、「Yoast SEO」というものを見つけました。

懸念があったので恐る恐るインストール→有効化したのですが、案の定「All In One SEO」と競合している旨の通知がぴょこぴょこと出てきました。
ファイルの編集を行うことなく自動的にパンくずリストを出してくれるため、大変便利なプラグインでしたが、SEO関連で意図しない誤動作や面倒な副作用が発生すると困るため、やむなく不採用となりました。

仕方がないので、人気の高い別のプラグインである「Breadcrumb NavXT」を使うことにしました。

後述する理由から、ショートコードやスニペットを埋め込む形式はメンテナンス面でかなり面倒な気配がするので避けたかったのですが……仕方ないということにします。

プラグインの設定

「Breadcrumb NavXT」は、WordPressのheader.phpにスニペットを追記して使うものでしたが、記載しても画面内に良い感じにパンくずリストは出ませんでした。

Yoastの設定解説を見ると、single.phpやpage.phpにスニペットを書き込むことでパンくずリストを表示できそうなことが書いてあります。
(不採用のプラグインでもリファレンスは活用していく)

パンくずの区切り文字を変更

デフォルトは「 > 」(全角で書いていますが実際は半角です)でしたが、間隔を自由に調整したいので下記のように変更しました。

 > 
<span style="margin: 0px 7px 0px 7px">></span>

テンプレートの編集

デフォルトではサイトタイトルなどが出たり、マウスオーバーでツールチップが表示されたりするので調整。

  • ホームページテンプレート
    • %htitle%TOP に表示を変更
    • ツールチップの「Go to ~~」が不要だったのでtitle要素を削除
  • 他のテンプレート
    • ツールチップの「Go to ~~」が不要だったのでtitle要素を削除
  • カテゴリー・タグのテンプレート
    • ツールチップの文言を適当な日本語に調整

テンプレートの変更内容詳細は記事の最後に記載しました。長いので。

テーマのファイルを編集

Salient: single.php

「投稿」用です。

nectar_hook_before_content(); の前にスニペットを追記しました。

(略)
<div class="row">

	<?php

	nectar_hook_before_content();
(略)
(略)
<div class="row">

	<p class="breadcrumbs" typeof="BreadcrumbList" vocab="https://schema.org/">
	    <?php if(function_exists('bcn_display')) { bcn_display(); }?>
	</p>

	<?php

	nectar_hook_before_content();
(略)

Salient: page.php

「固定ページ」用です。

nectar_hook_before_content(); の前にスニペットを追記しました。
TOPではパンくずリスト部分を出したくないため、スニペットに少し手を加えました。

(略)
<div class="row">

	<?php

	nectar_hook_before_content();
(略)
(略)
<div class="row">
	
	<?php

	if(!is_front_page()) {
		echo '<p class="breadcrumbs" typeof="BreadcrumbList" vocab="https://schema.org/" style="padding-top:20px">'; 
	    if(function_exists('bcn_display')) { bcn_display(); }
		echo '</p>'; 
	}

	nectar_hook_before_content();
(略)

こんな感じでとりあえず「投稿」と「固定ページ」にパンくずリストを表示できました。

single.phpやpage.phpの編集ではうまくいかない場合

表示位置が意図した場所にならず困ってしまう場合があります。
今回は固定ページで発生したので、解決法も記載しておきます。

メンテコストとかの話でまた後々面倒くさくなりそうですが、下記の記事を参考に対処しました。
参考: パンくずリストプラグイン【Breadcrumb NavXT】をショートコードで呼び出す方法 | WordPressカスタマイズ事例【100ウェブ】

Salient: functions.php

末尾に下記のように追記しました。

/**
 * For Breadcrumb NavXT. (2021/06/24 niwa)
 */
if (!function_exists('display_breadcrumb')) {
    function display_breadcrumb() {
        $html = '<div class="breadcrumbs">'. bcn_display(true). '</div>';
        return $html;
    }
    add_shortcode('display_breadcrumb', 'display_breadcrumb');
}

各固定ページ

上記の変更により、[display_breadcrumb]というショートコードでパンくずリストの表示ができるようになりました。
固定ページを編集して、テキストブロックを追加してショートコードを記述。任意の位置に設置するだけでパンくずリストが表示されます。

WPBakery Page Builderだとこんな感じ

ゴリ押し感~~

🧍‍♀️今回はここまで🧍‍♀️

付録: Breadcrumb NavXT 設定 – テンプレートの変更内容詳細

HTML特殊記号の類(>とか’とか)をエスケープして記載しているのですが、普通に記号に変換されて表示されている様子……

一般 – ホームページテンプレート

<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="Go to %title%." href="%link%" class="%type%" bcn-aria-current><span property="name">%htitle%</span></a><meta property="position" content="%position%"></span>
<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" href="%link%" class="%type%" bcn-aria-current><span property="name">TOP</span></a><meta property="position" content="%position%"></span>

投稿タイプ – 投稿テンプレート, 固定ページ ページテンプレート, メディアテンプレート

※3項目とも同じ内容

<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="Go to %title%." href="%link%" class="%type%" bcn-aria-current><span property="name">%htitle%</span></a><meta property="position" content="%position%"></span>
<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" href="%link%" class="%type%" bcn-aria-current><span property="name">%htitle%</span></a><meta property="position" content="%position%"></span>

タクソノミー – カテゴリーテンプレート

<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="Go to the %title% category archives." href="%link%" class="%type%" bcn-aria-current><span property="name">%htitle%</span></a><meta property="position" content="%position%"></span>
<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="カテゴリー: %title%" href="%link%" class="%type%" bcn-aria-current><span property="name">%htitle%</span></a><meta property="position" content="%position%"></span>

タクソノミー – タグテンプレート

<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="Go to the %title% tag archives." href="%link%" class="%type%" bcn-aria-current><span property="name">%htitle%</span></a><meta property="position" content="%position%"></span>
<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="タグ: %title%" href="%link%" class="%type%" bcn-aria-current><span property="name">%htitle%</span></a><meta property="position" content="%position%"></span>

タクソノミー – 投稿フォーマットテンプレート

<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="Go to the %title% archives." href="%link%" class="%type%" bcn-aria-current><span property="name">%htitle%</span></a><meta property="position" content="%position%"></span>
<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="投稿: %title%" href="%link%" class="%type%" bcn-aria-current><span property="name">%htitle%</span></a><meta property="position" content="%position%"></span>

その他 – 投稿者テンプレート

<span property="itemListElement" typeof="ListItem"><span property="name"><a title="Go to the first page of posts by %title%" href="%link%" class="%type%" bcn-aria-current>%htitle%</a> の記事</span><meta property="position" content="%position%"></span>
<span property="itemListElement" typeof="ListItem"><span property="name"><a title="%title% の記事" href="%link%" class="%type%" bcn-aria-current>%htitle%</a> の記事</span><meta property="position" content="%position%"></span>

その他 – 日付テンプレート

<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="Go to the %title% archives." href="%link%" class="%type%" bcn-aria-current><span property="name">%htitle%</span></a><meta property="position" content="%position%"></span>
<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="%title%" href="%link%" class="%type%" bcn-aria-current><span property="name">%htitle%</span></a><meta property="position" content="%position%"></span>

その他 – 検索テンプレート

<span property="itemListElement" typeof="ListItem"><span property="name">Search results for '<a property="item" typeof="WebPage" title="Go to the first page of search results for %title%." href="%link%" class="%type%" bcn-aria-current>%htitle%</a>'</span><meta property="position" content="%position%"></span>
<span property="itemListElement" typeof="ListItem"><span property="name">'<a property="item" typeof="WebPage" title="%title% の検索結果" href="%link%" class="%type%" bcn-aria-current>%htitle%</a>' の検索結果</span><meta property="position" content="%position%"></span>

Redmineにプラグインを入れる!

前置き

突然ですが、Redmineのガントチャートって激見づらくないですか?
そんな事象を解決するために、プラグインで解決しようじゃありませんか…


環境

LightSailのbitnamiを使ってRedmineが動いています。
     ↓こんな感じで動いてます↓


プラグインを入れる

①プラグインフォルダに移動してプラグインを入れる

cd ~/apps/redmine/htdocs/plugins

ここで、git clone なりでプラグインを引っ張ってきます。

②いったんhtdocsに戻ります

cd ../

③必要なものをインストールします

bundle install --no-deployment

④変更をマイグレーションします

bundle exec rake redmine:plugins:migrate RAILS_ENV=production

⑤再起動します

sudo /opt/bitnami/ctlscript.sh restart

■もしかすると、③を行った際に、mimemagicが~と怒られるかもしれませんが、安心してください。下記コマンドを実行すればおそらく解決します。

sudo apt install shared-mime-info

mimemagicの箇所でかなり手こずりました💦

Lambdaでもpipがしたい!

Lambdaでもpipしたいですよね….いろいろ面倒だし…..

やり方

①ローカルの作業ディレクトリにpipして使いたいものをためておきます。
  サンプルとして、pymsteamsを入れる形です。

pip3 install pymsteams -t .

 そうすると、作業ディレクトリにこんな感じでぶわっとファイルが量産されま  す↓

②それらをzipします。upload.zipだったりの名前にしておきます。

③作成したupload.zipをLambdaのアップロードでzip形式を選択後、該当ファイルをアップロード(既にアップロードされている状態….💦)

これでひとまず使えるようになりました…

レイヤーとかに追加してあげると、後で使うときに便利になるっぽいです。

AWS SESの送信クォータを監視する

AWSのSESには、送信クォータや送信レートという、アカウントごとに一定期間に送信できるメール数の上限が定められています。

送信クォータ
24時間のうちに何通のメールを送信できるか。上限を超えた場合には、送信クォータの上限に達した旨のエラーとなりメールが送信されなくなる(再送処理等はない)。

送信レート
SESが1秒間に受け入れ可能なメールの最大数です。この値を瞬間的に超えることは問題ありませんが、制限を超えた状態が長時間続く場合、送信クォータ同様メールの送信ができなくなる恐れがあります。

送信クォータの上限に達しているかどうかは、CloudWatchアラームなどでリアルタイムに監視するメトリクスが用意されておらず、SESのコンソールやAPIを通じて把握する必要があります。若干の面倒臭さが漂ってきますね。

送信レートの監視をする場合には秒間単位での監視になることや、上限を超えても直ちにペナルティが無いなどのはっきりとしない状況から、より明確なラインで確実な配信不能が訪れる送信クォータの監視をするのが良さそうです。

今回は、送信クォータの60%を消費した場合に、Slackに通知が来るようなLambda関数を紹介します。
※このLambda関数は、送信数に問題がない間は実行ログを吐き出す以外沈黙します。

Lambda関数

大まかな処理の流れは下記のとおりです。

  1. 送信クォータのステータスを取得
  2. 直近24時間の送信数 / 24時間での最大送信数 * 100 [%] を算出
  3. 60%以上消費していた場合、Slackへ通知(60%未満なら何もしない)
    1. 投稿メッセージを作成
    2. WebhookでSlackへPOST

Slackへの投稿はなるべく簡素にし、現在はメタデータなども含めないように(コメントアウト箇所)しています。

import boto3
import json
import urllib.request


def build_message_block(response, send_quota_usage):
    blocks = [
        {
            "type": "header",
            "text": {
                "type": "plain_text",
                "text": "SES Send quota use " + str(int(send_quota_usage)) + "%"
            }
        },
        {
            "type": "divider"
        },
        {
            "type": "section",
            "fields": [
                {
                    "type": "mrkdwn",
                    "text": "*SentLast24Hours (usage):* " + str(f"{int(response['SentLast24Hours']):,}")
                }
            ]
        },
        {
            "type": "section",
            "fields": [
                {
                    "type": "mrkdwn",
                    "text": "*Max24HourSend (limt):*   " + str(f"{int(response['Max24HourSend']):,}")
                }
            ]
        },
        {
            "type": "section",
            "fields": [
                {
                    "type": "mrkdwn",
                    "text": "*MaxSendRate:* " + str(f"{int(response['MaxSendRate']):,}")
                }
            ]
        }
    ]
    
    # if response['ResponseMetadata']:
    #     meta_data_json = json.dumps(response['ResponseMetadata'])
    #     blocks += [
    #         {
    #             "type": "section",
    #             "text": {
    #                 "type": "mrkdwn",
    #                 "text": "*ResponseMetadata:*\n```" + meta_data_json + "```"
    #             }
    #         }
    #     ]
    
    return blocks


def post_slack(blocks, message=None):
    send_data = {
        "channel": "XXXXXXXXXXX",   # SESの通知用channel
        "username": "SES Monitor",
        "icon_emoji": ":chart_with_upwards_trend:",
        "text": message,
        "blocks": blocks
    }
    send_text = "payload=" + json.dumps(send_data)
    
    print('post_slack() send_text: ', send_text)
    
    request = urllib.request.Request(
        "(SlackのWebhook URL)",
        data=send_text.encode('utf-8'),
        method="POST"
    )
    
    with urllib.request.urlopen(request) as response:
        response_body = response.read().decode('utf-8')


def get_ses_send_quota():
    ses = boto3.client('ses')
    response = ses.get_send_quota()
    send_quota_usage = response['SentLast24Hours'] / response['Max24HourSend'] * 100
    
    if send_quota_usage >= 60:
        message_body = build_message_block(response, send_quota_usage)
        notice_message = 'SES send quota usage: ' + str(send_quota_usage) + '%'
        print(message_body)
        print(notice_message)
        post_slack(message_body, notice_message)


def lambda_handler(event, context):
    get_ses_send_quota()
    
    return

Slackへの投稿内容をBlockで作成している都合上、どうしてもその箇所が長大になってしまっています……

Lambda関数に付与する権限

デフォルトのCloudWatchログ関係の他に、「AmazonSESReadOnlyAccess」を雑に付与してありました。もうちょっと精査しても良かったと思う。

実行方法

Lambda関数のトリガーに「EventBridge (CloudWatch Events)」を指定し、cronで定期実行しています。

送信されるメッセージの例

しきい値を下げてテストしたものがこちら。
※緩和申請済みのため送信クォータの上限が高いです。

これで、知らぬ間に送信クォータを使い果たしてメール配信不能に陥るような事故をケアすることができるようになりました。
緩和申請が間に合えば配信不能には陥りませんし、送信クォータの利用率がわからない状態で日々運用するよりも幾分精神衛生に良いと思います。

AWS SESのバウンス発生をSlackに通知する

こいついつもLambdaで何かやってるな。

AWSのSES(Simple Email Service)でバウンスや苦情が発生した際、それをSlackに通知することで、迅速な発生の検知と社内共有ができるようにしてみました。

「SESバウンス発生→SNSトピック呼び出し→SNSトピックからLambda関数を呼び出し」という流れでLambda関数に処理を渡しています。

Lambda関数

大まかな処理の流れは下記のとおりで、処理内容自体は単純です。
苦情の場合は配信抑制リストからの削除処理がありません。

  1. SNSトピックからLambdaが呼び出される。
  2. SNSトピックからのメッセージ内容を見て、バウンスか苦情かで処理を分岐する。
    1. バウンスの場合
      1. バウンス通知メッセージを作成する。
      2. 配信抑制リストから削除するアドレスの場合、削除する。
      3. 削除した場合、削除通知メッセージを通知に追加する。
      4. Slack通知を行うアドレスの場合、SlackへPOSTする。
    2. 苦情の場合
      1. 苦情通知メッセージを作成する。
      2. Slack通知を行うアドレスの場合、SlackへPOSTする。

SlackにPOSTするコンテンツをblock構造で記述しているせいで、非常にコードが長くなってしまっていまいました。見づらくて申し訳ありません。

import json
from datetime import date, datetime
import urllib.request

import boto3


def json_serial(obj):
    # If it is a date type, convert it to a character string.
    if isinstance(obj, (datetime, date)):
        return obj.isoformat()
    # Other than the above, it is not supported.
    raise TypeError ("Type %s not serializable" % type(obj))


def delete_suppressed_destination(mail):
    client = boto3.client('sesv2')

    suppressed = client.get_suppressed_destination(EmailAddress=mail)
    del_json = json.dumps(suppressed, default=json_serial)
    message = '\n'.join([
        '*Delete suppressed destination json*: `' + mail + '`',
        '```' + del_json + '```'
        ])

    try:
        client.delete_suppressed_destination(EmailAddress=mail)
    except client.exceptions.NotFoundException:
        post_slack('*NotFoundException*: `' + mail + '`\n' + message)
        return
    except client.exceptions.BadRequestException:
        post_slack('*BadRequestException*: `' + mail + '`\n' + message)
        return
    except client.exceptions.TooManyRequestsException:
        post_slack('*TooManyRequestsException*: `' + mail + '`\n' + message)
        return

    try:
        suppressed = client.get_suppressed_destination(EmailAddress=mail)
    except client.exceptions.NotFoundException:
        del_response = '*Deleted!*'
    except client.exceptions.BadRequestException:
        del_response = '*BadRequestException*'
    except client.exceptions.TooManyRequestsException:
        del_response = '*TooManyRequestsException*'

    return (del_response, del_json)


def is_no_suppress_mail(mail):
    if 'no-suppress' in mail or mail.endswith('@no.suppress.domain.jp'):
        return True
    return False
    
    
def is_ignore_mail(mail):
    ignore_list = (
        'ignore@example.com',
        )
    if mail in ignore_list or mail.endswith('@ignore.domain.jp'):
        return True
    return False


# Bounce.
def build_blocks_bounce(bounced_recipient, message_json):

    mail = bounced_recipient['emailAddress']

    diagnosticCode = bounced_recipient['diagnosticCode']
    bounce_status = '\n'.join((
        'action: ' + bounced_recipient['action'],
        'status: ' + bounced_recipient['status'],
        'diagnostic_code: ' + diagnosticCode,
        ))

    bounce_json = json.dumps(message_json)

    blocks = [
        {
            "type": "section",
            "fields": [
                {
                    "type": "mrkdwn",
                    "text": "*Mail: *"
                },
                {
                    "type": "mrkdwn",
                    "text": "`" + mail + "`"
                }
            ]
        },
		{
			"type": "section",
			"text": {
				"type": "mrkdwn",
				"text": "*Bounced information: *\n```" + bounce_status + "```"
			}
		},
		{
			"type": "section",
			"text": {
				"type": "mrkdwn",
				"text": "*Bounced message json: *\n```" + bounce_json + "```"
			}
		}
    ]
    return blocks

def build_blocks_delete(mail, del_response, del_json):
    blocks = [
        {
            "type": "header",
            "text": {
                "type": "plain_text",
                "text": "Delete notification"
            }
        },
        {
            "type": "divider"
        },
        {
            "type": "section",
            "fields": [
                {
                    "type": "mrkdwn",
                    "text": "*Mail:*"
                },
                {
                    "type": "mrkdwn",
                    "text": "`" + mail + "`"
                }
            ]
        },
        {
            "type": "section",
            "fields": [
                {
                    "type": "mrkdwn",
                    "text": "*Response:*"
                },
                {
                    "type": "mrkdwn",
                    "text": del_response
                }
            ]
        },
		{
			"type": "section",
			"text": {
				"type": "mrkdwn",
				"text": "*Suppressed destination json: *\n```" + del_json + "```"
			}
		}
    ]
    return blocks

def prepare_bounced_message(message_json):
    bounced_message = [
        {
            "type": "header",
            "text": {
                "type": "plain_text",
                "text": "Bounce notification"
            }
        },
        {
            "type": "divider"
        },
		{
			"type": "section",
            "fields": [
                {
                    "type": "mrkdwn",
                    "text": "*Timestamp (UTC): *"
                },
                {
                    "type": "mrkdwn",
                    "text": "`" + message_json['bounce']['timestamp'] + "`"
                }
            ]
		}
    ]
    return bounced_message


# Complaint.
def build_blocks_complaint(complained_recipient, message_json):

    mail = complained_recipient['emailAddress']

    complaint_json = json.dumps(message_json)

    blocks = [
        {
            "type": "section",
            "fields": [
                {
                    "type": "mrkdwn",
                    "text": "*Mail: *"
                },
                {
                    "type": "mrkdwn",
                    "text": "`" + mail + "`"
                }
            ]
        },
		{
			"type": "section",
			"text": {
				"type": "mrkdwn",
				"text": "*Complaintd message json: *\n```" + complaint_json + "```"
			}
		}
    ]
    return blocks

def prepare_complained_message(message_json):
    complained_message = [
        {
            "type": "header",
            "text": {
                "type": "plain_text",
                "text": "Complaint notification"
            }
        },
        {
            "type": "divider"
        },
		{
			"type": "section",
            "fields": [
                {
                    "type": "mrkdwn",
                    "text": "*Timestamp (UTC): *"
                },
                {
                    "type": "mrkdwn",
                    "text": "`" + message_json['complaint']['timestamp'] + "`"
                }
            ]
		}
    ]
    return complained_message


def post_slack(blocks, message=None):
    send_data = {
        "channel": "XXXXXXXXXXX",   # SESの通知用Channel-ID
        "username": "SES Notice",
        "icon_emoji": ":mailbox_with_no_mail:",
        "text": message,
        "blocks": blocks
    }

    send_text = "payload=" + json.dumps(send_data)

    print('post_slack() send_text: ', send_text)

    request = urllib.request.Request(
        "(SlackのWebhook URL)",
        data=send_text.encode('utf-8'),
        method="POST"
    )

    with urllib.request.urlopen(request) as response:
        response_body = response.read().decode('utf-8')


def ses_process(event):
    message_json = json.loads(event['Records'][0]['Sns']['Message'])

    if message_json['notificationType'] == 'Bounce':
        bounced_recipients = message_json['bounce']['bouncedRecipients']
        bounced_mails = []
        delete_result = []
        notice_message = ""

        for bounced_recipient in bounced_recipients:
            bounced_mails += build_blocks_bounce(bounced_recipient, message_json)
            mail = bounced_recipient['emailAddress']
            if is_no_suppress_mail(mail) and not is_ignore_mail(mail):
                del_result, del_json = delete_suppressed_destination(mail)
                delete_result += build_blocks_delete(mail, del_result, del_json)
                notice_message = "DELETE: " + mail

        bounced_message = prepare_bounced_message(message_json)
        bounced_message = bounced_message + bounced_mails + delete_result

        if not notice_message:
            notice_message = "BOUNCE: " + mail
            
        if not is_ignore_mail(mail):
            post_slack(bounced_message, notice_message)

    elif message_json['notificationType'] == 'Complaint':
        complained_recipients = message_json['complaint']['complainedRecipients']
        complained_mails = []

        for complained_recipient in complained_recipients:
            complained_mails += build_blocks_complaint(complained_recipient, message_json)
            mail = complained_recipient['emailAddress']

        complained_message = prepare_complained_message(message_json)
        complained_message = complained_message + complained_mails
        notice_message = "COMPLAINT: " + mail
        
        if not is_ignore_mail(mail):
            post_slack(complained_message, notice_message)

    return


def lambda_handler(event, context):
    if event['Records'][0]['EventSource'] == 'aws:sns' :
        ses_process(event)

    return

例外処理が甘いですね。

権限については実装当時に精査していなかったようで、「AmazonSESFullAccess」を付与されていましたが、get_suppressed_destination()delete_suppressed_destination()をする権限くらいがあれば動きそうです。甘々ですね。

細かい処理についての補足……?

is_no_suppress_mail(mail)

SESではバウンスの発生時に、配信抑制リスト(suppression-list)というものにメールアドレスを登録し、メールのバウンスが再発することを防止する仕組みがあります。

この仕組みは便利なもので活用したいのですが、一時的な問題でバウンスが発生することが度々ある等、特定のアドレスだけは抑制リストに入らないでほしいようなケースがあるかもしれません。

こういったケースでは、抑制リストに登録された直後にリストから自動で登録解除することで、擬似的にリストに登録されないような状態にすることができます。(バウンスレートが急上昇するリスクもあるので、運用時は要注意)

この「自動で配信抑制リストから削除するアドレス」かを判定する関数が is_no_suppress_mail(mail) です。

is_ignore_mail(mail)

Slackへ通知したくないメールアドレス(既知のダミーアドレス等)もあるため、通知を送らないアドレスのリストを用意します。

Slack通知の有無を判定する際に用いる判定関数が is_ignore_mail(mail) です。

build_blocks_bounce(), build_blocks_delete(), prepare_bounced_message() 等

SNSトピックから受け取ったメッセージを元に、見やすく整形してSlackにPOSTするコンテンツを作成します。

実際の投稿の様子

コードの短さを犠牲に情報が見やすい投稿を実現しています。

バウンス通知(Bounce)
バウンス通知(配信抑制リストにあるもの)
バウンス通知+削除通知(Bounce+Delete)
苦情通知(Complaint)