分享

一个gtalk的robot的简单实现...

 shaobin0604@163.com 2007-05-28
package jj.chen;
/*
* 本程序用到了www.的smack api
* 如果大家对googletalk有兴趣可以去以上网站自己下一个api
* 祝愿googletalk一路走好!
*/
import java.io.Serializable;
import java.util.Collection;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.GoogleTalkConnection;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
public class Test implements Serializable {

/**
*
*/
private static final long serialVersionUID = -5102956900764101510L;

/**
* @param args
* @throws XMPPException
* @throws XMPPException
*/
public static void main(String[] args) throws XMPPException {
// TODO Auto-generated method stub


final GoogleTalkConnection con = new GoogleTalkConnection();
con.login("jjchen831", "123456");//googletalk的 用户名, 密码
System.out.println("===========1");


Presence presence = new Presence(Presence.Type.AVAILABLE);
con.sendPacket(presence);


System.out.println("===============2");


final Roster roster = con.getRoster();
roster.addRosterListener(new RosterListener() {
public void presenceChanged(String user) {
// If the presence is unavailable then "null" will be printed,
// which is fine for this example.
System.out.println("Presence changed: "
+ roster.getPresence(user));


if(roster.getPresence(user)!=null){
final Chat chat=con.createChat(user);
try {

chat.sendMessage("hello");//给每个在线人说hello

chat.addMessageListener(new PacketListener(){

public void processPacket(Packet arg0) {
// TODO Auto-generated method stub
System.out.println("PacketListener");
Message message=chat.nextMessage(); //得到别人给你的信息

try {
f(message.getBody().equals("help")){
//如果别人给你的信息是help
chat.sendMessage("thank you for test robot!");
//就发给他thank you for test robot!
}else{
chat.sendMessage(" please enter help ");
//如果别人发给你的信息不是help就发给他 please enter help
}
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

});
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
system.out.println(user+"++++++++");
}

}

public void entriesAdded(Collection arg0) {
// TODO Auto-generated method stub
System.out.println("entriesAdded");
}

public void entriesUpdated(Collection arg0) {
// TODO Auto-generated method stub
System.out.println("entriesUpdated");
}

public void entriesDeleted(Collection arg0) {
// TODO Auto-generated method stub
System.out.println("entriesDeleted");
}
});
System.out.println("===============3");


while(true){
try {
Thread.sleep(500000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多