-
Notifications
You must be signed in to change notification settings - Fork 0
/
telebot.php
121 lines (112 loc) · 3.68 KB
/
telebot.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
<?php
class TeleBotLib {
public $url = "https://api.telegram.org/bot";
public $token = "";
private $logs;
public function __construct($token = null) {
$this->setToken($token);
$this->logs = array();
}
public function setToken($token = null) {
$this->token = ($token !== null) ? $token : 'setDefaultKey';
$this->url .= $this->token;
}
private function addLog($command, $params, $result){
$this->logs[] = array(
"command" => $command,
"params" => $params,
"result" => $result
);
}
public function showLogs(){
return $this->logs;
}
public function send($command, $data=array()){
$url = $this->url.'/'.$command;
if(count($data)){
$postdata = http_build_query($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
}else{
$result = file_get_contents($url);
}
$result = json_decode($result);
$this->addLog($command, $data, $result);
return $result;
}
public function sendMessage($chatId, $text, $parseMode = "html") {
$postData = array(
'chat_id' => $chatId,
'text' => $text,
'parse_mode' => $parseMode
);
$result = $this->send("sendmessage", $postData);
return $result;
}
public function sendDocument($chatId, $text, $documentURL) {
$postData = array(
'chat_id' => $chatId,
'document' => $documentURL,
'caption' => $text,
'parse_mode' => 'html'
);
$result = $this->send("senddocument", $postData);
return $result;
}
public function editMessage($chatId, $messageId, $text) {
$postData = array(
'chat_id' => $chatId,
'message_id' => $messageId,
'text' => $text,
'parse_mode' => "html"
);
$result = $this->send("editmessagetext", $postData);
return $result;
}
public function editMessageCaption($chatId, $messageId, $caption) {
$postData = array(
'chat_id' => $chatId,
'message_id' => $messageId,
'caption' => $caption,
'parse_mode' => "html"
);
$result = $this->send("editmessagecaption", $postData);
return $result;
}
public function pinChatMessage($chatId, $messageId) {
$postData = array(
'chat_id' => $chatId,
'message_id' => $messageId,
);
$result = $this->send("pinchatmessage", $postData);
return $result;
}
public function getMe() {
$result = $this->send("getME");
return $result;
}
public function getUpdates() {
$result = $this->send("getUpdates");
return $result;
}
public function findChatId() {
$chatId = "";
$results = $this->getUpdates();
if(isset($results->result[0]->message->chat->id)){
$chatId = $results->result[0]->message->chat->id;
}
return $chatId;
}
public function getLastIncomeMessage() {
$results = $this->getUpdates();
$last = end($results->result);
return $last->message->text;
}
}