-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtelegrambot.class.php
107 lines (95 loc) · 3.22 KB
/
telegrambot.class.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
<?php
class TelegramBot{
var $base_bot_url;
var $token=null;
var $ca_cert=null;
function __construct($token){
$this->token=$token;
$this->base_bot_url="https://api.telegram.org/bot".$token."/";
}
function useCertificate($path){
$this->ca_cert=$path;
}
private function httpRequest($url){
if($this->ca_cert){
$ssl_options = array("ssl"=>array(
"cafile" => ($this->ca_cert),
"verify_peer"=> true,
"verify_peer_name"=> true,
));
return file_get_contents($url, false, stream_context_create($ssl_options));
}else{
return $this->url_get_contents($url);
}
}
function url_get_contents($url){
if(!function_exists('curl_init')){
return file_get_contents($url);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
private function callTelegramAPI($command, $data){
return $this->httpRequest(($this->base_bot_url).$command."?".str_replace("&", "&", http_build_query($data)));
}
function sendMessage($text, $chat_id, $disable_notifications=false){
$this->callTelegramAPI("sendMessage",[
'text'=>$text,
'chat_id'=>$chat_id,
'disable_notification'=>$disable_notifications
]);
}
function sendPhoto($image_url, $chat_id, $caption='', $callback=null){
$url = ($this->base_bot_url)."sendPhoto?chat_id=".$chat_id;
$post_fields = array(
'chat_id' => $chat_id,
'photo' => new CURLFile(realpath($image_url)),
'caption' => $caption
);
$ch = curl_init();
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, $post_fields);
$output = curl_exec($ch);
curl_close($ch);
if($callback!=null) $callback();
}
function getUpdates($offset=0,$limit=100,$timeout=0){
return $this->callTelegramAPI("getUpdates",['offset'=>$offset,'limit'=>$limit,'timeout'=>$timeout]);
}
function parseUpdates($updates,$chat_id=null){
$result = json_decode($updates);
if($result->ok){
if(array_key_exists('result', $result)){
$updates=$result->result;
$parsed_update=array();
foreach($updates as $key=>$update){
$message=$update->message;
// Reconstruct user message in a more human readable way with only necessary info.
$parsed_message=array();
$parsed_message['update_id']=$update->update_id;
$parsed_message['message_id']=$message->message_id;
$parsed_message['from_id']=$message->from->id;
$parsed_message['from_username']=isset($message->from->username) ? $message->from->username : '';
$parsed_message['date']=$message->date;
$parsed_message['text']=$message->text;
// If we are parsing all messages or we are parsing only messages from a certain chat_id...
if($chat_id==null || $chat_id==$parsed_message['from_id']){
// Save the parsed message to a list.
$parsed_update[]=$parsed_message;
}
}
return $parsed_update;
}else return false;
}else return false;
}
function getParsedUpdates($chat_id=null,$offset=0,$limit=100,$timeout=0){
return $this->parseUpdates($this->getUpdates($offset,$limit,$timeout),$chat_id);
}
}
?>