SugarCRM and Asterisk integration in Java ( II )

  Asterisk, Sugarcrm

Opening a customer SugarCRM page in the browser on incoming calls

Introduction

Here comes the really interesting part, as this is what most readers have been asking for lately. The feature lots of people seem to be interested in is the possibility of , on an inbound asterisk call,  opening/redirecting the browser to the specific account/contact SugarCRM page. Before we proceed you should first have a look at two of our previous articles:

In particular, we’ll take the code developed in the first one and add some features, while the second one is useful to see how we’re going to cope with SugarCRM authentication.
As usual acknowledgments are due to the authors of  AsteriskJava for the great job they are doing with their library.


The code

In the first article we’ve written two java classes: “PhoneNumberResolver”, which is our main class and the listener for asterisk events, and “AccountFinder” which is in charge of querying SugarCRM for the Account owning the caller phone number for our inbound call. At the end of the article we were able to get a caller id from an asterisk event in the method “onManagerEvent” of our “PhoneNumberResolver” class, pass the caller id to the “searchAccountByNumber” method of our class “AccountFinder”, obtaining back an hashtable with data eventually extracted from Sugarcrm and show them.
What we are going to do now, is to create a new method “browserToSugarCRM” in our “AccountFinder” class. Again we’ll pass it the caller id and it will again check if a corresponding Account exists in SugarCRM , and if so it will open our browser to the specific SugarCrm page. Here is our new method:

   1:    public void browserToSugarCRM(String phoneNumber){
   2:        try{
   3:            Set_entry_result loginRes=port.login(userAuth, "myAppName");
   4:            String sessionID = loginRes.getId();
   5:            port.seamless_login(sessionID);
   6:            Get_entry_list_result entryList=port.get_entry_list(sessionID,"Accounts","accounts.phone_office='" + 
   7:                                        phoneNumber + "'", "",0,new String[]{"id","name","phone_fax","website"}, 
   8:                                               1, 0);
   9:            if(entryList.getEntry_list().length>0){
  10:                Entry_value entry = (entryList.getEntry_list())[0];
  11:                Name_value[] nvl=entry.getName_value_list();
  12:                String value = nvl[0].getValue();
  13:                String[] commandLine= {"/usr/bin/firefox","http://your_sugar_server_url/index.php?action=DetailView
                                                             &module=Accounts&record="+value+ "&MSID="+ sessionID};
  14:                Process process = Runtime.getRuntime().exec(commandLine);          
  15:            }
  16:        } catch(Exception ex){
  17:            ex.printStackTrace();
  18:        }
  19:    
  20:    }
  21: 


The method used here is explained in the second article cited above. We do a SOAP login to SugarCRM and get a session id (lines 3 and 4). then we use the session id to do a “seamless login”(line 5) which will allow us to be also logged in to the web interface.

On line 6 we query SugarCRM to see if an Account with the given telephone number exists and, if so(line 9),  we get the id of the extracted account (line 12). A little explanation for line 12;  the account id is at index “0” of the array “nvl” because on line 7, when querying SugarCRM, we’ve put “id” as the first (that is index 0) element of the String array of the required fields.
Now we have all we need to compose the URL to enter SugarCRM bypassing the login form and be redirected to the details page of the corresponding account record. On line 13 we create a 2 elements String array containing the path to our browser and the URL itself with variable “value” representing the account id on the database and the sessionID passed as parameter “MSID”. This is what allows us to bypass the login page.

WARNING: the “MSID method” of remotely logging in hasn’t been ported to version “5.0” of SugarCRM, while it seems it is present again on version “5.1” . For testing I’m using SugarCRM 4.2.1b, and the mechanism works properly.

On line 13 we launch the browser. This string is strictly related to the OS and browser you’re using.In my case, if Firefox is already open, a new tab gets opened and I’m redirected to the  account record sugarcrm page.
To complete the modifications, we need to change the “onManageEvent” method of our “PhoneNumberResolver” class. Here is the code:

  1 :    public void onManagerEvent(ManagerEvent event) {
  2 :        String event_name = event.getClass().getSimpleName();
  3 :        if(event_name.equals("DialEvent")){
  4 :            DialEvent e=(DialEvent)event;
  5 :            af.browserToSugarCRM(e.getCallerId());
  6 :        }
  7 :    }

We’ve only changed the call to AccountFinder, this time calling our new “browserToSugarCRM” method. Done this we don’t need anymore the method “showData”, so here is the final version of “PhoneNumberResolver”:


   1:import java.io.IOException;
   2:import java.util.Date;
   3:import java.util.Enumeration;
   4:import java.util.Hashtable;
   5:import javax.swing.JFrame;
   6:import javax.swing.JLabel;
   7:import javax.swing.JOptionPane;
   8:import org.asteriskjava.manager.*;
   9:import org.asteriskjava.manager.event.*;
  10:
  11:public class PhoneNumberResolver implements ManagerEventListener{
  12:    private ManagerConnection managerConnection;
  13:    private AccountFinder af;
  14:    
  15:    public PhoneNumberResolver(){
  16:        ManagerConnectionFactory factory = new ManagerConnectionFactory(
  17:                                "asterisk_server_url", "user", "password");
  18:        this.managerConnection = factory.createManagerConnection();
  19:        af=new AccountFinder();
  20:    }
  21:    public void run() throws IOException, AuthenticationFailedException,TimeoutException,
                                                        InterruptedException, IllegalStateException
  22:    {
  23:        managerConnection.addEventListener(this);
  24:        managerConnection.login();
  25:        while(true)
  26:            Thread.sleep(60000);
  27:    }
  28:    public void onManagerEvent(ManagerEvent event) {
  29:        String event_name = event.getClass().getSimpleName();
  30:        if(event_name.equals("DialEvent")){
  31:            DialEvent e=(DialEvent)event;
  32:            af.browserToSugarCRM(e.getCallerId());
  33:            
  34:        }
  35:    }
  36:    public static void main(String[] args) throws Exception
  37:    {
  38:        PhoneNumberResolver phoneNumberResolver = new PhoneNumberResolver();
  49:        phoneNumberResolver.run();
  40:    }
  41:}


Conclusions

This article should answer a lot of email from readers interested in the exposed mechanism. I’ll be writing more articles on Asterisk/SugarCRM  and SugarCRM/CMS integration soon.
Hasta la proxima.

 

LEAVE A COMMENT