Kwerty Messenger Library

Kwerty Messenger Library is a .NET class library enabling connectivity with the .NET Messenger service. You can use Kwerty Messenger Library as the foundation for your own custom MSN Messenger/Windows Live Messenger client or bot.

Its API is clean and for the most part self explanatory. It uses the MSNP9 protocol to communicate with the .NET Messenger service over TCP/IP. It's fully asynchronous and uses the async/await keywords introduced in the .NET Framework 4.5 Async CTP. It also uses Reactive Extensions at its core for handling incoming commands.

Microsoft recently announced they would be shutting down Windows Live Messenger and doing a forced migration to Skype. They have been quite vague about whether they will be shutting down the messenger servers so the future of this library is uncertain. Supposedly MSN Messenger will continue to operate in China so it will be interesting to see if this library can still be used there.

Regardless of whether the servers shut down, this library serves as an excellent resource for programmers looking to familiarize themselves with socket programming, the Async CTP, or Reactive Extensions.

Grab the source code on GitHub

Below are some examples demonstrating its basic usage. There is a console app included in the source code which covers the rest.

Logging in

 
Credentials credentials = new Credentials("you@hotmail.com", "yourpassword");

MessengerClient msgr = new MessengerClient(credentials);

await msgr.LoginAsync();		

Changing your nickname

 
await msgr.LocalUser.ChangeNicknameAsync("This is my new name");

Changing your status

 
await msgr.LocalUser.ChangeStatusAsync(UserStatus.Away);

User changes status

 
msgr.UserStatusChanged += new EventHandler<UserStatusEventArgs>((sender, e) =>
{
	if (e.Status == UserStatus.Online)
		Console.WriteLine("{0} is now online", e.User.LoginName);
});

User changes nickname

 
msgr.UserNicknameChanged += new EventHandler<UserNicknameEventArgs>((sender, e) =>
{
	Console.WriteLine("User {0} nickname changed to {1}", e.User.LoginName, e.User.Nickname);
});

Add a user to your contact list

 
User user = msgr.GetUser("somebody@hotmail.com");

//all users must be added to reverse list
await msgr.UserLists.ReverseList.AddUserAsync(user);

//forward list is your list of contacts
await msgr.UserLists.ForwardList.AddUserAsync(user);

//allow list is the list of users allowed to see you online
await msgr.UserLists.AllowList.AddUserAsync(user);

Create a group and add a user to it

 
Group group = await msgr.CreateGroupAsync("My mates");

await group.AddUserAsync(user);

Block a user

 
await msgr.UserLists.BlockList.AddUserAsync(user);

User added to a list

 

msgr.UserAddedToList += new EventHandler<UserListUserEventArgs>((sender, e) =>
{

	if (e.UserList == msgr.UserLists.ForwardList)
	{
		Console.WriteLine("{0} has been added to the forward list", e.User.LoginName);
	}

});

Start an instant messaging session

 
User user = msgr.GetUser("somebody@hotmail.com");

IMSession imSession = await msgr.CreateIMSession();

imSession.UserJoined += new EventHandler<UserEventArgs>((sender, e) => {
	Console.WriteLine("User was invited and has joined");
});

await imSession.InviteUserAsync(user);

Send a message

 
Message message = new Message();

message.Body = UTF8Encoding.UTF8.GetBytes("Hello my friend");

await imSession.SendMessageAsync(message);

Send a message with custom formatting

 
Message message = new Message();

MessageFormatter formatter = new MessageFormatter();
formatter.SetRandomColor();
formatter.Bold = true;
formatter.ApplyFormat(message);

message.Body = UTF8Encoding.UTF8.GetBytes("Wow look at this");

await imSession.SendMessageAsync(message);

Accepting an invitation to an instant messaging session

 
msgr.InvitedToIMSession += new EventHandler<InvitationEventArgs>(async (sender, e) =>
{
	IMSession imSession = await Msgr.AcceptInvitationAsync(e.Invitation);
});	

Receiving messages

 
imSession.MessageReceived += new EventHandler<MessageEventArgs>((sender, e) =>
{

	if (e.Message.Headers.ContainsKey("TypingUser"))
	{
		Console.WriteLine("{0} is typing", e.Sender.LoginName);
	}
	else if (e.Message.ContentType == "text/plain; charset=UTF-8")
	{
		Console.WriteLine("{0} says: {1}", e.Sender.LoginName, Encoding.UTF8.GetString(e.Message.Body));
	}

});

Changing privacy settings

 
msgr.SetPrivacySettingAsync(PrivacySetting.AcceptInvitations, MessengerClient.AcceptInvitationsFromAllUsers);

Phone numbers

 
await msgr.LocalUser.SetPropertyAsync(UserProperty.HomePhone, "555-1505");

string mobilePhone = user.GetProperty(UserProperty.MobileDevice);

Capabilities

 
var capabilities = UserCapabilities.RendersGif | 
		UserCapabilities.SupportsWinks |
		UserCapabilities.SupportsVoiceIM |
		UserCapabilities.HasSpace;

await Msgr.LocalUser.ChangeCapabilitiesAsync(capabilities);	

Unexpected logout

 
msgr.LoggedOut += new EventHandler<LoggedOutEventArgs>((sender, e) => {

	if (e.Reason == LogoutReason.LoggedInElsewhere)
		Console.WriteLine("You logged in at another location");

	else if (e.Reason == LogoutReason.ConnectionError)
		Console.WriteLine("Logged out due to a connection error: " + e.ConnectionError.Message);

});

Many thanks to the two websites below, I couldn't have done it without you

← Back to main page

Fork me on GitHub