1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JarvisPHP\TelegramBot; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* TelegramBot |
7
|
|
|
* @author Many |
8
|
|
|
* copied from example found in Telegram site |
9
|
|
|
*/ |
10
|
|
|
class TelegramBotApiWrapper { |
11
|
|
|
|
12
|
|
|
function exec_curl_request($handle) { |
|
|
|
|
13
|
|
|
$response = curl_exec($handle); |
14
|
|
|
|
15
|
|
|
if ($response === false) { |
16
|
|
|
$errno = curl_errno($handle); |
17
|
|
|
$error = curl_error($handle); |
18
|
|
|
echo("Curl returned error $errno: $error\n"); |
19
|
|
|
curl_close($handle); |
20
|
|
|
return false; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE)); |
24
|
|
|
curl_close($handle); |
25
|
|
|
|
26
|
|
|
if ($http_code >= 500) { |
27
|
|
|
// do not want to DDOS server if something goes wrong |
28
|
|
|
sleep(10); |
29
|
|
|
return false; |
30
|
|
|
} else if ($http_code != 200) { |
31
|
|
|
$response = json_decode($response, false); |
32
|
|
|
echo("Request has failed with error ".$response->error_code." : ".$response->description."\n"); |
33
|
|
|
if ($http_code == 401) { |
34
|
|
|
throw new Exception('Invalid access token provided'); |
35
|
|
|
} |
36
|
|
|
return false; |
37
|
|
|
} else { |
38
|
|
|
$response = json_decode($response, false); |
39
|
|
|
if (isset($response->description)) { |
40
|
|
|
echo("Request was successfull: ".$response->description."\n"); |
41
|
|
|
} |
42
|
|
|
$response = $response->result; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $response; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
function apiRequestJson($method, $parameters) { |
|
|
|
|
49
|
|
|
if (!is_string($method)) { |
50
|
|
|
echo("Method name must be a string\n"); |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!$parameters) { |
55
|
|
|
$parameters = array(); |
56
|
|
|
} else if (!is_array($parameters)) { |
57
|
|
|
error_log("Parameters must be an array\n"); |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$parameters["method"] = $method; |
62
|
|
|
|
63
|
|
|
$_BOT_TOKEN = ''; |
64
|
|
|
//Load API key from json config |
65
|
|
|
if(file_exists('TelegramBot/api-key.json')) { |
66
|
|
|
//Create your own bot token and put it in api-key.json |
67
|
|
|
// like {"bot_token": "<your-bot-token>"} |
|
|
|
|
68
|
|
|
$json_config = json_decode(file_get_contents('TelegramBot/api-key.json')); |
69
|
|
|
$_BOT_TOKEN = $json_config->bot_token; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$handle = curl_init('https://api.telegram.org/bot'.$_BOT_TOKEN.'/'); |
73
|
|
|
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
74
|
|
|
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5); |
75
|
|
|
curl_setopt($handle, CURLOPT_TIMEOUT, 60); |
76
|
|
|
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); |
77
|
|
|
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters)); |
78
|
|
|
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); |
79
|
|
|
|
80
|
|
|
return $this->exec_curl_request($handle); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.