post_to_telegram/init.php

77 lines
2 KiB
PHP
Raw Normal View History

2019-03-03 11:23:06 +02:00
<?php
2022-02-23 15:35:44 +02:00
include_once __DIR__.'/prefs.php';
2022-02-23 15:30:18 +02:00
class Post_To_Telegram extends Plugin {
2019-03-03 11:23:06 +02:00
private $host;
function about() {
return array(1.0,
2022-02-23 15:30:18 +02:00
"Filter action to send articles to Telegram",
"Oreolek");
2019-03-03 11:23:06 +02:00
}
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);
2022-02-23 15:30:18 +02:00
$host->add_filter_action($this, "action_telegram", "Send to Telegram");
2019-03-03 11:23:06 +02:00
}
function flags() {
return array("needs_curl" => true);
}
2022-01-14 12:25:39 +02:00
function hook_filter_triggered($feed_id, $owner_uid, $article, $matched_filters, $matched_rules, $article_filters) {
2019-03-03 11:23:06 +02:00
# 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;
}
2022-02-23 15:30:18 +02:00
if ($delete && $action["param"] === "Post_To_Telegram:action_telegram") {
2019-03-03 11:23:06 +02:00
unset($article_filters[$action_key]);
}
}
}
function hook_article_filter_action($article, $action) {
2022-02-23 15:30:18 +02:00
if ($action == "action_telegram") {
2019-03-03 11:23:06 +02:00
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()) {
2022-02-23 15:30:18 +02:00
Debug::log("Article already in database, not triggering telegram..", Debug::$LOG_VERBOSE);
2019-03-03 11:23:06 +02:00
return $article;
}
2022-02-23 15:30:18 +02:00
$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;
}
2019-03-03 11:23:06 +02:00
}
?>