-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
telegram.php
151 lines (144 loc) · 4.49 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
class telegram
{
public $token;
public $db;
public $query;
public $queryResult;
public function __construct($token, $host, $username, $password, $dbname)
{
$this->token = $token;
try {
$this->db = new PDO(
"mysql:host=$host;dbname=$dbname",
$username,
$password,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4")
);
} catch (PDOException $error) {
echo "Unable to connect to database.";
}
}
public function __destruct()
{
$this->db = null;
}
// get users msg + info
public function getTxt()
{
$text = json_decode(file_get_contents('php://input'));
return $text;
}
// send message to specific user
public function sendMessage($userid, $text)
{
$url = 'https://api.telegram.org/bot' . $this->token . '/sendMessage?chat_id=' . $userid . '&text=' . $text;
file_get_contents($url);
}
public function sendMessageCURL($userid, $text, $options)
{
$url = 'https://api.telegram.org/bot' . $this->token . '/sendMessage';
$keyboard = $this->makeMenu($options);
$postfields = array(
'chat_id' => $userid,
'text' => $text,
//'parse_mode' => 'HTML'
'reply_markup' => $keyboard
);
$this->executeCURL($url, $postfields);
}
public function sendHTML($userid, $text, $options)
{
$url = 'https://api.telegram.org/bot' . $this->token . '/sendMessage';
$keyboard = $this->makeMenu($options);
$postfields = array(
'chat_id' => $userid,
'text' => $text,
'parse_mode' => 'HTML',
'reply_markup' => $keyboard
);
$this->executeCURL($url, $postfields);
}
public function makeMenu($options)
{
$keyboard = array(
'keyboard' => $options,
'resize_keyboard' => true,
'one_time_keyboard' => false,
'selective' => true
);
return $keyboard = json_encode($keyboard);
}
public function getChatMember($channel, $user_id)
{
$get = $this->exeCURL('getChatMember', [
'chat_id' => $channel,
'user_id' => $user_id
]);
$data = $get->result->status;
return $data;
}
public function deleteMessage($chatid, $msgid)
{
$url = 'https://api.telegram.org/bot' . $this->token . '/deleteMessage?chat_id=' . $chatid . '&message_id=' . $msgid;
file_get_contents($url);
}
public function executeCURL($url, $postfields)
{
$ch = curl_init();
//$timeout = 100 ; // 10 seconds
// rawurlencode for sanitizing malicius inputs
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: multipart/form-data"
));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
//curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$contents = curl_exec($ch);
curl_close($ch);
$output = json_decode($contents, true);
return $output;
}
public function exeCURL($method, $datas = [])
{
$url = "https://api.telegram.org/bot" . TOKEN . "/" . $method;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($datas));
$res = curl_exec($ch);
if (curl_error($ch)) {
var_dump(curl_error($ch));
} else {
return json_decode($res);
}
}
}
function bot($method, $datas = [])
{
$url = "https://api.telegram.org/bot" . TOKEN . "/" . $method;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($datas));
$res = curl_exec($ch);
if (curl_error($ch)) {
var_dump(curl_error($ch));
} else {
return json_decode($res);
}
}
function get_type($id)
{
$url = "https://api.telegram.org/bot" . TOKEN . "/getFile?file_id=$id";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
if (curl_error($ch)) {
var_dump(curl_error($ch));
} else {
return json_decode($res);
}
}