-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtelegram.php
133 lines (119 loc) · 3.3 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
mb_substitute_character(0xFFFD);
$telebot = null;
function sendtelegram( $to, $mail, $tag = '' ){
global $telebot;
if( $telebot == null ){
$telebot = new TelegramBot();
}
$cont = TelegramBot::parseHTMLToTelegram( $mail['simplecontent'] );
$sub = TelegramBot::cleanHeaderField( $mail['subject'] );
$fro = TelegramBot::cleanHeaderField( $mail['from'] );
$r = $telebot->sendMessage(
mb_convert_encoding(
'*' . ( empty( $tag ) ? '' : $tag . ' ' ) . $sub . '* _' . $fro . '_' . "\n\n" . $cont,
'UTF-8',
'UTF-8'
),
$to
);
Logger::logTelegram( $r, $to, $sub, $fro, $cont, $tag, $telebot->getLastResponse() );
}
class TelegramBot{
/**
* Decodes HEADER-Texte $f to UTF-8
*/
public static function cleanHeaderField( $f ){
$dec = imap_mime_header_decode( $f );
if( count($dec) > 0 ){
$dec = $dec[0];
$f = ( $dec->charset != 'default' ) ? mb_convert_encoding( $dec->text , 'utf-8', $dec->charset ) : $dec->text;
}
return str_replace( ['[','*','_','[',']','(',')','`',']'], '', $f );
}
/**
* Makes $cont (e.g html) to telegram sendable code
*/
public static function parseHTMLToTelegram($cont){
$cont = str_replace( ['[','*','_','[',']','(',')','`',']'], ['\[','\*','\_','\[','\]','\(','\)','\`','\]'], $cont );
$cont = preg_replace( //allowed html part to markdown
array(
'/<(?:i|(?:em))(?:[^>]*)>([^[^<\/]]*)<\/(?:i|(?:em))>/',
'/<(?:b|(?:strong))(?:[^>]*)>([^[^<\/]]*)<\/(?:b|(?:strong))>/',
'/<code(?:[^>]*)>([^<\/]*)<\/code>/',
'/<pre(?:[^>]*)>([^<\/]*)<\/pre>/',
'/<a(?:[^>]*)href="([^>]*)"(?:[^>]*)>([^<\/]*)<\/a>/'
),
array(
'/_$1_/',
'/*$1*/',
'/`$1`/',
'/```$1```/',
'/[$2]($1)/'
),
$cont
);
return strip_tags( $cont, ''); // html cleanup
}
/**
* JSON Response saver
*/
private $json = null;
/**
* Generates URL for Request, with given $method
*/
private function method_url( $method = 'getMe' ){
return CONFIG::$TELEGRAM['API_URL'] . CONFIG::$TELEGRAM['API_TOKEN'] . '/' . $method;
}
/**
* Sends a html message $cont to a user $to (Chat ID)
*/
public function sendMessage( $cont, $to ){
if( !empty( $to ) && !empty($cont)){
return $this->send('sendMessage', array(
'chat_id' => $to,
'text' => $cont,
'parse_mode' => 'Markdown',
'disable_web_page_preview' => true
));
}
else{
return false;
}
}
/**
* Fetches the Bot updates
*/
public function getUpdates(){
return $this->send( 'getUpdates' );
}
/**
* Gets the last Response data in a readable format
*/
public function getLastResponse(){
return ( $this->json !== null ? print_r( $this->json, true ) : print_r(['status' => 'no last query'], true));
}
/**
* Sends the API Request
*/
private function send( $method, $postarray = array() ){
//API Call
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $this->method_url($method));
curl_setopt($c, CURLOPT_HEADER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $postarray );
$response = curl_exec($c);
$code = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close( $c );
if( $response !== false ){
$this->json = json_decode( $response, true );
return $code == 200 ? true : false;
}
else{
return false;
}
}
}
?>