Browse Source

add keywords support

main
ghost 11 months ago
parent
commit
39c6b08ee6
  1. 14
      config.example.json
  2. 39
      src/cli/bot.php

14
config.example.json

@ -21,7 +21,12 @@
"queue": "queue":
{ {
"limit":5 "limit":5
} },
"keywords":
[
"News",
"news"
]
}, },
{ {
"source":"https://...", "source":"https://...",
@ -29,7 +34,12 @@
"queue": "queue":
{ {
"limit":5 "limit":5
} },
"keywords":
[
"News",
"news"
]
} }
] ]
} }

39
src/cli/bot.php

@ -152,21 +152,52 @@ foreach ($config->feed as $feed)
$k = (int) $posts['result'][0]['userpost']['k'] + 1; $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; // empty feed does not return null @TODO log
else if (is_null($posts)) continue;
// 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 // Send each message to the twister account
foreach ($query->fetchAll() as $queue) 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 = []; $errors = [];
$twister->newPostMessage( $twister->newPostMessage(
$feed->target, $feed->target,
$k, $k,
$queue->message, $message,
$errors $errors
); );

Loading…
Cancel
Save