-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestbot.php
107 lines (86 loc) · 3.25 KB
/
testbot.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
define('API_KEY', '127242530:JFIOEFEF-IOINIUDNIAUFBIUESBF');// replace this with your own API_KEY
$debug = false;
if ($argc > 1){
if (($argv[1] == '-d' || $argv[1] == '--debug')) {
$debug = true;
}
}
function APIRequest($Method, $Data, $Debug = null, $APIKey = null)
{
if (defined('API_KEY') && $APIKey === null) {
$APIKey = API_KEY;
}
$ch = curl_init(); //initializes cURL
curl_setopt($ch, CURLOPT_URL, "https://api.telegram.org/bot{$APIKey}/{$Method}");//set the URL that needs to be fetched
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); //seconds to take while trying to connect
curl_setopt($ch, CURLOPT_TIMEOUT, 60); //max time cURL fucntions can execute
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //returns a string when curl_exec() is called
curl_setopt($ch, CURLOPT_POST, true); //to use HTTP POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $Data); //the POST data will be sent from this array
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Expect:']);
$answer = curl_exec($ch); //making connection and getting data
if ($Debug) {
echo "\n ****querry**** ".date('g:i:s a')."\n";
}
if (curl_error($ch)) {
error_log(curl_error($ch));
}
$answer_json = json_decode($answer);
if ($answer_json) {
return $answer_json;
} else {
return $answer;
}
}
$lastoffset = 0;
while (true) {
$post_r = array('offset' => $lastoffset + 1, 'timeout' => 20);
$data = APIRequest('getUpdates', $post_r, $debug);
$reply = null;
if ($data) {
foreach ($data->result as $update_object) {
if ($debug) {
echo 'UpdateOffset: ' . $lastoffset . "\n";
}
if (isset($update_object->message->text)) {
$command = str_replace('@throwawaybot', '', $update_object->message->text);
} else {
continue;
}
$args = explode(' ', $command);
$command = $args[0];
$chatid = $update_object->message->chat->id;
$messageid = $update_object->message->message_id;
if ($command == '/help') {
$helptext = 'this is a throwaway bot';
$reply['method'] = 'sendMessage';
$reply['message'] = array(
'chat_id' => $chatid,
'text' => $helptext,
'reply_to_message_id' => $messageid
);
}
if ($command == '/start') {
//$reply['method'] = 'sendVoice';
$reply['method'] = 'sendMessage';
$reply['message'] = array(
'chat_id' => $chatid,
'reply_to_message_id' => $messageid,
'text' => 'How do I start?'
//'voice' => '@'.realpath('dial-up.ogg'),
);
}
if (!is_null($reply['method']) && !is_null($reply['message'])) {
$datas = APIRequest($reply['method'], $reply['message']);
if ($debug) {
echo $command."\n";
echo $reply['method']." sent \n";
}
}
$lastoffset = $update_object->update_id;
}
}
}