-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.php
More file actions
69 lines (61 loc) · 2.27 KB
/
Copy pathMain.php
File metadata and controls
69 lines (61 loc) · 2.27 KB
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
<?php
require __DIR__.'/vendor/autoload.php';
require "Utils.php";
use Slack\User;
use Slack\ChannelInterface;
#https://hooks.slack.com/services/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
$loop = React\EventLoop\Factory::create();
#$client = new \Slack\ApiClient($loop);
$client = new Slack\RealTimeClient($loop);
$client->setToken('xoxb-349247732690-XXXXXXXXXXXXXXXX');
date_default_timezone_set('Europe/Kiev');
$client->on('message', function ($data) use ($client) {
$user_id = $data['user'];
$msg = $data['text'];
$message_data = array($msg, date("Y-m-d H:i:s"));
$wdata = null;
$real_name = null;
$user_name = null;
$client->getUserById($data['user'])->then(function (User $user) use (&$real_name, &$user_name) {
$real_name = $user->getRealName();
$user_name = $user->getUsername();
});
try {
$wdata = json_decode(read_json_data("data.json"), true);
if ($wdata == null) {
throw new Exception;
}
try {
$msgs = $wdata[(string)$user_id]["messages"];
$msgs[count($msgs)] = $message_data;
}
catch (Exception $e) {
$msgs = array($message_data);
}
$dest = array( $user_id=> array("_name"=>$user_name, "_real_name"=>$real_name, "messages"=>$msgs));
$wdata = array_merge($wdata, $dest);
write_json_data("data.json", json_encode($wdata));
}
catch (Exception $e) {
echo $e;
$wdata = array();
$msgs = array($message_data);
$dest = array( $user_id=> array("_name"=>$user_name, "_real_name"=>$real_name, "messages"=>$msgs));
$wdata = array_merge($wdata, $dest);
write_json_data("data.json", json_encode($wdata));
}
$command = parse_direct_message($msg);
if ($command == "send_messages") {
#send command
$client->getChannelGroupOrDMByID($data["channel"])->then(function (ChannelInterface $channel) use ($client, $wdata) {
return $client->send(read_json_data("data.json"), $channel)->then(null);
});
}
});
$client->connect()->then(function () use ($client) {
echo "Connected! Start bot...\n";
$client->getAuthedUser()->then(function (User $user) {
echo "bot_id ".$user->getId()."\n";
});
});
$loop->run();