-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtelegram.php
97 lines (82 loc) · 2.75 KB
/
telegram.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* WordPress to Telegrambot
*
* Description: Read RSS and send to Telegram
* Author: Sean <[email protected]>
* Author URI: https://sean.taipei/
* Version: 0.1
* License: GPLv3
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
*
* @author Sean <[email protected]>
* @license http://www.gnu.org/licenses/gpl-3.0.txt
* @link Github Repo: https://github.com/kocpc/Telegram-RSS-feed-bot
* @since 0.1
*/
require('config.php');
$xml = file_get_contents(RSS_URL);
$object = simplexml_load_string($xml); # XML to Object (will lose some data)
$json = json_encode($object); # Object to JSON
$data = json_decode($json, True); # JSON to Array
$item = $data['channel']['item'][0]; # Latest article
$url = $item['link'];
$pubtime = $item['pubDate']; # publish date
$pubtime = strtotime($pubtime); # Convert to unix timestamp
$late = time() - $pubtime;
if ($late > Rate*60) # second*60 = minute
exit; # No new article
$data = file_get_contents($url);
preg_match('/" rel="author">(.*?)<\/a><\/span>/', $data, $matches);
$author = $matches[1];
$author = str_replace(' ', '_', $author);
$msg .= '<b>' . enHTML($item['title']) . '</b> #' . enHTML($author) . "\n"; # Like this: <bold>Helo World</bold> #Sean
if (preg_match('/<meta name="keywords" content="(.+?)"\/>/', $data, $matches)) {
$keywords = $matches[1];
$keywords = str_replace(', ', ',', $keywords);
$keywords = str_replace(' ', '_', $keywords);
$keywords = str_replace('-', '_', $keywords);
$msg .= 'Tags: #' . str_replace(',', ' #', $keywords) . "\n";
}
if (preg_match('/<meta name="description" content="(.+?)" *\/>/s', $data, $matches))
$msg .= $matches[1] . "\n\n";
/**
* Short Link Enable: <a href="https://google.com/">link</a>
* Shrot Link Disable: https://google.com/
*/
$msg .= (ShortLink ? '<a href="' : '') . enHTML($url) . (ShortLink ? '">link</a>' : '');
sendMsg($msg);
/**
* Send message to channel via Telegram bot API
* @param string $message
*/
function sendMsg(string $messsage) {
$url = 'https://api.telegram.org/bot' . Token . '/sendMessage';
$params = Array(
'chat_id' => Channel,
'text' => $messsage,
'parse_mode' => 'HTML',
);
$params = json_encode($params);
file_get_contents($url, False, stream_context_create(Array(
'http' => Array(
'method' => 'POST',
'header' => Array(
'Content-Type: application/json; charset=utf-8',
),
'content' => $params,
),
)));
}
/**
* Replace &, ", <, > to HTML entity
* Offical document: https://core.telegram.org/bots/api#html-style
* @param string $str
* @return string $str Encoded string
*/
function enHTML(string $str = ''): string {
$search = Array('&', '"', '<', '>');
$replace = Array('&', '"', '<', '>');
$str = str_replace($search, $replace, $str);
return $str;
}