<script>
var g_app_id,g_app_secret;
function on_roster(iq) {
$(iq).find('item').each(function () {
var jid = $(this).attr('jid');
var name = $(this).attr('name') || jid;
$('#contacts').append(new Option(name , jid, true, true));
$("#contacts option[value='"+jid+"']").attr("disabled","");
});
syslog('add ' + $('#contacts option').length + ' contacts');
// set up presence handler and send initial presence
connectionFB.addHandler(on_presence, null, "presence");
connectionFB.addHandler(on_message, null, "message", "chat");
}
function on_message(message) {
var from = $(message).attr('from');
if($('#contacts option[value='+from+']').length = 0)
var name = from;
else
var name = $('#contacts option[value='+from+']').text();
var body = $(message).find("html > body");
if (body.length === 0) {
body = $(message).find('body');
if (body.length > 0) {
body = body.text()
} else {
body = null;
}
} else {
body = body.contents();
}
if(body!=null)
{
syslog( '<font color="blue">' + name + ' says:' + body + '</font>');
}
return true;
}
function on_presence(presence) {
var ptype = $(presence).attr('type');
var from = $(presence).attr('from');
if($('#contacts option[value='+from+']').length = 0)
return;
var name = $('#contacts option[value='+from+']').text();
if (ptype === 'subscribe') {
syslog('jid: ' + from + ' (' + name + ') has subscribed.');
} else if (ptype !== 'error') {
if (ptype === 'unavailable') {
// set offline
syslog('jid: ' + from + ' ( ' + name + ' ) is offline');
} else {
var show = $(presence).find("show").text();
$("#contacts option[value='"+from+"']").attr("disabled","");
if (show === "" || show === "chat") {
// set online
syslog('jid: ' + from + ' ( ' + name + ' ) is online');
} else {
// set away
syslog('jid: ' + from + ' ( ' + name + ' ) is away');
}
}
}
return true;
}
function syslog(msg)
{
console.log(msg);
$('#syslog').append(msg+'<br />');
}
function CreateStrophe(app_id, app_secret)
{
g_app_id = app_id;
g_app_secret = app_secret;
connectionFB = new Strophe.Connection("http://bosh.metajack.im:5280/xmpp-httpbind");
connectionFB.facebookConnect("your-name@chat.facebook.com", onConnectFacebook, 300, 1, app_id, app_secret, g_accessToken);
}
function reconnect()
{
connectionFB.facebookConnect("your-name@chat.facebook.com", onConnectFacebook, 300, 1, g_app_id, g_app_secret, g_accessToken);
}
function onConnectFacebook(resp)
{
// syslog('onConnectFacebook (' + resp + ')');
switch(resp)
{
case Strophe.Status.CONNECTED:
$('#btnSend').show('slow');
var iq = $iq({type: 'get'}).c('query', {xmlns: 'jabber:iq:roster'});
connectionFB.sendIQ(iq, on_roster);
syslog('You can send the messages now!');
syslog('Retriving contacts...');
break;
case Strophe.Status.ERROR:
syslog('onConnect: ERROR!');
break;
case Strophe.Status.CONNECTING:
syslog('onConnect: CONNECTING!');
break;
case Strophe.Status.CONNFAIL:
syslog('onConnect: CONNFAIL!');
reconnect();
break;
case Strophe.Status.AUTHENTICATING:
syslog('onConnect: AUTHENTICATING!');
break;
case Strophe.Status.AUTHFAIL:
syslog('onConnect: AUTHFAIL!');
reconnect();
break;
case Strophe.Status.DISCONNECTED:
syslog('onConnect: DISCONNECTED!');
reconnect();
break;
case Strophe.Status.DISCONNECTING:
syslog('onConnect: DISCONNECTING!');
break;
case Strophe.Status.ATTACHED:
syslog('onConnect: ATTACHED!');
break;
}
}
function sendMsg(jid, text)
{
var message = $msg({to: jid,
"type": "chat"})
.c('body').t(text).up()
.c('active', {xmlns: "http://jabber.org/protocol/chatstates"});
connectionFB.send(message);
}
</script>
關於 email 如何取得,這部分只要是有開發 FB App 經驗的應該都不是問題,所以就不細說了。而權限部分則記得增加以下幾個權限!
user_online_presence, friends_online_presence, xmpp_login, read_friendlists