Keyword based routing


Sometimes you might want to control the processing of incoming email messages by specific keywords found in the subject or the email's body text.

Please start todo4teams and log in with the role "admin" or "superadmin" - depending on your user right. As an administrator you need to have the rights to configure email boxes in todo4teams.

Select the tab "Email" and click on the table row of the email address you want to configure. Click on "Edit" to open the configuration dialog.

Now select the tab "receive action" to edit the receive script in the upper text pane.

Preliminary Notes

The receive script carries some important objects in it's execution context (as scripting variables) which you can read/write by means of your script:

  • task: The ticket object which you might want to update depending on the email properties
  • message: The email from which the ticket was created. When your script is run the ticket (task) is already filled and configured as defined in the "basic data" tab of your email configuration.
  • coretext: This character string variable is a concatenation of all text parts of the message received. The incoming email might contain different parts of text - plain text and html formatted parts. coretext contains all parts and is perfectly suited to be checked for keywords. So you don't have to care about checking different MIME parts of the email.
  • helper: This is an auxiliary object that provides a set of useful functions e.g. to change the addressing of a task - which we will use here.

The Script

With the set of variables described above it is straight forward to check the incoming email for a specific keyword. Let's say we want to treat messages containing the word "salesforce" in a special way. Messages containig this keyword should be routed to a group which differs from the one configured in the "basic data" tab (see option "address to").

This piece of code will add the phrase "Salesforce: " the task title and route the ticket to the group named "Salesforce":


if(coretext.toLowerCase().indexOf("salesforce")>-1){
 task.title="Salesforce: "+task.title;   
 helper.routeToGroupByName("Salesforce");
}

The script converts the content of coretext to lowercase first and searches it for the word salesforce afterwards using the indexOf-function.  indexOf will return the position of salesforce if found and -1 else. Thus the code within the curly braces is executed if salesforce  was found in coretext.  

The helper-method routeToGroupByName will alter the task's routing and send it to the group named "Salesforce". To let your code work as desired please make sure that a group/skill with this name exists and is marked as active in your installation. You may also use the method routeToGroup and write the group's id as the parameter to make your code robust against renaming groups, e.g.

helper.routeToGroup(17);

Copy the code above (the four liner) into the script pane in the tab "Receive action", modify the keywords and workgroup name as needed, save the configuration and try your script by sending emails to the email address in question.

If you want to solely check the email's subject for the keyword try the following code:    

if(message.getSubject().indexOf("Alarm:")>-1){
    task.title="Alarm: "+task.title;
   // ... do something special with this type of message
}

If you want to search for more than on keyword try the following:
if(message.getSubject().indexOf("Alarm:")>-1 ||
   message.getSubject().indexOf("Emergency:")>-1 ||
   message.getSubject().indexOf("Important:")>-1 ){
    task.title="Alarm: "+task.title;
   // ... do something special with this type of message
}

Here the subject is checked for "Alarm:", "Emergency:" and "Important:" and the results are combined using the "or" operation which is written as "||" in javascript.

     

Child Pages