Project MSNBot
From Frederick Chapleau Wiki
Contents |
Great, but... why?
Hum, let me think, I want to talk to my website... ! I just wanted to have quick interaction between some section of my website, without having to develop an entire chat/admin client. So, the website bot is "talking" to me, and asking me questions, like "Do you want to give access to xxxxxxx for the section yyyyy ? [yes|no]".
Also I can talk to it like "Change display picture to webaddress" or "Change personal message to 'I'm now working on zzzzz'".
How does it work
The Components
The new MSNP Sharp component, that is compliant with the lastest MSNP15 protocol. http://code.google.com/p/msnp-sharp/
Bot options behavior
Simple methods to get the personnal message of some contacts, the status, and even the display picture. The bot is a singleton and can is automatically connecting the bot based on a predefined account, using a predefined display picture.
The Code
Controller
public class Controller { private Controller() { } private static Controller _instance; public static Controller Instance { get { if (_instance == null) _instance = new Controller(); return _instance; } } private Messenger _messenger; private string _botEmail; private string _displayImage; private Thread _thAsyncUpdate; public void Initialize(string botEmail, string displayImage) { _botEmail = botEmail; _displayImage = displayImage; } public Messenger Messenger { get { if (_messenger == null) { _messenger = new Messenger(); _messenger.Credentials = new Credentials(_botEmail, "thepassword", "msmsgs@msnmsgr.com", "Q1P7W2E4J9R8U3S5"); _messenger.Nameserver.SynchronizationCompleted += new EventHandler(Nameserver_SynchronizationCompleted); _messenger.Nameserver.SignedIn += new EventHandler(Nameserver_SignedIn); _messenger.Nameserver.ReverseAdded += new ContactChangedEventHandler(Nameserver_ReverseAdded); _thAsyncUpdate = new Thread(new ThreadStart(AsyncUpdate)); _thAsyncUpdate.IsBackground = true; _thAsyncUpdate.Start(); } return _messenger; } } private void AsyncUpdate() { while (true) { foreach (Contact selectedContact in Messenger.ContactList.Allowed) { if (selectedContact.Status != PresenceStatus.Offline && selectedContact.DisplayImage != null) { RetreiveImage(selectedContact); } } Thread.Sleep(60000); } } private void RetreiveImage(Contact selectedContact) { MSNSLPHandler msnslpHandler = Messenger.GetMSNSLPHandler(selectedContact.Mail); P2PTransferSession session = msnslpHandler.SendInvitation(Messenger.Owner.Mail, selectedContact.Mail, selectedContact.DisplayImage); session.TransferFinished += new EventHandler(Session_TransferFinished); session.ClientData = selectedContact.DisplayImage; } void Session_TransferFinished(object sender, EventArgs e) { P2PTransferSession session = (P2PTransferSession)sender; DisplayImage image = (DisplayImage)session.ClientData; image.RetrieveImage(); } void Nameserver_ReverseAdded(object sender, ContactEventArgs e) { _messenger.Nameserver.ContactService.AddNewContact(e.Contact.Mail); } public void Connect() { Messenger.Connect(); } private void Nameserver_SignedIn(object sender, EventArgs e) { } private void Nameserver_SynchronizationCompleted(object sender, EventArgs e) { _messenger.Owner.Status = PresenceStatus.Online; Image fileImage = Image.FromFile(_displayImage); DisplayImage displayImage = new DisplayImage(); displayImage.Image = fileImage; _messenger.Owner.DisplayImage = displayImage; } public string AskQuestion(string email, string question) { return AskQuestion(email, question, 15000); } public string AskQuestion(string email, string question, int timeout) { Contact msnContact = Messenger.ContactList[email]; msnContact.OnAllowedList = true; msnContact.OnForwardList = true; StandAloneConversation sac = new StandAloneConversation(Messenger, msnContact); return sac.SendAndReceive(question, timeout); } public void SetPersonnalMessage(string message) { Messenger.Owner.PersonalMessage = new PersonalMessage(message, MediaType.None,null); } public string GetPersonnalMessage(string p) { if (Messenger != null && Messenger.ContactList != null && Messenger.ContactList[p] != null && Messenger.ContactList[p].PersonalMessage != null) return Messenger.ContactList[p].PersonalMessage.Message; else return "n/a"; } public PresenceStatus GetStatus(string p) { return Messenger.ContactList[p].Status; } public Image GetDisplayImage(string p) { Contact selectedContact = Messenger.ContactList[p]; // request the image, if not already available if (selectedContact.Status != PresenceStatus.Offline && selectedContact.DisplayImage != null) { if (selectedContact.DisplayImage.Image == null) RetreiveImage(selectedContact); else return selectedContact.DisplayImage.Image; } return null; } }
Standalone Conversation
public class StandAloneConversation { private Messenger _messenger; private Contact _msnContact; private string _question; private string _response; private Conversation _conv; public StandAloneConversation(Messenger messenger, Contact msnContact) { _messenger = messenger; _msnContact = msnContact; } public string SendAndReceive(string question, int timeout) { DateTime start = DateTime.Now; TimeSpan elapsed = new TimeSpan(0); _question = question; _conv = _messenger.CreateConversation(); _conv.Switchboard.TextMessageReceived += new TextMessageReceivedEventHandler(Switchboard_TextMessageReceived); while (!_conv.Switchboard.IsSessionEstablished && elapsed.TotalMilliseconds < timeout) { elapsed = (DateTime.Now - start); Thread.Sleep(100); } if (!_conv.Switchboard.IsSessionEstablished) return string.Empty; _conv.Invite(_msnContact); Thread.Sleep(500); _conv.Switchboard.SendTextMessage(new TextMessage(_question)); _response = string.Empty; while (_response.Length == 0 && elapsed.TotalMilliseconds < timeout) { elapsed = (DateTime.Now-start); Thread.Sleep(100); } _conv.SwitchboardProcessor.Disconnect(); return _response; } private void Switchboard_TextMessageReceived(object sender, TextMessageEventArgs e) { _response = e.Message.Text; } public void Send(string text) { Conversation conv = _messenger.CreateConversation(); if (conv.SwitchboardProcessor.Connected == false) { conv.Messenger.Nameserver.RequestSwitchboard(conv.Switchboard, this); } conv.Switchboard.TextMessageReceived += new TextMessageReceivedEventHandler(Switchboard_TextMessageReceived); conv.SwitchboardProcessor.Connect(); conv.Invite(_msnContact); conv.Switchboard.SendTextMessage(new TextMessage(text)); conv.SwitchboardProcessor.Disconnect(); } }

