vk() api bot

 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
<?php
$bot_id = 1;
$admin_id = 1;
$chat_id = 37;
function vk($method, $data, $token = "47a23205f295accea4e01cesosihuiec913ec7fbsas23b6b7ba0ce11c0800691f")
{
$url = "https://api.vk.com/method/" . $method;
if ($curl = curl_init()) {
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$data['access_token'] = $token;
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$out = curl_exec($curl);
curl_close($curl);
return $out;
}
return true;
}
function send($chat_id, $msg, $attachment='')
{
vk("messages.send", array(
"chat_id" => $chat_id,
"message" => $msg,
"attachment" => $attachment));
}
function getRndUser($chat_id) {
$s = json_decode(vk("messages.getChatUsers", array("chat_id" => $chat_id, "fields" => "photo")), 1);
$rnd = mt_rand(0, count($s["response"]));
$rnd_user = $s["response"][$rnd]["first_name"] . " " . $s["response"][$rnd]["last_name"];
return $rnd_user;
}
//echo '<pre>';
//print_r();
$last_msg = 0;
while (1) {
$msgs = json_decode(vk("messages.get", array("count" => 20, "out" => 0)), 1);
if ($msgs["response"][1]["mid"] > $last_msg) {
$msg = $msgs["response"][1]["body"];
$args = mb_split(" ", $msg);
unset($args[0]);
switch ($msg) {
case "пинг":
send($chat_id, "понг");
break;
case "кто здесь няшка?":
send($chat_id, getRndUser($chat_id));
break;
}
$last_msg = $msgs["response"][1]["mid"];
}
sleep(1);
}