1
0
Fork 0
mirror of https://gitlab.com/ifiction/ifhub-telegram.git synced 2024-04-29 23:59:34 +03:00
ifhub-telegram/bot.php

64 lines
1.8 KiB
PHP
Raw Normal View History

2017-05-16 09:42:20 +03:00
<?php
require_once ("vendor/autoload.php");
use Symfony\Component\Yaml\Yaml;
$config = Yaml::parse(file_get_contents('config.yml'));
2017-05-16 09:52:32 +03:00
$lastrun = 0;
if (file_exists('.lastrun')) {
$lastrun = file_get_contents('.lastrun');
}
echo $lastrun;
2017-05-16 09:42:20 +03:00
function get_text($url) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
));
$resp = curl_exec($curl);
curl_close($curl);
return $resp;
}
$string = get_text('https://ifhub.club/rss/new/');
$service = new \Sabre\Xml\Service();
$service->elementMap = [
'{}item' => function(\Sabre\Xml\Reader $reader) {
return \Sabre\Xml\Deserializer\keyValue($reader, '');
},
'{}channel' => function(\Sabre\Xml\Reader $reader) {
return \Sabre\Xml\Deserializer\repeatingElements($reader, '{}item');
},
];
$articles = $service->parse($string)[0]['value'];
unset($string);
$pandoc = new \Pandoc\Pandoc();
foreach ($articles as $article) {
2017-05-16 09:52:32 +03:00
if (strtotime($article['pubDate']) <= $lastrun) {
continue;
}
2017-05-16 09:42:20 +03:00
$title = $article['title'];
$link = $article['link'];
$description = $article['description'];
// $description = strip_tags($description);
$description = $pandoc->convert($description, "html", "markdown_github");
$description = "$title\n\n".$description;
$description .= ": $link";
if (!$config['DRY_RUN']) {
try {
$telegram = new Longman\TelegramBot\Telegram($config['API_KEY'], $config['BOT_NAME']);
$result = \Longman\TelegramBot\Request::sendMessage([
'chat_id' => $config['CHAT_ID'],
'text' => $description,
'parse_mode' => 'Markdown'
]);
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
echo $e;
}
} else {
echo $description;
}
}
2017-05-16 09:52:32 +03:00
file_put_contents('.lastrun', time());