QQ即时通讯消息群发例子,QQ机器人开发学习资料
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OQQ.UI;
using System.Windows.Forms;
using OverredQQ.QQCore;
using KaosCodeGen;
namespace SimpleQQ.QQList
{
public class QQNode : TreeNode
{
private double _Interval = 10*1000;
public double Interval
{
get { return _Interval; }
set { _Interval = value; }
}
private System.Timers.Timer _Timer = null;
public System.Timers.Timer Timer
{
get { return _Timer; }
set { _Timer = value; }
}
private string _Password = string.Empty;
public string Password
{
get { return _Password; }
set { _Password = value; }
}
private string _UserName = string.Empty;
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
private string _msg = "http://haha.kaslive.com 分享一个笑话^_^";
public string msg
{
get { return _msg; }
set { _msg = value; }
}
public QQNode(string qq, string password)
{
this.UserName = qq;
this.Password = password;
this.Text = qq;
_Timer = new System.Timers.Timer(Interval);
_Timer.Enabled = false;
_Timer.Elapsed += new System.Timers.ElapsedEventHandler(_Timer_Elapsed);
}
private GuiAdapter guiAdapter;
private OverredQQ.QQCore.NetworkLayerAdapt networkLayerAdapter;
public void Login(object sender)
{
guiAdapter = new GuiAdapter(uint.Parse(UserName), OQQ.UI.Util.QQPassword(Password), true, null);
guiAdapter.Onlogined += new OnQQReady(guiAdapter_Onlogined);
guiAdapter.User.Udp = true;
networkLayerAdapter = new OverredQQ.QQCore.NetworkLayerAdapt(
guiAdapter.User,
guiAdapter);
//此处服务器可选
networkLayerAdapter.Connect(@"sz.tencent.com", 8000);
}
public void SendJoin()
{
//
}
//public void OnAddNode(TreeNode node,TreeNode no)
//{
// this.Nodes.Add(node);
//}
public AddNodeDelegate addnode = null;
void guiAdapter_Onlogined(OverredQQ.QQCore.Misc.QQCache cache)
{
//addnode = new AddNodeDelegate(OnAddNode);
foreach (OverredQQ.QQCore.Bean.Uin uin in cache.AllFriends.Uins)
{
FriendNode friend = new FriendNode(uin.Id, cache.AllFriends[uin].Nick);
friend.Adapter = this.guiAdapter;
friend.msg = msg;
this.Nodes.Add(friend);
}
foreach (OverredQQ.QQCore.Bean.ClusterId clusterId in cache.ClusterIds)
{
//GroupNode
GroupNode group = new GroupNode(clusterId.Id,cache.Clusters[clusterId].Name);
group.Adapter = this.guiAdapter;
group.msg = msg;
//this.Nodes.Add(group);
this.Nodes.Add(group);
//this.SendClusterIM(clusterId, new Font(FontFamily.Families[0], 12), Color.Red, this.msg);
//Thread.Sleep(800);
//this.textBoxMsg.Text += "group:" + Convert.ToString(clusterId);
}
}
public Random rr = new Random();
public bool OpenSendIM = true;
public bool OpenSendGIM = true;
public bool OpenSendADDM = true;
public void OnAutoStartSendMsg(object sender)
{
this.Timer.Enabled = true;
this.Timer.Start();
this.Text = this.UserName + "(正在运行)";
}
void _Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (guiAdapter == null)
{
Login(null);
}
if (guiAdapter.IsReady)
{
AutoMSG();
}
}
public int OpenSendADDMCount = 0;
public int ii = 0;
public bool MV = false;
public bool AV = false;
public bool Done
{
get
{
return MV && AV;
}
}
public void AutoMSG()
{
if (guiAdapter == null)
{
Login(null);
}
if (!guiAdapter.IsReady)
{
AppContext.Write("正在登陆....");
return;
}
if (MV && AV)
{
this.Timer.Enabled = false;
this.Timer.Stop();
guiAdapter.Dispose();//注销QQ
//TreeView p = this.TreeView;
//this.Remove();
//if (p.Nodes.Count > 0)
//{
// QQNode node = this.TreeView.Nodes[0] as QQNode;
// node.OnAutoStartSendMsg(null);
//}
}
//发送消息例子
if (ii < this.Nodes.Count)
{
FriendNode node = this.Nodes[ii] as FriendNode;
if (OpenSendIM && node != null) node.SendMsg(null);
GroupNode gnode = this.Nodes[ii] as GroupNode;
if (OpenSendGIM && gnode != null) gnode.SendMsg(null);
ii++;
}
else
{
MV = true;
}
//邀请好友实例
if (OpenSendADDM)
{
uint fqq = 0;
while (fqq==0)
{
int[] n = new int[6];
for (int i = 0; i < 6; i++)
{
n[i] = rr.Next(0, 99);
}
int cn = rr.Next(4, 6);
string qq = "";
for (int c = 0; c < cn; c++)
{
qq += n[c].ToString();
}
uint.TryParse(qq, out fqq);
}
int addme = rr.Next(10);
//if (addme <= 2)
//{
// guiAdapter.Adapter.SendAddFriendAuthPacket(51267195, FriendAuthRequest.Request, "我");
// AppContext.AMsg(string.Format("正在邀请:{0}为好友~^_^", fqq));
//}
//else
{
guiAdapter.Adapter.SendAddFriendAuthPacket(fqq, FriendAuthRequest.Request, "http://haha.kaslive.com~一个笑话");
AppContext.AMsg(string.Format("正在邀请:{0}为好友~^_^", fqq));
OpenSendADDMCount++;
if (OpenSendADDMCount > 10)
{
OpenSendADDM = false;//邀请10个,超过就停止
AV = true;
}
}
}
}
}
public delegate void AddNodeDelegate(TreeNode node);
}
实例下载