using System;
using System.Collections.Generic;
using System.Text;
using XihSolutions.DotMSN;
namespace Robot
{
public class Robot
{
private XihSolutions.DotMSN.Messenger messenger = new Messenger();
public Robot()
{
InitializeComponent();
// by default this example will emulate the official microsoft windows messenger client
messenger.Credentials.ClientID = "msmsgs@msnmsgr.com";
messenger.Credentials.ClientCode = "Q1P7W2E4J9R8U3S5";
//登录成功之后触发事件
messenger.NameserverProcessor.ConnectionEstablished += new EventHandler(NameserverProcessor_ConnectionEstablished);
messenger.Nameserver.SignedIn += new EventHandler(Nameserver_SignedIn);
messenger.ConversationCreated += new ConversationCreatedEventHandler(messenger_ConversationCreated);
}
//MSN机器人登录函数
private void login_Click(object sender, EventArgs e)
{
if (messenger.Connected)
{
SetStatus("Disconnecting from server");
messenger.Disconnect();
}
// set the credentials, this is ofcourse something every DotMSN program will need to
// implement.
messenger.Credentials.Account = account.Text;
messenger.Credentials.Password = password.Text;
// inform the user what is happening and try to connecto to the messenger network.
SetStatus("Connecting to server");
messenger.Connect();
// note that Messenger.Connect() will run in a seperate thread and return immediately.
// it will fire events that informs you about the status of the connection attempt.
// these events are registered in the constructor.
}
//Robot Form定时器执行函数
private void Timer_Tick(object sender, EventArgs e)
{
//遍历MSN机器人的所有好友
foreach (Contact contact in messenger.ContactList.All)
{
if (contact != null && contact.Online == true)
{
//实例化一个机器人与好友的对话
Conversation conversation = messenger.CreateConversation();
conversation.Invite(contact);//邀请好友参与对话
MyConversation agent = CreateMyConversation(conversation);//实例化一个对话处理
}
}
}
//对话处理的实例化(这个很关键)
/// <summary>
/// A delegate passed to Invoke in order to create the conversation form in the thread of the main form.
/// </summary>
private delegate MyConversation CreateConversationDelegate(Conversation conversation);
private MyConversation CreateMyConversation(Conversation conversation)
{
// create a new conversation. However do not show the window untill a message is received.
// for example, a conversation will be created when the remote client sends wants to send
// you a file. You don't want to show the conversation form in that case.
MyConversation agent = new MyConversation(conversation);
// do this to create the window handle. Otherwise we are not able to call Invoke() on the
// conversation form later.
//agent.Handle.ToInt32();
return agent;
}
//如果是第一次好友主动向MSN机器人发送消息,则调用
private void messenger_ConversationCreated(object sender, ConversationCreatedEventArgs e)
{
// check if the request is initiated remote or by this object
// if it is initiated remote then we have to create a conversation form. Otherwise the
// form is already created and we don't need to create another one.
if (e.Initiator == null)
{
// use the invoke method to create the form in the main thread
this.Invoke(new CreateConversationDelegate(CreateMyConversation), new object[] { e.Conversation });
}
}
//再来看看class MyConversation
private Conversation _conversation;
/// <summary>
/// The conversation object which is associated with the form.
/// </summary>
public Conversation Conversation
{
get { return _conversation; }
}
public void MyConversation(Conversation conversation)
{
_conversation = conversation;
//绑定事件,处理好友发送消息给MSN机器人
Conversation.Switchboard.TextMessageReceived += new TextMessageReceivedEventHandler(Switchboard_TextMessageReceived);
Conversation.Switchboard.SessionClosed += new SBChangedEventHandler(Switchboard_SessionClosed);
Conversation.Switchboard.ContactJoined += new ContactChangedEventHandler(Switchboard_ContactJoined);
Conversation.Switchboard.ContactLeft += new ContactChangedEventHandler(Switchboard_ContactLeft);
}
//MSN机器人给好友发送消息
public void SendInput(String inputMessage)
{
// check whether there is input
if (inputMessage.Length == 0) return;
// if there is no switchboard available, request a new switchboard session
if (Conversation.SwitchboardProcessor.Connected == false)
{
Conversation.Messenger.Nameserver.RequestSwitchboard(Conversation.Switchboard, this);
}
// note: you can add some code here to catch the event where the remote contact lefts due to being idle too long
// in that case Conversation.Switchboard.Contacts.Count equals 0.
TextMessage message = new TextMessage(inputMessage);
/* You can optionally change the message's font, charset, color here.
* For example:
* message.Color = Color.Red;
* message.Decorations = TextDecorations.Bold;
*/
Conversation.Switchboard.SendTextMessage(message);
}
private void Switchboard_ContactJoined(object sender, ContactEventArgs e)
{
SendInput("定时发送的消息");//实现MSN机器人向好友主动推送消息
}
private void PrintText(string name, string text)
{
if (text.ToLower().Equals("h"))
{
SendInput("帮助命令如下:\r输入“on”接收朋友信息\r输入“off”停止接收朋友信息\r输入“@用户名+空格+内容”给某人发送信息\r输入“*用户名+空格+内容”给某人发送悄悄话\r输入“find+空格+关键字”查询用户\r输入“add+空格+用户名”添加某人为好友\r输入其他信息则发送到有趣吧。");
}
}
//MSN机器人接收消息
private void Switchboard_TextMessageReceived(object sender, TextMessageEventArgs e)
{
PrintText(e.Sender.Name, e.Message.Text);
}
}
}
错误 1 找不到类型或命名空间名称“MyConversation”(是否缺少 using 指令或程序集引用?) C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\ClassLibrary2\ClassLibrary2\Class1.cs 64 26 ClassLibrary2
错误 2 找不到类型或命名空间名称“MyConversation”(是否缺少 using 指令或程序集引用?) C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\ClassLibrary2\ClassLibrary2\Class1.cs 66 17 ClassLibrary2
我错在那儿了?
DotMSN2.0在http://www.xihsolutions.net/dotmsn/download.html可以下载。
跪求了,不完成这个世界上又要多一个可怜的失业者了。