forked from kakaochatfriend/KakaoChatFriendAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKakaoBot.class.php
More file actions
130 lines (100 loc) · 2.64 KB
/
Copy pathKakaoBot.class.php
File metadata and controls
130 lines (100 loc) · 2.64 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
define('RESULT_OK', 200);
class KakaoBot {
var $socket;
var $address;
var $port;
var $handler;
var $rbuf;
function KakaoBot($address, $port, $handler) {
$this -> address = $address;
$this -> port = $port;
$this -> handler = $handler;
if (!$this -> connect()) {
throw new Exception("connection error");
}
}
function connect() {
$this -> socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
return socket_connect($this -> socket, $this -> address, $this -> port);
}
function disconnect() {
socket_close($this -> socket);
$this -> socket = null;
}
function login($bot_id, $auth_key) {
$msg = array("type" => "login", "id" => $bot_id, "pass" => $auth_key);
$this -> send($msg);
$ret = $this -> receive();
if ($ret["code"] != RESULT_OK) {
throw new Exception("login error code : " . $ret["code"] . ", msg : " . $ret["msg"]);
}
}
function send($msg) {
$serialized_message = bson_encode($msg);
$len = strlen($serialized_message);
$off = 0;
while ($off < $len) {
$len_write = socket_write($this -> socket, $off == 0 ? $serialized_message : $substr($serialized_message, $off), $len - $off);
if (!$len_write)
throw new Exception("write error");
$off += $len_write;
}
}
function receive() {
$this -> rbuf = '';
$ret = socket_recv($this -> socket, $this -> rbuf, 4, MSG_PEEK | MSG_WAITALL);
if (!$ret)
throw new Exception("socket read error");
$msg_len = unpack('N', strrev($this -> rbuf));
$this -> rbuf = '';
echo "receiving message size = $msg_len[1]";
$ret = socket_recv($this -> socket, $this -> rbuf, $msg_len[1], MSG_WAITALL);
if (!$ret)
throw new Exception("socket read error");
return bson_decode($this -> rbuf);
}
function process() {
$msg = $this -> receive();
switch($msg["type"]) {
case "add" :
$this -> handler -> onAdd($this, $msg);
break;
case "request" :
$this -> handler -> onRequestMessage($this, $msg);
break;
case "ping" :
$this -> handler -> onPing($this, $msg);
break;
case "result" :
$this -> handler -> onResult($this, $msg);
break;
case "leave" :
$this -> handler -> onLeave($this, $msg);
break;
case "block" :
$this -> handler -> onBlock($this, $msg);
break;
default :
echo "unknown message : " . $msg["type"];
}
}
}
class BotMessageHandler {
function onAdd($bot, $req) {
}
function onRequestMessage($bot, $req) {
}
function onPing($bot, $req) {
echo "ping arrived";
$res = array("type" => "pong", "time" => (int)$req["time"]);
$bot -> send($res);
}
function onResult($bot, $req) {
}
function onLeave($bot, $req) {
}
function onBlock($bot, $req) {
}
}
?>