|
下面的目的就是想在有人想添加我为好友时,我能同意,并向对方发出添加好友的申请,请各位大大帮忙看看,该怎么完善,谢谢!
import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.QQClient;
import edu.tsinghua.lumaqq.qq.Util;
import edu.tsinghua.lumaqq.qq.beans.QQUser;
import edu.tsinghua.lumaqq.qq.events.IQQListener;
import edu.tsinghua.lumaqq.qq.events.QQEvent;
import edu.tsinghua.lumaqq.qq.net.PortGateFactory;
import edu.tsinghua.lumaqq.qq.packets.in.ReceiveIMPacket;
import edu.tsinghua.lumaqq.qq.packets.in.SystemNotificationPacket;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author zhzhl0
*/
public class qqtest implements IQQListener {
QQClient client = null;
QQUser user = null;
SystemNotificationPacket snpacket = null;
ReceiveIMPacket impacket = null;
int qqnum;
String immsg;
/** Creates a new instance of qqtest */
public qqtest() {
try {
client = new QQClient();
client.addQQListener(this);
client.setConnectionPoolFactory(new PortGateFactory());
user = new QQUser(123456789,"******");
user.setStatus(QQ.QQ_LOGIN_MODE_NORMAL);
user.setUdp(true);
client.setUser(user);
client.setLoginServer("sz.tencent.com");
client.login();
} catch (Exception ex) {
ex.printStackTrace();
client.release();
}
}
public static void main(String[] args) {
new qqtest();
}
public void qqEvent(QQEvent e) {
switch (e.type) {
case QQEvent.QQ_LOGIN_SUCCESS:
this.msg("QQ_LOGIN_SUCCESS");
break;
case QQEvent.QQ_LOGIN_FAIL:
case QQEvent.QQ_LOGIN_UNKNOWN_ERROR:
case QQEvent.QQ_LOGIN_REDIRECT_NULL:
this.msg("QQ_LOGIN_FAIL");
client.release();
System.exit(0);
break;
case QQEvent.QQ_RECEIVE_NORMAL_IM://收到正常消息时
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
impacket = (ReceiveIMPacket) e.getSource();
qqnum = impacket.normalHeader.sender;
immsg = new String(impacket.normalIM.messageBytes);
System.out.println("[" + sdf.format(new Date(impacket.normalHeader.sendTime)) +
"] " +qqnum + ":" + immsg);
if (impacket.normalIM.replyType != QQ.QQ_IM_AUTO_REPLY) {
client.sendIM(qqnum,Util.getBytes("Hello, I am robot"));
}
if (immsg.trim().equalsIgnoreCase("exit")) {
System.out.println(qqnum + "命令你退出。" );
client.logout();
client.release();
System.exit(0);
}
break;
case QQEvent.QQ_ADDED_BY_OTHERS://事件发生在有人将我加为好友时
case QQEvent.QQ_ADDED_BY_OTHERS_EX://事件发生在有人将我加为好友时
this.msg("QQ_ADDED_BY_OTHERS_EX");
snpacket = (SystemNotificationPacket) e.getSource();
qqnum = snpacket.from;
this.msg(qqnum + "把我加为了好友。");
client.sendIM(qqnum,Util.getBytes("hello."));
break;
case QQEvent.QQ_REQUEST_ADD_ME://事件发生在有人请求加我为好友时
case QQEvent.QQ_REQUEST_ADD_ME_EX://事件发生在有人请求加我为好友时
this.msg("QQ_REQUEST_ADD_ME_EX");
snpacket = (SystemNotificationPacket) e.getSource();
qqnum = snpacket.from;
this.msg(qqnum + "想加我为好友。");//1675103
client.sendAddFriendAuth(qqnum,"天天持之以恒");
break;
case QQEvent.QQ_REQUEST_ADD_OTHER_APPROVED://事件发生在有人请求加我为好友时,我同意并且加他为好友
this.msg("QQ_REQUEST_ADD_OTHER_APPROVED");
break;
case QQEvent.QQ_REQUEST_ADD_OTHER_APPROVED_AND_ADD://事件发生在有人请求加我为好友时,我同意并且加他为好友
this.msg("QQ_REQUEST_ADD_OTHER_APPROVED_AND_ADD");
break;
case QQEvent.QQ_REQUEST_ADD_OTHER_REJECTED://事件发生在我请求加一个人,那个人拒绝时
snpacket = (SystemNotificationPacket) e.getSource();
this.msg(snpacket.from + "拒绝加我为好友。理由为:"
+ ((snpacket.message == null || snpacket.message.equals(""))
? "空" : snpacket.message));
break;
}
}
private static void msg(Object obj) {
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
System.out.println("[" + sdf.format(new Date()) + "] " + obj);
}
} |
|