在网络上查阅了很多关于通过DotMSN开发包来开发MSN机器人的小程序,为了验证其正确性,本人亲自测试,发现了这样一篇文章,他写的很好,没有问题。下面就贴出源代码,和大家分享。请提前在项目引用中添加下载到的DotMSN.dll文件,再using DotMSN;
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DotMSN;
namespace MSNBotTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DotMSN.Messenger msn = new DotMSN.Messenger();//初始化一个Messenger对象用来登陆服务器
private void button1_Click(object sender, EventArgs e)
{
try
{
statusBar1.Text="正在连接到服务器";
msn.ConversationCreated += new Messenger.ConversationCreatedHandler(ConversationCreated);
msn.Connect(textBox1.Text, textBox2.Text);//前一个为用户名,后一个为密码
msn.SynchronizeList();初始化列表,用来显示在线用户
statusBar1.Text="成功连接到服务器";
}
catch (DotMSN.MSNException me)
{
MessageBox.Show(me.ToString());
}
}
private void ReadMsg(Conversation sender, DotMSN.MessageEventArgs e)
{
//string content;
string input = e.Message.Text;
if (input.Equals("j"))
{
string content = "你好,我是机器人小范";
sender.SendMessage(content);
}
else if (input.StartsWith("H"))
{
string content = "我很笨,很多功能还在开发哦";
sender.SendMessage(content);
}
else
{
string content = "你好啊,我不知道你说的是什么啊,有什么事情请输入H";
sender.SendMessage(content);
}
}
private void ConversationCreated(Messenger sender, ConversationEventArgs e)
{
e.Conversation.MessageReceived += new Conversation.MessageReceivedHandler(ReadMsg);
}
private void button2_Click(object sender, EventArgs e)
{
try
{
ShowAll();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void ShowAll()
{
try
{
msn.SetStatus(MSNStatus.Online);
AllListView.Clear();
foreach (Contact contact in msn.GetListEnumerator(MSNList.ForwardList))
{
ListViewItem item = new ListViewItem(contact.Name);
item.Tag = contact;
if (contact.Status != MSNStatus.Offline)
{
AllListView.Items.Add((item + "在线").Remove(0, 14));
}
// if(contact.Status == MSNStatus.Busy)
// {
// AllListView.Items.Add(item+"忙碌");
// }
// if(contact.Status == MSNStatus.Away)
// {
// AllListView.Items.Add(item+"离开");
// }
else
{
AllListView.Items.Add((item + "离线").Remove(0, 14));
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button3_Click(object sender, EventArgs e)//用来断开连接
{
msn.CloseConnection();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DotMSN;
namespace MSNBotTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DotMSN.Messenger msn = new DotMSN.Messenger();//初始化一个Messenger对象用来登陆服务器
private void button1_Click(object sender, EventArgs e)
{
try
{
statusBar1.Text="正在连接到服务器";
msn.ConversationCreated += new Messenger.ConversationCreatedHandler(ConversationCreated);
msn.Connect(textBox1.Text, textBox2.Text);//前一个为用户名,后一个为密码
msn.SynchronizeList();初始化列表,用来显示在线用户
statusBar1.Text="成功连接到服务器";
}
catch (DotMSN.MSNException me)
{
MessageBox.Show(me.ToString());
}
}
private void ReadMsg(Conversation sender, DotMSN.MessageEventArgs e)
{
//string content;
string input = e.Message.Text;
if (input.Equals("j"))
{
string content = "你好,我是机器人小范";
sender.SendMessage(content);
}
else if (input.StartsWith("H"))
{
string content = "我很笨,很多功能还在开发哦";
sender.SendMessage(content);
}
else
{
string content = "你好啊,我不知道你说的是什么啊,有什么事情请输入H";
sender.SendMessage(content);
}
}
private void ConversationCreated(Messenger sender, ConversationEventArgs e)
{
e.Conversation.MessageReceived += new Conversation.MessageReceivedHandler(ReadMsg);
}
private void button2_Click(object sender, EventArgs e)
{
try
{
ShowAll();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void ShowAll()
{
try
{
msn.SetStatus(MSNStatus.Online);
AllListView.Clear();
foreach (Contact contact in msn.GetListEnumerator(MSNList.ForwardList))
{
ListViewItem item = new ListViewItem(contact.Name);
item.Tag = contact;
if (contact.Status != MSNStatus.Offline)
{
AllListView.Items.Add((item + "在线").Remove(0, 14));
}
// if(contact.Status == MSNStatus.Busy)
// {
// AllListView.Items.Add(item+"忙碌");
// }
// if(contact.Status == MSNStatus.Away)
// {
// AllListView.Items.Add(item+"离开");
// }
else
{
AllListView.Items.Add((item + "离线").Remove(0, 14));
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button3_Click(object sender, EventArgs e)//用来断开连接
{
msn.CloseConnection();
}
}
}