Skip to main content

Notify production team on Slack when new episodes found during feed sync

Free Beginner

When Benecaster imports episodes from an existing feed, it does so on a schedule and without saying anything. That is the right default once you trust it — and exactly the wrong one while you are still finding out whether you do.

benecaster_feed_sync_completed runs at the end of every sync and reports what was found, including the created drafts’ IDs. Posting that to Slack gives the team a running account of what is being picked up, which is worth having during a migration, when the question “did it get everything?” is asked daily and answering it by hand means counting episodes. If a count is enough, the lighter benecaster_feed_sync_after( int $show_id, int $drafts_created, int $episodes_new, string $triggered_by ) carries no IDs but is simpler to consume.

Code

<?php
add_action(
    'benecaster_feed_sync_completed',
    function ( array $result ): void {
        if ( 'success' !== $result['status'] || empty( $result['draft_ids'] ) ) {
            return;
        }

        $lines = array_map(
            static fn ( $id ): string => '- ' . get_the_title( (int) $id ),
            $result['draft_ids']
        );

        wp_remote_post( 'https://hooks.slack.com/services/T000/B000/XXXX', [
            'timeout'  => 5,
            'blocking' => false,
            'headers'  => [ 'Content-Type' => 'application/json' ],
            'body'     => wp_json_encode( [
                'text' => sprintf(
                    "*%s*: %d new draft(s) from Feed Sync (%s):\n%s",
                    get_the_title( (int) $result['show_id'] ),
                    count( $result['draft_ids'] ),
                    $result['triggered_by'],
                    implode( "\n", $lines )
                ),
            ] ),
        ] );
    }
);

View on GitHub →

Hooks Used

Need this built rather than just documented? See our services →