forked from hanksudo/facebook-chat-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb_chat_bot.php
272 lines (214 loc) · 8.04 KB
/
fb_chat_bot.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
@include_once('config.php');
require_once('lib.php');
$STREAM_XML = '<stream:stream '.
'xmlns:stream="http://etherx.jabber.org/streams" '.
'version="1.0" xmlns="jabber:client" to="chat.facebook.com" '.
'xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace">';
$AUTH_XML = '<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" '.
'mechanism="X-FACEBOOK-PLATFORM"></auth>';
$CLOSE_XML = '</stream:stream>';
$RESOURCE_XML = '<iq type="set" id="3">'.
'<bind xmlns="urn:ietf:params:xml:ns:xmpp-bind">'.
'<resource>fb_xmpp_script</resource></bind></iq>';
$SESSION_XML = '<iq type="set" id="4" to="chat.facebook.com">'.
'<session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></iq>';
$START_TLS = '<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>';
$MESSAGE_XML = '<message type="chat" from="-%s@chat.facebook.com" to="-%s@chat.facebook.com">'.
'<body>%s</body></message>';
function open_connection($server) {
echo "[INFO] Opening connection... ";
$fp = fsockopen($server, 5222, $errno, $errstr);
if (!$fp) {
echo "$errstr ($errno)<br>";
} else {
echo "connnection open<br>";
}
return $fp;
}
function send_xml($fp, $xml) {
fwrite($fp, $xml);
}
function recv_xml($fp, $size=4096) {
$xml = fread($fp, $size);
if ($xml === "") {
return null;
}
// parses xml
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $xml, $val, $index);
xml_parser_free($xml_parser);
return array($val, $index);
}
function find_xmpp($fp, $tag, $value=null, &$ret=null) {
static $val = null, $index = null;
do {
if ($val === null && $index === null) {
list($val, $index) = recv_xml($fp);
if ($val === null || $index === null) {
return false;
}
}
foreach ($index as $tag_key => $tag_array) {
if ($tag_key === $tag) {
if ($value === null) {
if (isset($val[$tag_array[0]]['value'])) {
$ret = $val[$tag_array[0]]['value'];
}
return true;
}
foreach ($tag_array as $i => $pos) {
if ($val[$pos]['tag'] === $tag && isset($val[$pos]['value']) && $val[$pos]['value'] === $value) {
$ret = $val[$pos]['value'];
return true;
}
}
}
}
$val = $index = null;
} while (!feof($fp));
return false;
}
function xmpp_connect($options, $access_token) {
global $STREAM_XML, $AUTH_XML, $RESOURCE_XML, $SESSION_XML, $CLOSE_XML, $START_TLS;
$fp = open_connection($options['server']);
if (!$fp) {
return false;
}
// initiates auth process (using X-FACEBOOK_PLATFORM)
send_xml($fp, $STREAM_XML);
if (!find_xmpp($fp, 'STREAM:STREAM')) {
return false;
}
if (!find_xmpp($fp, 'MECHANISM', 'X-FACEBOOK-PLATFORM')) {
return false;
}
// starting tls - MANDATORY TO USE OAUTH TOKEN!!!!
send_xml($fp, $START_TLS);
if (!find_xmpp($fp, 'PROCEED', null, $proceed)) {
return false;
}
stream_socket_enable_crypto($fp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
send_xml($fp, $STREAM_XML);
if (!find_xmpp($fp, 'STREAM:STREAM')) {
return false;
}
if (!find_xmpp($fp, 'MECHANISM', 'X-FACEBOOK-PLATFORM')) {
return false;
}
// gets challenge from server and decode it
send_xml($fp, $AUTH_XML);
if (!find_xmpp($fp, 'CHALLENGE', null, $challenge)) {
return false;
}
$challenge = base64_decode($challenge);
$challenge = urldecode($challenge);
parse_str($challenge, $challenge_array);
// creates the response array
$resp_array = array(
'method' => $challenge_array['method'],
'nonce' => $challenge_array['nonce'],
'access_token' => $access_token,
'api_key' => $options['app_id'],
'call_id' => 0,
'v' => '1.0',
);
// creates signature
$response = http_build_query($resp_array);
// sends the response and waits for success
$xml = '<response xmlns="urn:ietf:params:xml:ns:xmpp-sasl">'.base64_encode($response).'</response>';
send_xml($fp, $xml);
if (!find_xmpp($fp, 'SUCCESS')) return false;
// finishes auth process
send_xml($fp, $STREAM_XML);
if (!find_xmpp($fp,'STREAM:STREAM')) return false;
if (!find_xmpp($fp, 'STREAM:FEATURES')) return false;
send_xml($fp, $RESOURCE_XML);
if (!find_xmpp($fp, 'JID')) return false;
send_xml($fp, $SESSION_XML);
if (!find_xmpp($fp, 'SESSION')) return false;
echo ("<p>Authentication complete</p>");
return $fp;
}
function xmpp_send_msg($fp, $options) {
global $CLOSE_XML, $MESSAGE_XML;
echo 'Sent message: <strong>'.$options['msg'].'</strong>, from '.$options['uid'].' to '.$options['recv_id'];
send_xml($fp, sprintf($MESSAGE_XML, $options['uid'], $options['recv_id'], $options['msg']));
send_xml($fp, $CLOSE_XML);
}
//Gets access_token with xmpp_login permission
function get_access_token($app_id, $app_secret, $my_url){
if( !isset($_REQUEST["code"]) && empty($_REQUEST["code"]) ) {
$dialog_url = "https://www.facebook.com/dialog/oauth?scope=xmpp_login".
"&client_id=".$app_id.
"&redirect_uri=".urlencode($my_url);
echo("<script>top.location.href='".$dialog_url."'</script>");
exit();
}
$token_url = "https://graph.facebook.com/oauth/access_token?".
"client_id=".$app_id."&redirect_uri=".urlencode($my_url).
"&client_secret=" . $app_secret.
"&code=" . $_REQUEST["code"];
$access_token = @file_get_contents($token_url);
parse_str($access_token, $output);
return($output['access_token']);
}
function _main() {
global $app_id, $app_secret, $uid, $recv_id, $access_token;
echo "<h2>Facebook Chat Bot</h2>";
if (isset($_GET['app_id'])) $app_id = $_GET['app_id'];
if (isset($_GET['app_secret'])) $app_secret = $_GET['app_secret'];
if (isset($_GET['uid'])) $uid = $_GET['uid'];
if (isset($_GET['recv_id'])) $recv_id = $_GET['recv_id'];
// if app_id or app_secret not set, show config form.
if ( empty($app_id) || empty($app_secret) ) {
show_config_form();
exit();
}
// get access_token if not set
if ( !isset($access_token) || empty($access_token) ) {
echo '<p>Trying to getting access_token ... ';
$access_token = get_access_token($app_id, $app_secret, current_url());
if (empty($access_token)) {
unset($_GET['code']);
echo("<script>top.location.href='" . current_url(true) . '?' . http_build_query($_GET) . "'</script>");
exit();
}
echo 'Done.</p>';
echo '<p>access_token: '.$access_token.'</p>';
}
// get uid from Facebook /me
if (empty($uid)) {
$json_me = json_decode(file_get_contents('https://graph.facebook.com/me?access_token='.$access_token));
$uid = $json_me->id;
echo "<p>uid empty, get uid from Graph API. uid= ".$uid."</p>";
}
// if reciver empty, receiver will be sender.
if (empty($recv_id)) $recv_id = $uid;
// $json_friends = json_decode(file_get_contents('https://graph.facebook.com/me/friends?access_token='.$access_token));
// list_array($json_friends->data);
_post();
}
function _post() {
global $app_id, $app_secret, $uid, $recv_id, $access_token;
// connect to XMPP Authentication
$server_options = array(
'app_id' => $app_id,
'server' => 'chat.facebook.com'
);
$fp = xmpp_connect($server_options, $access_token);
// send message
if ($fp) {
$msg_options = array(
'uid' => $uid,
'recv_id' => $recv_id,
'msg' => 'Hi, I am robot. '.date("Y-m-d H:i:s").'
Source Code: https://github.com/hankwang/facebook-chat-bot/'
);
xmpp_send_msg($fp, $msg_options);
} else {
echo "An error ocurred<br>";
}
fclose($fp);
}
_main();