DEVELOPERS BLOG

Long Email Threads: Be Gone!!

BBM Enterprise SDK is now BlackBerry Spark Communications Platform. Learn more, and download it for free at BlackBerry.com/Spark.

We’ve all been on those long email threads that don’t seem to ever end. We’ve all thought to ourselves: “this is taking way too long to resolve…” or “surely there is a better way !!! “.

Wouldn’t it be great if you can click a button to convert that long email thread to a group chat? Well that wish can become a reality for you with the BlackBerry Spark Communications Platform. You can bring a rich and highly secure messaging experience to your employees, and easily integrate it into your corporate directory powered by Microsoft Azure Active Directory (Azure AD).

As we’ve previously mentioned, all sample applications (and that’s Android, iOS, and JavaScript) that come bundled up with Spark can integrate with Microsoft Azure Active Directory for Identity and User Management. We demonstrated how you can start a chat with one or more users from your Azure AD backed corporate directory. It’s quite simple, all you need is your users’ BBM regIds, and those can be retrieved from your Active Directory using the Microsoft Graph APIs:

Microsoft Graph APIs:

//Lookup the regId for a user ID.
//This assumes that your app will store the regId for each user when they first sign in.
//https://developer.microsoft.com/en-us/graph/docs/concepts/extensibility_open_usersvar getRegIdForUser = function(userId, accessToken) {
    return new Promise(function(resolve, reject) {
    var httpRequest = new XMLHttpRequest();
    httpRequest.onreadystatechange = function() {
      if (httpRequest.readyState === 4) {
        if (httpRequest.status === 200 && httpRequest.response.extensions && 
httpRequest.response.extensions.length > 0
          && httpRequest.response.extensions[0].id ===
 "com.bbm.sdk.example.azure.userValues"
          && httpRequest.response.extensions[0].regId) {
          //Get the regId that was stored in the Active Directory for that user
          resolve(httpRequest.response.extensions[0].regId);
        } else {
          reject();
        }
      }
    };    httpRequest.responseType = "json";
    //get the extensions for that user by passing the $expand=extensions param
    //https://developer.microsoft.com/en-us/graph/docs/concepts/query_parameters#expand-parameter
    httpRequest.open("GET",
 "https://graph.microsoft.com/v1.0/users/"+userId+"?$select=id,displayName&$expand=extensions");
    httpRequest.setRequestHeader('Authorization', 'Bearer ' + accessToken);
    httpRequest.send();
  });
};

Once you have your users’ regIds, you can simply call the Spark API to start a chat:

 

//Start a chat
bbmsdk.messenger.chatStart({
        invitees: regIds,
        isOneToOne: false,
        subject: chatSubject,
        invitePolicy: BBMEnterprise.Messenger.Chat.InvitePolicy.ParticipantsOnly
      });

Code Snippet – JavaScript

Starting a chat from an Active Directory Group

There are multiple ways you can discover users to start a chat with. Your messaging application – powered by Spark – can directly integrate into your corporate directory. You can select users individually from the corporate directory to start a group chat, or you can start a group chat with an Active Directory Group. This can come in handy when you want to start a chat with the “support team” for example about a customer issue, but you don’t know who are the right people to contact.

The Microsoft Graph APIs provide several ways to find users in a group, one simple way is using the List members.

//Get the user IDs for the members of an Azure Active Directory group.
var getUserIdsOfGroup = function(groupId, accessToken) {
  return new Promise(function(resolve, reject) {
    var httpRequest = new XMLHttpRequest();
    httpRequest.onreadystatechange = function() {
      if (httpRequest.readyState === 4) {
        if (httpRequest.status === 200) {
          //The response.value will be an array of user objects
          //https://developer.microsoft.com/en-us/graph/docs/api-
reference/v1.0/resources/user#properties
          //return array with the user ID for each member of the group
          resolve(httpRequest.response.value.map(function(member) {return member.id;}));
        } else {
          reject();
        }
      }
    };
    httpRequest.responseType = "json";
    //https://developer.microsoft.com/en-us/graph/docs/api-
reference/v1.0/api/group_list_members
    httpRequest.open("GET",
"https://graph.microsoft.com/v1.0/groups/"+groupId+"/members");
    //need to pass the access token from authentication with Azure. It must have a scope
    //with required permissions to access the Microsoft Graph API
    //https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/group_list_members#permissions
    httpRequest.setRequestHeader('Authorization', 'Bearer ' + accessToken);
    httpRequest.send();
  });
};

Once you’ve got the list of users, you can retrieve their BBM regIDs as we’ve shown before, and then start a chat.

//Start a chat will all members in a group that have a regId
var startChatForGroup = function(bbmsdk, accessToken, groupId, chatSubject) {
  return getUserIdsOfGroup(groupId, accessToken).then(function(userIds) {
    //Create an of promises to lookup the regId for each user ID
    var regIdPromises = userIds.map(function(userId) {
      return getRegIdForUser(userId, accessToken).catch(function()
 {console.log("Skipping user without regId. userId="+userId)});
    });
     //lookup for all users
    return Promise.all(regIdPromises)
      .then(function(regIds) {
      //start the chat for those regIds
      //https://developer.blackberry.com/files/bbm-
enterprise/documents/guide/reference/javascript/BBMEnterprise.Messenger.html#chatStart
      return bbmsdk.messenger.chatStart({invitees: regIds.filter(function(regId) {
          //Filter out the ones that were not found and the regId for the local user
          return regId && regId !== bbmsdk.getRegistrationInfo().regId;
        }),
        isOneToOne: false, subject: chatSubject,
        invitePolicy: BBMEnterprise.Messenger.Chat.InvitePolicy.ParticipantsOnly
      });
    });
  });
};

Turning those Pesky Email Threads into a Group Chat

Now for those pesky email threads. All we need to do is parse the addresses in an email and then use the Microsoft Graph APIs to lookup users and retrieve their BBM regID to start a group chat. You can bundle up this functionality into an Outlook add-in that you rollout to your entire enterprise. For more information about building your own Outlook add-in, check out this overview.

To learn more about embedding real-time communications into your apps and services, visit the BlackBerry Spark Communications Platform Website.

BlackBerry

About BlackBerry

BlackBerry is an enterprise software and services company focused on securing and managing IoT endpoints.