Using Smack XMPP API to send Facebook Chat Messages

| No TrackBacks

之前說到XMPP已經成為即時訊息的標準,那就利用Facebook手機即時通的便捷快速來送Facebook Chat訊息。
其實只要拿之前GTalk的程式碼,稍微修改一下,就可發送了。其中差異在於DIGEST-MD5的認證機制。在Smack討論區已經有人實作出XMPP DIGEST-MD5的物件。只要將login部份加上DIGEST-MD5認證。
※注意:facebook chat發送對象的帳號是-XXXXX@@chat.facebook.com,而XXXXX是一組數字,必須由c.displayBuddyList();列出才會知道。
完整程式碼如下:

package messenger;


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Collection;


import org.jivesoftware.smack.Chat;

import org.jivesoftware.smack.ConnectionConfiguration;

import org.jivesoftware.smack.MessageListener;

import org.jivesoftware.smack.Roster;

import org.jivesoftware.smack.RosterEntry;

import org.jivesoftware.smack.SASLAuthentication;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.XMPPException;

import org.jivesoftware.smack.packet.Message;


public class facebook implements MessageListener {

XMPPConnection connection;

private volatile static facebook fclient;

static String username = "xxxxxxxx";

static String password = "xxxxxxxx";

private facebook(){}

public static facebook getInstance(){

if (fclient == null) {

synchronized (facebook.class){

if (fclient == null) {

fclient = new facebook();

}

}

}

return fclient;

}


public void login(String userName, String password) throws XMPPException {

SASLAuthentication.registerSASLMechanism("DIGEST-MD5", MySASLDigestMD5Mechanism.class);

ConnectionConfiguration config = new ConnectionConfiguration(

"chat.facebook.com", 5222, "chat.facebook.com");

config.setCompressionEnabled(true);

config.setSASLAuthenticationEnabled(true);

connection = new XMPPConnection(config);


connection.connect();

connection.login(userName, password);

}

public void sendMessage(String message, String to) throws XMPPException {

Chat chat = connection.getChatManager().createChat(to, this);

chat.sendMessage(message);

}


public void displayBuddyList() {

Roster roster = connection.getRoster();

roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);

Collection<RosterEntry> entries = roster.getEntries();


System.out.println("\n\n" + entries.size() + " buddy(ies):");

for (RosterEntry r : entries) {

System.out.println(r.getUser());

}

}

public void addRoster(String bot, String email, String input){

facebook c = facebook.getInstance();

try {

addRoster(bot,input);

c.sendMessage(input, email);

} catch (XMPPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public void addRoster(String bot,String input){

facebook c = facebook.getInstance();

try {

c.login(username, password);

Roster roster = connection.getRoster();

roster.createEntry(input, null, null);

} catch (XMPPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

c.disconnect();

}

public void disconnect() {

connection.disconnect();

}


public void processMessage(Chat chat, Message message) {

if (message.getType() == Message.Type.chat)

System.out.println(chat.getParticipant() + " says: "

+ message.getBody());

}


public static void main(String args[]) throws XMPPException, IOException {

// declare variables

facebook c = facebook.getInstance();

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String msg;


// turn on the enhanced debugger

XMPPConnection.DEBUG_ENABLED = false;


// provide your login information here

c.login(username, password);


c.displayBuddyList();

System.out.println("-----");

System.out.println("Enter your message in the console.");

System.out.println("All messages will be sent to abhijeet.maharana");

System.out.println("-----\n");


while (!(msg = br.readLine()).equals("bye")) {

// your buddy's gmail address goes here

c.sendMessage(msg, "-xxxxxxxx@chat.facebook.com");

}


c.disconnect();

System.exit(0);

}


public void alert(String bot,String email, String msg) {

facebook c = facebook.getInstance();


// turn on the enhanced debugger

XMPPConnection.DEBUG_ENABLED = false;


// provide your login information here

try {

c.login(username, password);

c.sendMessage(msg, email);

} catch (XMPPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

c.disconnect();

}

}

No TrackBacks

TrackBack URL: http://server.everfine.com.tw/blog/mt-tb.cgi/318

February 2012

Sun Mon Tue Wed Thu Fri Sat
      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      

Archives

Powered by Movable Type 4.34-en

About this Entry

This page contains a single entry by philipz published on January 5, 2012 11:02 AM.

XMPP協定已一統即時訊息協定標準 was the previous entry in this blog.

Find recent content on the main index or look in the archives to find all content.