How To: Create Windows Live Messenger Addin
How To : Create Windows Live Messenger Addin
This is my first post in a series of posts about Addins.
Next : How To : Create SQL Server Management Studio Addin
Let’s Start":
Messenger Addins are not enabled by default, so to enable Addins for Messenger -
Go to HKCU\SOFTWARE\Microsoft\MSNMessenger and add a DWORD value named AddInFeatureEnabled set to 1
Now launch Windows Live Messenger, sign in and go to Tools, Options, Add-ins. You should see this:
The Basics of Live Messenger add-ins
Windows Live Messenger add-ins are written using a managed library called MessengerClient.dll (can be found in %programfiles%\Windows Live\Messenger) which holds the namespace Microsoft.Messenger.
In order to create an add-in, open up Visual Studio 2005/8, create a new Class Library project (e.g. called "MyAddin") and add a reference to the MessengerClient.dll file:
Next, change the project properties so that the assembly name equals the (fully qualified) class name of the add-in. This is important to get the add-in to work. In my case the class is defined as follows:
namespace MessengerAddin
{
public class MyAddin
{
So, we need the following assembly name:
Next, import the namespace Microsoft.Messenger and implement the IMessengerAddin interface:
using Microsoft.Messenger;
namespace MessengerAddin
{
public class MyAddin : IMessengerAddIn
{
private MessengerClient messenger;
public void Initialize(MessengerClient messenger)
{
this.messenger = messenger;
}
}
}
Example : Create New Status
namespace MessengerAddin
{
public class MyAddin : IMessengerAddIn
{
private MessengerClient messenger;
public void Initialize(MessengerClient messenger)
{
this.messenger = messenger;
messenger.AddInProperties.Creator = "Shai Raiten";
messenger.AddInProperties.Description = "This Is My Addin";
messenger.AddInProperties.FriendlyName = "MyAddin";
messenger.AddInProperties.PersonalStatusMessage = "Greetings from MyAddin";
messenger.AddInProperties.Status = UserStatus.Unknown;
messenger.AddInProperties.Url = new Uri("http://blogs.microsoft.co.il/blogs/shair");
}
}
}
Now compile the project and go to Windows Live Messenger. Load the add-in via Tools, Options, Add-ins:
Now you should see something like this:
Click OK to continue. In the Windows Live Messenger main window, click your nickname and choose "Turn on "MyAddin"":
Example : Auto Responder
public void Initialize(MessengerClient messenger)
{
this.messenger = messenger;
messenger.OutgoingTextMessage += new EventHandler<OutgoingTextMessageEventArgs>
(messenger_OutgoingTextMessage);
}
Again, the event handler can be stubbed automatically, but of course you need to supply some additional code:
void messenger_IncomingTextMessage(object sender, IncomingTextMessageEventArgs e)
{
string s = e.TextMessage;
StringBuilder sb = new StringBuilder();
for (int i = s.Length - 1; i >= 0; i--)
sb.Append(s[i]);
string reverse = sb.ToString();
messenger.SendTextMessage(reverse, e.UserFrom);
}
Example : Birthday Counter + Settings
public class MyAddin: IMessengerAddIn
{
private MessengerClient messenger;
private System.Timers.Timer tmr;
private DateTime birthday;
public void Initialize(MessengerClient messenger)
{
this.messenger = messenger;
messenger.ShowOptionsDialog += new EventHandler(messenger_ShowOptionsDialog);
tmr = new System.Timers.Timer(5000);
tmr.Elapsed += new System.Timers.ElapsedEventHandler(tmr_Elapsed);
StartTimer();
}
private void StartTimer()
{
if (DateTime.TryParse(messenger.SavedState, out birthday))
tmr.Start();
}
void tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
TimeSpan span = DateTime.Now - birthday;
messenger.AddInProperties.PersonalStatusMessage = String.Format("{0} days {1} hours {2} minutes {3} seconds on planet Earth", span.Days, span.Hours, span.Minutes, span.Seconds);
}
private void messenger_ShowOptionsDialog(object sender, EventArgs e)
{
tmr.Stop();
SettingsDialog settings = new SettingsDialog(birthday);
if (settings.ShowDialog() == DialogResult.OK)
messenger.SavedState = settings.Birthday.ToShortDateString();
StartTimer();
}
}
For the Settings Dialog create new windows application called : SettingsDialog
2 Buttons (OK & Cancel)
DateTimePicker
public partial class SettingsDialog : Form
{
DateTime birthday;
public SettingsDialog(DateTime m_birthday)
{
InitializeComponent();
birthday = m_birthday;
if (birthday != null)
this.birthday = DateTime.Now;
}
private DateTime bd;
public DateTime Birthday
{
get { return bd; }
set { bd = value; }
}
private void btnOK_Click(object sender, EventArgs e)
{
this.bd = birthday;
this.DialogResult = DialogResult.OK;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
Time to try out. Usual steps again: exit Live Messenger, compile, start Live Messenger and enable the add-in. Go to Tools, Options, Add-ins and look at the enabled Settings button now:
When you click it, you'll see the dialog popping up (unfortunately somewhere in the back - have to check this little issue) with a security warning (caused by the CAS feature):
Ignore this for now (you can solve it by strong-naming the add-in and some good deal of CAS knowledge; add-ins in Live Messenger are sandboxed by putting them in the Internet security zone) and set your birthday:
Windows Live Messenger will now start counting the days, hours, minutes and seconds we've spent so far on our lovely planet:
Warning: This might cause a relatively big amount of internet traffic!