From 39c6b08ee6bd13e2b39f5908e8a7680204b6b572 Mon Sep 17 00:00:00 2001 From: ghost Date: Thu, 21 Dec 2023 09:59:05 +0200 Subject: [PATCH] add keywords support --- config.example.json | 14 ++++++++++++-- src/cli/bot.php | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/config.example.json b/config.example.json index e5f0718..1ca13b6 100644 --- a/config.example.json +++ b/config.example.json @@ -21,7 +21,12 @@ "queue": { "limit":5 - } + }, + "keywords": + [ + "News", + "news" + ] }, { "source":"https://...", @@ -29,7 +34,12 @@ "queue": { "limit":5 - } + }, + "keywords": + [ + "News", + "news" + ] } ] } \ No newline at end of file diff --git a/src/cli/bot.php b/src/cli/bot.php index ca12a33..820d407 100644 --- a/src/cli/bot.php +++ b/src/cli/bot.php @@ -152,21 +152,52 @@ foreach ($config->feed as $feed) $k = (int) $posts['result'][0]['userpost']['k'] + 1; } - else continue; + else continue; // @TODO log } - // Empty feed does not return null - else if (is_null($posts)) continue; + else if (is_null($posts)) continue; // empty feed does not return null @TODO log + + // Apply keywords + $search = []; + foreach ($feed->keywords as $keyword) + { + $search[] = sprintf( + ' %s', // make sure keyword is not a part of another construction by separator (e.g. URL) + $keyword + ); + } + + $replace = []; + foreach ($feed->keywords as $keyword) + { + $replace[] = sprintf( + ' #%s', + $keyword + ); + } // Send each message to the twister account foreach ($query->fetchAll() as $queue) { + // Apply replacements + $message = str_replace( + $search, + $replace, + $queue->message + ); + + // Keep message original on length limit reached + if (mb_strlen($message) > 256) + { + $message = $queue->message; + } + $errors = []; $twister->newPostMessage( $feed->target, $k, - $queue->message, + $message, $errors );