ifhub-telegram/bot.php

167 lines
4.8 KiB
PHP
Raw Permalink Normal View History

2017-05-16 06:42:20 +00:00
<?php
require_once ("vendor/autoload.php");
2024-10-20 05:54:24 +00:00
use Colorfield\Mastodon\MastodonAPI;
2017-05-16 06:42:20 +00:00
use Symfony\Component\Yaml\Yaml;
$config = Yaml::parse(file_get_contents('config.yml'));
2024-09-30 02:16:23 +00:00
$seen = [];
if (file_exists('.seen.yml')) {
$seen = Yaml::parse(file_get_contents('.seen.yml'));
2017-05-16 06:52:32 +00:00
}
2017-05-16 06:42:20 +00: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;
}
2024-09-09 04:07:40 +00:00
function ellipse($str,$n_chars,$crop_str='[…]')
2017-06-16 09:33:01 +00:00
{
$buff = $str;
2017-06-16 09:33:01 +00:00
if(strlen($buff) > $n_chars)
{
$cut_index=strpos($buff,' ',$n_chars);
$buff=substr($buff,0,($cut_index===false? $n_chars: $cut_index+1)).$crop_str;
// then cut for last newline or dot
$cut_index_a=strpos($buff,'.',$n_chars);
$cut_index_b=strpos($buff,PHP_EOL,$n_chars);
$cut_index = max((int) $cut_index_a, (int) $cut_index_b);
$buff=substr($buff,0,($cut_index===false? $n_chars: $cut_index+1)).$crop_str;
2017-06-16 09:33:01 +00:00
}
return trim($buff);
2017-06-16 09:33:01 +00:00
}
function formatdsc($description) {
global $pandoc;
$description = strip_tags($description, 'img');
2017-10-16 08:10:28 +00:00
$description = $pandoc->runWith($description, [
'from' => 'html',
'to' => 'plain',
'wrap' => 'none'
]);
$description = str_replace('Читать дальше', '', $description);
$description = trim($description);
return $description;
}
2017-06-16 19:11:28 +00:00
function download($url, $outFile) {
$options = array(
CURLOPT_FILE => fopen($outFile, 'w'),
CURLOPT_TIMEOUT => 28800, // set this to 8 hours so we dont timeout on big files
CURLOPT_URL => $url
);
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
}
$string = get_text('https://ifhub.club/rss/full/');
2017-05-16 06:42:20 +00:00
$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();
2024-09-30 02:16:23 +00:00
try {
2017-06-16 09:33:01 +00:00
$telegram = new Longman\TelegramBot\Telegram(
$config['TELEGRAM_API_KEY'],
$config['TELEGRAM_BOT_NAME']
);
2024-09-30 02:16:23 +00:00
} catch (\Exception $e) {
if (!$config['DRY_RUN']) {
echo 'Wrong Telegram API key.';
return;
};
}
2024-10-07 02:30:56 +00:00
try {
2024-10-20 05:54:24 +00:00
$oAuth = new Colorfield\Mastodon\MastodonOAuth('IFHub.club', $config['MASTODON_SERVER']);
$oAuth->config->setAuthorizationCode($config['MASTODON_ACCESS_TOKEN']);
$mastodon = new Colorfield\Mastodon\MastodonAPI($oAuth->config);
2024-10-07 02:30:56 +00:00
} catch (\Exception $e) {
2024-10-20 05:54:24 +00:00
echo 'WARNING: could not start Mastodon client';
2024-10-07 02:30:56 +00:00
}
2017-06-16 19:03:07 +00:00
2017-05-16 06:42:20 +00:00
foreach ($articles as $article) {
2024-09-30 02:16:23 +00:00
if (in_array($article['link'], $seen)) {
2017-05-16 06:52:32 +00:00
continue;
}
2017-05-16 06:42:20 +00:00
$title = $article['title'];
$link_mastodon = $article['link'];
2017-05-16 06:42:20 +00:00
$description = $article['description'];
2017-06-16 09:33:01 +00:00
$image = NULL;
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $description, $image);
if (isset($image[1])) {
$image = $image[1];
}
$long_description = $description;
$description = substr( $description, 0, strpos($description, '<a name="cut"'));
$description = formatdsc($description);
/*
if (strlen($description) < 50) { // description is too small
$description = $long_description;
$description = formatdsc($description);
}
*/
2024-09-30 01:55:14 +00:00
$limit = 500 - strlen($link_mastodon) - strlen($title) - 20;
2024-09-30 02:16:23 +00:00
$title = trim($title);
$description = "$title\n\n".ellipse($description, $limit);
2024-09-30 02:16:23 +00:00
$description = $description."\n$link_mastodon";
2017-05-16 06:42:20 +00:00
if (!$config['DRY_RUN']) {
2017-06-16 09:33:01 +00:00
if ($config['TELEGRAM'] === true) {
try {
$result = \Longman\TelegramBot\Request::sendMessage([
'chat_id' => $config['TELEGRAM_CHAT_ID'],
2024-09-30 02:16:23 +00:00
'text' => $description,
2017-06-16 09:33:01 +00:00
'parse_mode' => 'Markdown'
]);
2017-07-19 06:46:31 +00:00
if (!$config['DRY_RUN']) {
file_put_contents('.lastrun', time());
}
2017-06-16 09:33:01 +00:00
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
echo $e;
}
}
2024-10-07 02:30:56 +00:00
if ($config['MASTODON'] === true && isset($mastodon)) {
2017-06-16 19:03:07 +00:00
/*
2017-06-16 09:33:01 +00:00
if ($image) {
2017-06-16 19:11:28 +00:00
download('https://ifhub.club'.$image, './'.basename($image));
2017-06-16 19:03:07 +00:00
$attachment = $mastodon->post('/media', [
2017-06-16 19:11:28 +00:00
'file' => file_get_contents('./'.basename($image))
2017-06-16 19:03:07 +00:00
]);
var_dump($attachment);
2017-06-16 19:11:28 +00:00
unlink('./'.basename($image));
2017-06-16 09:33:01 +00:00
$mdescription .= $attachment->url;
2017-06-16 19:03:07 +00:00
}*/
2024-10-20 05:54:24 +00:00
$mastodon->post('statuses', [
'content' => $description,
'language' => 'ru',
]);
2024-09-30 02:16:23 +00:00
$seen[] = $article['link'];
2017-05-16 06:42:20 +00:00
}
2024-09-30 02:16:23 +00:00
if (!$config['DRY_RUN'] && !empty($config['INDEXNOW'])) {
2023-10-18 07:06:15 +00:00
$client = new \GuzzleHttp\Client();
$client->request('GET', 'https://www.bing.com/indexnow?url='.$link.'&key='.$config['INDEXNOW']);
}
2017-05-16 06:42:20 +00:00
} else {
2024-09-30 02:16:23 +00:00
echo $description."\n---\n";
2017-05-16 06:42:20 +00:00
}
}
2024-09-30 02:16:23 +00:00
if (!$config['DRY_RUN']) {
file_put_contents('.seen.yml', Yaml::dump($seen));
}