Apex · Integration · REST · Salesforce

Google Assistant Integration with Salesforce – Part 1

Everyone has come across Google Assistant which help people in communication that is a Google’s voice-controlled AI smart assistant. Google Assistant is integrated with Salesforce, to make communication easy and reduce the work. Here we do a simple integration with Salesforce to create, delete and fetch details of a particular record.

First, we should sign up Dialogflow.api with your Gmail account. Click on signup for free. You will be redirected to a page asking for the sign in with Google. Sign up with existing Gmail account and you can see the redirected page that logins into dialogflow.com. And create an agent (a project) for a custom development.

This slideshow requires JavaScript.

Four terminologies to know:

  1. Intent
  2. Entities
  3. Fulfillment
  4. Integrations

Intent:

Intent maps the user input commands with related actions for your app. Intent allows the users to specify what they wanted to do and figures out what activity matches what was said. Click on Intent that is on your left and click on create new Intent as shown below.

This slideshow requires JavaScript.

Entities:

Entities are used for extracting parameter values from the user inputs and any important data that you wanted to get from the user, you will create the corresponding entity. It is not necessary to create all possible concepts as entities, entities are created only for the actionable data that is needed. To create an Entity check the below images.

This slideshow requires JavaScript.

Fulfillment:

Fulfillment allows us to decide our responses to our conversations. It is a conversational interface between your application and the logic to fulfill the action. For example, we integrate with Salesforce and Google Assistant. We need to create a site from salesforce org as shown below (Site -Custom URL is used because we need to get a public access to the apex class from our org). Enter the domain URL in Fulfillment Webhook URL and append the URL with the rest resource name.

This slideshow requires JavaScript.

Integration:

Integration is to use Dialogflow’s Actions on Google integration to test your Dialogflow agent in the Actions on Google simulator. Click on Integration in the left menu and select Integration Settings. Enable Auto-Preview changes as the dialogflow will propagate changes to the Actions Console and Assistant Simulator automatically. Now you are all set to test your custom app.

This slideshow requires JavaScript.

We create an apex class with the annotation @RestResource in the class because we expose an apex class as a REST resource. If we use RestResource, that particular class should be defined as global. In the brackets, we provide the rest resource name. We are extracting the data from the JSON so you can get the key terms whether you want to insert, update or delete records.

@RestResource(urlMapping='/Dialogflow')
global class restCall {
    @HTTPPost
    global static string createRecords(){
        //response from Google Assistant as a JSON
        String request = RestContext.request.requestBody.toString();
	//deserialize the JSON
        mapurl orp = (mapurl)JSON.deserialize(request, mapurl.class);
        string str=orp.result.metadata.intentName;

        //check whether it is an account
        if((str.contains('New')||(str.contains('Add')) ||(str.contains('Create'))) &&(str.contains('Account'))){
            account acc= new account();
            acc.name=orp.result.parameters.Name;
            acc.Phone=orp.result.parameters.phone;
            acc.Email__c=orp.result.parameters.Email;
            insert acc;
        }
	//check whether it is a contact
        else if((str.contains('New')||(str.contains('Add')) ||(str.contains('Create'))) &&(str.contains('Contact'))){
            contact con= new contact();
            con.LastName = orp.result.parameters.Name;
            con.Phone = orp.result.parameters.phone;
            con.Email = orp.result.parameters.Email;
            insert con;
        }
        String s= 'Success';
        return s;
    }
	//wrapper to get the values from the JSON
    global class mapurl{
        global result result;
    }
    global class result{
        global parameters parameters;
        global metadata metadata;
        global string resolvedQuery;
    }
    global class parameters{
        global String Phone;
        global String Name;
        global String Email;
    }
    global class metadata{
        global String intentName;
    }
}

On click of Test from Integration setting, it will redirect to a page called simulator. Google allows you to test in a browser without an actual google home device named Google Home Web Simulator. You have not yet named your custom App so initially, the command will be: “Talk to Test app”. The setup to create your application name and use the app live will be continued in our next blog.

This slideshow requires JavaScript.

One thought on “Google Assistant Integration with Salesforce – Part 1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s