post_to_telegram/init.php
2022-02-23 20:35:44 +07:00

77 lines
2 KiB
PHP

<?php
include_once __DIR__.'/prefs.php';
class Post_To_Telegram extends Plugin {
private $host;
function about() {
return array(1.0,
"Filter action to send articles to Telegram",
"Oreolek");
}
function init($host) {
$this->host = $host;
$this->pdo = Db::pdo();
$host->add_hook($host::HOOK_ARTICLE_FILTER_ACTION, $this);
$host->add_hook($host::HOOK_FILTER_TRIGGERED, $this);
$host->add_filter_action($this, "action_telegram", "Send to Telegram");
}
function flags() {
return array("needs_curl" => true);
}
function hook_filter_triggered($feed_id, $owner_uid, $article, $matched_filters, $matched_rules, $article_filters) {
# Don't apply to articles that have been deleted by a previous filter
$delete = false;
foreach($article_filters as $action_key => $action) {
if ($action["type"] === "filter") {
$delete = true;
}
if ($delete && $action["param"] === "Post_To_Telegram:action_telegram") {
unset($article_filters[$action_key]);
}
}
}
function hook_article_filter_action($article, $action) {
if ($action == "action_telegram") {
if (!function_exists("curl_init"))
return $article;
# Ignore articles that are already in the database so we only trigger the webhook once
$csth = $this->pdo->prepare("SELECT id FROM ttrss_entries
WHERE guid = ? OR guid = ?");
$csth->execute([$article['guid'], $article['guid_hashed']]);
if ($row = $csth->fetch()) {
Debug::log("Article already in database, not triggering telegram..", Debug::$LOG_VERBOSE);
return $article;
}
$content = '### '.$article["title"] . "\n" . $article["link"] . "\n" . $article['content'];
$telegram = new Longman\TelegramBot\Telegram(
TELEGRAM_API_KEY,
TELEGRAM_BOT_NAME
);
\Longman\TelegramBot\Request::sendMessage([
'chat_id' => TELEGRAM_CHAT_ID,
'text' => $content,
'parse_mode' => 'Markdown'
]);
}
return $article;
}
function api_version() {
return 2;
}
}
?>