CSDN博客

img UglyBean

制作MSN机器人 (转帖)

发表于2004/9/16 16:49:00  797人阅读

突然有点心动想做个MSN机器人,在 dotMSN 找到了一个 .net 写的库。调用起来非常简单。

  //登陆
  private void StartMSN()
  {
   if( (txtEmail.Text != "")||(txtPsw.Text != "") )
   {
    msn.Connect(txtEmail.Text, txtPsw.Text);
    if(msn.Connected)
    {
     //setup callback function
     msn.ContactOnline += new DotMSN.Messenger.ContactOnlineHandler(msn_ContactOnline);
     msn.ContactOffline += new DotMSN.Messenger.ContactOfflineHandler(OnContactOffline);
     msn.SynchronizationCompleted += new DotMSN.Messenger.SynchronizationCompletedHandler(OnSynchronizationCompleted);
     msn.MessageReceived += new DotMSN.Messenger.MessageReceivedHandler(OnMessageReceived);
     msn.ConversationCreated += new DotMSN.Messenger.ConversationCreatedHandler(OnConversationCreated);
     msn.SynchronizeList();
     txtLog.Text = "MSN Connected/r/n" + txtLog.Text;
    }
   }
  }
  //注销
  private void EndMSN()
  {
   if(msn.Connected)
   {
    msn.CloseConnection();
    if(!msn.Connected)
    {
     txtLog.Text = "MSN Disconnected/r/n" + txtLog.Text;
    }
   }
  }
  //成功同步联系人列表
  private void OnSynchronizationCompleted(Messenger sender, EventArgs e)
  {
   msn.SetStatus(MSNStatus.Online);
   txtLog.Text = "set status : Online/r/n" + txtLog.Text;
   FillListView(lstViewFL, msn.GetListEnumerator(MSNList.ForwardList) );
   FillListView(lstViewRL, msn.ReverseList);
   FillListView(lstViewBL, msn.BlockedList);
   FillListView(lstViewAL, msn.AllowedList);
  }
  //联系人上线
  private void msn_ContactOnline(Messenger sender, ContactEventArgs e)
  {
   txtLog.Text = e.Contact.Name + " status : " + e.Contact.Status.ToString() + "/r/n" + txtLog.Text;
   RefreshListView(lstViewFL, e.Contact);
   RefreshListView(lstViewRL, e.Contact);
   RefreshListView(lstViewBL, e.Contact);
   RefreshListView(lstViewAL, e.Contact);
  }
  //联系人下线
  private void OnContactOffline(Messenger sender, ContactEventArgs e)
  {
   txtLog.Text = e.Contact.Name + " status : " + e.Contact.Status.ToString() + "/r/n" + txtLog.Text;
   RefreshListView(lstViewFL, e.Contact);
   RefreshListView(lstViewRL, e.Contact);
   RefreshListView(lstViewBL, e.Contact);
   RefreshListView(lstViewAL, e.Contact);
  }
  //初始化联系人列表
  private void FillListView(ListView lst, Messenger.ContactList.ListEnumerator contactList)
  {
   lst.Clear();
   foreach( Contact contact in contactList )
   {
    ListViewItem item = new ListViewItem(contact.Name + " status: " + contact.Status.ToString());
    item.Tag = contact;
    lst.Items.Add(item);
   }
  }
  //刷新联系人列表
  private void RefreshListView(ListView lst, Contact contact)
  {
   foreach( ListViewItem item in lst.Items)
   {
    Contact itemtag = (Contact)item.Tag;
    if( itemtag.Mail.Equals(contact.Mail) )
    {
     item.Text = contact.Name + " status: " + contact.Status.ToString() ;
     return;
    }
   }
//   ListViewItem newItem = new ListViewItem(contact.Name + " status: " + contact.Status.ToString());
//   newItem.Tag = contact;
//   lst.Items.Add(newItem);
  }
  //加入对话
  private void OnConversationCreated(Messenger sender, ConversationEventArgs e)
  {
   txtLog.Text = "ConversationCreated/r/n" + txtLog.Text;
   e.Conversation.ContactJoin += new DotMSN.Conversation.ContactJoinHandler(ContactJoin);
   e.Conversation.MessageReceived += new DotMSN.Conversation.MessageReceivedHandler(MessageReceived);
   e.Conversation.UserTyping += new DotMSN.Conversation.UserTypingHandler(UserTyping);
  }
  #region 在标题栏显示联系人Email
  private void lstViewFL_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   DisplayMail( (ListView)sender );
  }
  private void lstViewRL_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   DisplayMail( (ListView)sender );
  }
  private void lstViewAL_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   DisplayMail( (ListView)sender );
  }
  private void lstViewBL_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   DisplayMail( (ListView)sender );
  }
  private void DisplayMail(ListView lst)
  {
   if( lst.SelectedItems.Count == 1 )
   {
    Contact contact = (Contact)lst.SelectedItems[0].Tag ;
    this.Text = contact.Mail;
   }
  }
  #endregion
  //加入对话
  private void ContactJoin(Conversation sender, ContactEventArgs e)
  {
   txtLog.Text = e.Contact.Name + " " + e.Contact.Mail + " " + "ContactJoin/r/n" + txtLog.Text;
  }
  //收到消息
  private void MessageReceived(Conversation sender, MessageEventArgs e)
  {
   txtLog.Text = e.Sender.Name + " " + e.Sender.Mail + " " + e.Message.Text + "/r/n" + txtLog.Text;
   sender.SendMessage( e.Message.Text );
  }
  //对方输入
  private void UserTyping(Conversation sender, ContactEventArgs e)
  {
   txtLog.Text = e.Contact.Name + " " + e.Contact.Mail + " " + "is Typing/r/n" + txtLog.Text;
  }

这里是部分代码完整的工程可以点击这里下载,这个机器人现在还只能鹦鹉学舌,但只要修改一下 MessageReceived 就可以让他变得很聪明了。甚至让列表中的人教它回答问题。只要你有创意。

0 0

相关博文

我的热门文章

img
取 消
img