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.
Four terminologies to know:
- Intent
- Entities
- Fulfillment
- 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.
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.
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.
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.
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.
i am getting this error MalformedResponse Webhook error (206).
can you help me ?
LikeLike
Awesome. Thanks for sharing
LikeLiked by 1 person
Hello sir, I am having this error, will you please help me to resolve it,
Webhook call failed. Error: Failed to parse webhook JSON response: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $.
LikeLike
I try to create an account using “My test app” but I get the error as – “My test app isn’t responding right now. Try again soon.”
LikeLike
Hey There,
I would like to know the privacy model of our data if we integrate Google Assistance with Salesforce using dialogflow.api.
Would appreciate your response.
Regards
LikeLike
what is the rest resource name here? can you elaborate?
LikeLike
Hi Becky Williams
The @RestResource is used at the class level and enables you to expose an Apex class as a REST resource.
Example: Site page URL + /restresource_name
In the above case: Your org site page URL + /Dialogflow (refer the first line in the apex code)
So when the URL is mentioned outside your Salesforce org (eg: Webhook), the RestResource name mentioned in the apex class is exposed.
To know more about @RestResource check the link (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_rest_resource.htm)
LikeLike
How to authorize my salesforce org ? with dialogflow
LikeLike
Hi Kazi,
Follow the steps shown below the Fulfillment.
Step 1: Site Page creation
Step 2: In Fulfilment tab, Enable Webhook
Step 3: Paste the site page URL with the rest resource name
LikeLike