Initiate a Skype chat from a Windows app

This post will show how to initiate a chat using Skype from a Windows app.

Background

I am working on a team project that needs to add a bunch of functionality such as sharing to social networks and initiating discussion with others.  We have a very limited amount of time to build this functionality ourselves.  Rather than code everything by hand, we are taking advantage of the functionality in Windows apps to do the heavy lifting for us.  For instance, rather than build in all the logic to post to Twitter or Facebook or send an email ourselves, we can instead leverage sharing in apps to share information to other Windows apps.  Similarly, we did not want to build a new instant messaging app ourselves, instead we wanted to use what the platform provides out of box. 

Finding Contacts

The first part of the problem is trying to figure out how to contact a user.  In our scenario, we want to initiate a conversation with our existing contacts.  Windows 8x provides a very easy way to find contacts using the ContactPicker class.

Code Snippet

  1. //Select the contact using a registered contact picker
  2. //By default, this launches the People app.
  3. ContactPicker picker = new ContactPicker();         
  4. var contact = await picker.PickContactAsync();

When this code is run, you can now choose from your available contacts in Windows.  If you have connected to LinkedIn, Facebook, or Twitter, your contacts from those networks will appear.  Select one, and choose OK, and the contact information is returned to your app.

image

Want to select contacts from another source?  You can also provide your own contact picker implementation.  There is a Contact Picker app sample that shows how you could provide your own contact picker to select contacts from another system, perhaps from O365 or your own system. 

Got Skype?

The next part of the equation is determining if your contact has a Skype account or not.  One of the properties of the Contact class is ConnectedServiceAccounts, which will let you determine if the contact has an associated Skype account.  If we determine the user has a Skype account associated with their contact information, we can launch the Skype app.

Code Snippet

  1. //Select the contact using a registered contact picker
  2. //By default, this launches the People app.
  3. ContactPicker picker = new ContactPicker();         
  4. var contact = await picker.PickContactAsync();
  5.  
  6. //Contact will be null if they cancel without selecting
  7. if(null != contact)
  8. {
  9.     //Inspect service accounts
  10.     var accounts = contact.ConnectedServiceAccounts;
  11.     ContactConnectedServiceAccount skypeAccount =
  12.         accounts.Where(i => i.ServiceName.Equals("skype.com")).FirstOrDefault();
  13.  
  14.     //If the contact has a Skype account in their contact information
  15.     if (null != skypeAccount)
  16.     {
  17.         //Launch Skype to start a chat session
  18.         Uri appUri = new Uri("skype:" + skypeAccount.Id + "?chat");
  19.         var viewSize = Windows.UI.ViewManagement.ViewSizePreference.UseMore;
  20.         await Windows.System.Launcher.LaunchUriAsync(appUri,
  21.             new Windows.System.LauncherOptions
  22.             {
  23.                 DesiredRemainingView = viewSize
  24.             });                    
  25.     }    
  26.     else
  27.     {
  28.         MessageDialog d = new MessageDialog("The selected contact does not have a Skype account associated in their contact information.");
  29.         await d.ShowAsync();
  30.     }
  31. }

When the app is run, the value of “skypeAccount.Id” on line 18 will be their Skype ID.  We request the amount of screen real estate by using the LauncherOptions class.  The result is Skype is launched using snap view, and our app takes the remaining available real estate on the screen. 

image

More Skype options

There are many different options for initiating a conversation such as opening or creating a chat, as we did by appending the “?chat” querystring to the URI.  If we omit the querystring, the default is to call the user.  You could also initiate a video call.  The various options are documented in the Skype URI API Reference.

Another option would be to initiate a call using the contact’s phone number instead of their Skype ID.  Simply replace the “skypeAccount.Id” on line 18 above with their phone number, and Skype will initiate a call with the contact.  You could also initiate a conversation with the user with their email address.  There are lots of options now that you see how easy this is.

For More Information

Skype URI API Reference

Build apps that connect with People and Calendar, Part 1: Contact Cards

Contact Picker app sample