Saturday, March 2, 2019

NICE inContact WorkItem Routing


Summary

I'll demonstrate the use of the NICE inContact Work Item API in this post.  That API provides the ability to bring non-native tasks into the routing engine.  The 'workitem' to be routed will be email from Google Gmail.  I'll cover the use of the Gmail API in addition to WorkItem.

This is a contrived example with, at best, demo-grade code.

Overall Architecture

Below is a depiction of the overall set up of this exercise.  Emails are pulled from Gmail via API, submitted to InContact's routing engine via API, then delivered to an agent target via a routing a strategy.


Application Architecture

Below is the application layout.  

Routing Logic

Picture of the routing strategy used for this demo.  This a very simple strategy that pulls a work item from queue and then pops a web page with the email contents to an agent.


Code Snippets


Main Procedure

Code below performs the OAuth2 handshake with Google, gets a list of messages currently in the Inbox, then packages up an array of Promises each of which pulls message contents from Gmail and submit them to the Work Item API.
function processMessage(id) {
 return gmail.getMessage(id)
 .then(msg => {
  return workItem.sendEmail(msg.id, msg.from, msg.payload);
 })
 .then(contactId => {
  return {msgId : id, contactId : contactId};
 });
}

gmail.authorize()
.then(_ => {
 return gmail.getMessageList();
})
.then((msgs) => {
 let p = [];
 msgs.forEach(msg => {
  p.push(processMessage(msg.id));
 });
 return Promise.all(p);
})
.then(results => {
 console.log(results);
})
.catch((err) => {
 console.log(err);
});

Gmail GetMessageList and GetMessage

getMessageList() pulls a list of Gmail IDs currently with the INBOX label.  getMessage() pulls the content of a message for a given Gmail ID.

 getMessageList() {
  console.log('getMessageList()');
  const auth = this.oAuth2Client;
  const gmail = google.gmail({version: 'v1', auth});
  return new Promise((resolve, reject) => {
   gmail.users.messages.list({userId: 'me', labelIds: ['INBOX']}, (err, res) => {
    if (err) {
     reject(err);
    }
    resolve(res);
   });
  })
  .then((res) => {
   return res.data.messages;
  })
  .catch((err) => {
   console.error('getMessageList() - ' + err.message);
   throw err;
  });
 }


 getMessage(id) {
  console.log('getMessage() - id: ' + id);
  const auth = this.oAuth2Client;
  const gmail = google.gmail({version: 'v1', auth});
  return new Promise((resolve, reject) => {
   gmail.users.messages.get({userId: 'me', 'id': id}, (err, res) => {
    if (err) {
     reject(err);
    }
    resolve(res);
   });
  })
  .then((res) => {
   const id = res.data.id;
   const arr = (res.data.payload.headers.find(o => o.name === 'From')).value
   .match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
   let from;
   if (arr) {
    from = arr[0];
   }
   else {
    from = '';
   }
   let payload = '';
   if (res.data.payload.body.data) {
    payload = atob(res.data.payload.body.data);
   }
   return {id: id, from: from, payload: payload};
  })
  .catch((err) => {
   console.error('getMessage() - ' + err.message);
   throw err;
  });
 }


WorkItem API Call

This consists of a simple POST with the workItem params in the body.

 postWorkItem(workItemURL, token, id, from, payload, type) {
  console.log('postWorkItem() - url: ' + workItemURL + ' from: ' + from);
  const body = {
    'pointOfContact': this.poc,
    'workItemId': id,
    'workItemPayload': payload,
    'workItemType': type,
    'from': from
  };
 
  return fetch(workItemURL, {
   method: 'POST',
   body: JSON.stringify(body),
   headers: {
    'Content-Type' : 'application/json', 
    'Authorization' : 'bearer ' + token
   },
   cache: 'no-store',
   mode: 'cors'
  })
  .then(response => {
   if (response.ok) {
    return response.json();
   }
   else {
    const msg = 'response status: ' + response.status;
    throw new Error(msg);
   }
  })
  .then(json => {
    return json.contactId;
  })
  .catch(err => {
   console.error('postWorkItem() - ' + err.message);
   throw err;
  });
 }

Results


Source

Full source w/comments here: https://github.com/joeywhelan/workitem

Copyright ©1993-2024 Joey E Whelan, All rights reserved.