饭否,唧歪们MSN、QQ、GTalk机器人实现原理及代码
—-饭否,唧歪MSN机器人实现原理(DotMSN2.0解析篇,以下代码作者全部测试通过)

腾讯滔滔近期发布,各大IT网站争相报道
最后的结论是滔滔不会做MSN、GTalk机器人,所以饭否、唧歪类网站在腾讯入侵后仍然有生存的余地。
twitter模式,即迷你博客网站的兴起、流行主要是因为接入方法多样
我们打开这些网站,可以发现迷你博客内容的发布多数不是来自网页,超过半数是来自IM
因为IM是互联网发展到现在非常成功的一个产品,其中QQ、MSN、GTalk占有绝对优势
我们的网民每天上网都会使用IM,而饭否、唧歪类网站是第一批开始借助IM运作的互联网产品
饭否是目前国内模仿twitter模式较为成功的,我们以饭否为例,IM我们以MSN为例
我们可以通过MSN接入饭否做以下事情,首先加饭否的MSN机器人为好友,记我的MSN帐号为A,饭否机器人MSN帐号为B,我在饭否加了一个好友为C:
1.A打开B,输入“H”,即A发送消息“H”给B,B马上把使用帮助信息发送给A
2.A打开B,输入其他字符串,马上饭否网站A的迷你博客有了一条新的,内容是这个字符串
3.C在饭否通过一种方式发布了一个迷你博客,A这时在用MSN,B为主动发送一个消息给A,消息内容是:C发布了迷你博客“迷你博客内容”
具体到技术层面,这些功能是怎么实现的呢?
微软公司的MSN,现在叫做Live Messager,提供了开发接口,通过更容易做二次开发的当推DotMSN2.0
开发平台为Visual Studio 2005,基于.net 2.0

我们对DotMSN2.0源代码进行了分析、改造,现总结如下:
先通过改造后的程序功能介绍让列位看官有一个感性的认识
整个应用程序有2个主体类
public partial class Robot : Form
//界面,实现机器人MSN帐号的登录,以及定时读取数据库
class MyConversation
//实现机器人与好友对话的功能
//Robot的实现:
// Create a Messenger object to use DotMSN.
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 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);                   
         }

通过以上程序,即对dotmsn2.0实例代码的改造,初步实现了类似饭否、唧歪这些仿twitter的MSN机器人
我们结合上面的程序,再来看看饭否的MSN机器人
1.我们的MSN输入h给机器人,机器人可以把一个固定的字符串发过来,这就对应上面代码的Switchboard_TextMessageReceived模块;
2.我们输入其他信息给机器人,还是这个模块,只不过继续改装把这个消息数据写入到饭否的数据库;
3.我们的好友在饭否发布了消息,MSN机器人会马上主动把这个消息推送给我,这个怎么实现呢?
上面我给出的代码,Timer时间间隔设置为2秒,然后 Timer_Tick继续改造
先读取数据库,看有没有新的消息,如果有,则把这个消息发送者的关注者(并且绑定了MSN)形成一个MSN用户列表
这些用户肯定是MSN机器人的好友,于是下面可以把这个消息主动推送给这些用户,其他功能类似。

遗憾的是,在DotMSN2.0中还没有找到MSN用户个性签名变化引发的消息,希望列位看官有知道的给予补充。

这篇博文介绍了饭否类网站MSN机器人的实现
而GTalk机器人实现更为简单,因为Google公布了GTalk的协议以及开发文档,后面的文章会进行介绍
那么QQ机器人是怎么做的呢?我们研究了linux平台下的lumaqq源代码,并进行改造,后面的文章也会介绍QQ机器人是怎么实现,敬请关注。

另外MSN机器人会在我们的网站有趣吧(http://www.youqubar.com)使用,只要你添加我们的MSN机器人为好友
这个机器人会主动推送网站上新的趣图给您,敬请关注。

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1749348