Apex · Integration · REST · Salesforce

Google Assistant Integration with Salesforce – Part 2

Let suppose when the Intent creation is ready, you want the custom data as a reply in Google assistant, we overwrite the intent from Apex call. You can add a custom response from Salesforce to Google assistant. We fetch the record names from the Salesforce org and overwrite the response in the intent.

@RestResource(urlMapping='/Dialogflow')
global class restCall {
    @HTTPPost
    global static string fetchRecords(){
	//response from Google Assistant as a JSON
        String request = RestContext.request.requestBody.toString();
        mapurl orp = (mapurl)JSON.deserialize(request, mapurl.class);
        string str=orp.result.metadata.intentName;
        string givenInt=orp.result.parameters.number1;
	string outputstring='';
        String accountList='';
	if((str.contains('New')||(str.contains('Add')) ||(str.contains('Create'))) &&(str.contains('Account'))){
            outputstring='Congratulations Account is created successfully';
            postWebhook.postCallout(outputstring);
            string lastname=orp.result.parameters.Name;
            string phone=orp.result.parameters.phone;
            string email=orp.result.parameters.Email;
            account acc= new account();
            acc.name=lastname;
            acc.Phone=phone;
            acc.Email__c=email;
            insert acc;
        }
        else if((str.contains('get ')) && (str.contains('Account'))){
            Integer num =  Integer.valueof(givenInt);
            accList = [select name from Account order by createddate limit :num];
            for(Account a: accList)
            {
                accountList=accountList+ ' ' + a.name;
            }
            postWebhook.postCalloutquery(accountList);
        }
        String s= 'Success';
        return s;
    }
    global class mapurl{
        global result result;
    }
    global class result{
        global parameters parameters;
        global metadata metadata;
        global string resolvedQuery;
    }
    global class parameters{
        global String number1;
        global String Phone;
        global String Name;
        global String Email;
    }
    global class metadata{
        global String intentName;
    }
}

Access token to be used in salesforce org is as shown below:

Capture34.PNG

So when a response is received from Google assistant we make a call to an apex method to edit the intent. The below class is to edit the intent with our data from salesforce.

In the below code you can see a string that is passed to request.setBody where you can try to get it from postman or some related apps. In the JSON we edit our intent data that should overwrite the data in the Google Intent.

global class postWebhook {

	global static void postCallout(string outputstring){
        string accessToken='Here we have to use the developer Access token';
        //The below Text is the response from postman. dont use the same it will throw error
string text = '{"id": "332b2bf0-a53e-4b3e-b03c-986cbe8ee00e","name": "yes","auto": true,"contexts": [],"responses": [{"resetContexts": false,"affectedContexts": [],"parameters": [],"messages": [{"type": 0,"speech": "'+str1+'"}],"defaultResponsePlatforms": {},"speech": []}],"priority": 500000,"cortanaCommand": {"navigateOrService": "NAVIGATE","target": ""},"webhookUsed": false,"webhookForSlotFilling": false,"lastUpdate": 1526896421,"fallbackIntent": false,"events": [],"userSays": [{"id": "1ff25907-2b72-4866-ae96-c1dca66e9747","data": [{"text": "sure"}],"isTemplate": false,"count": 0,"updated": 1526896421,"isAuto": false},{"id": "b95c226f-4b36-4ba3-849d-824fb44a0bc0","data": [{"text": "yes"}],"isTemplate": false,"count": 0,"updated": 1526550631,"isAuto": false}],"followUpIntents": [],"endInteraction": True,"templates": []}';
        Http http  = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndPoint('Here you have to set the endpoint URL which is Dialogflow Api URL');
        request.setMethod('PUT');
        request.setHeader('content-type', 'application/json');
        request.setHeader('Accept-Language', 'application/json');
        request.setHeader('Authorization', 'Bearer '+accessToken);
        request.setBody(text);
        HTTPResponse response = new HTTP().send(request);
    }
    global static void postCalloutquery(string accountList){
        string accessToken='241b07a2d7eb4f82a31158bb6e310d35';
        string text = '{"id": "2b641bce-bdf7-4b6c-8637-0388a05f71af","name": "True","auto": true,"contexts": [],"responses": [{"resetContexts": false,"affectedContexts": [],"parameters": [],"messages": [{"type": 0,"speech": "'+ listOf +'"}],"defaultResponsePlatforms": {},"speech": []}],"priority": 500000,"cortanaCommand": {"navigateOrService": "NAVIGATE","target": ""},"webhookUsed": false,"webhookForSlotFilling": false,"lastUpdate": 1526900788,"fallbackIntent": false,"events": [],"userSays": [{"id": "06833ebd-5581-4b35-b23b-c655231605ad","data": [{"text": "True"}],"isTemplate": false,"count": 0,"updated": 1526900594,"isAuto": false}],"followUpIntents": [],"endInteraction": True,templates": []}';
        Http http  = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndPoint('https://api.dialogflow.com/v1/intents/2b641bce-bdf7-4b6c-8637-0388a05f71af');
        request.setMethod('PUT');
        request.setHeader('content-type', 'application/json');
        request.setHeader('Accept-Language', 'application/json');
        request.setHeader('Authorization', 'Bearer '+accessToken);
        request.setBody(text);
        HTTPResponse response = new HTTP().send(request);
    }
}

So create an intent as shown below and you find the intent will be changed when the method is called.

You can test it from the Google assistant Simulator as shown below.

Capture4.PNG

Now you are all set with your Google assistant intent that your action is ready to deploy into production. Few more steps to be followed. Click on overview in the Actions on Google page.

Capture21.PNG

This is to setup your app and upload it to the production. When it is uploaded to production, everyone will be able to access the custom app. The first step is the quick setup:

 

This slideshow requires JavaScript.

The second step is to build you App:

This slideshow requires JavaScript.

The third step is get ready for Deployment:

This slideshow requires JavaScript.

The final step is to submit your application to the production as shown below.

If any doubts feel free to reach out to us.

6 thoughts on “Google Assistant Integration with Salesforce – Part 2

      1. Thank you so much @ypranav :).I tried that but it says malformed response,when I am posting from salesforce to dialogflow while overwriting the default response for the get account intent.

        Like

  1. Hi Pranav,
    Can you help me?I have replicated your code and my text string looks like below:
    string text = ‘{“id”:”cad08db6-e7cb-4572-ae3e-26749a196a8e”,”name”:”True”,”auto”:true,”contexts”:[],”responses”:[{“resetContexts”:false,”affectedContexts”:[],”parameters”:[],”messages”:[{“type”:0,”speech”:”HUOI”}],”defaultResponsePlatforms”:{},”speech”:[]}],”priority”:500000,”cortanaCommand”:{“navigateOrService”:”NAVIGATE”,”target”:””},”webhookUsed”:false,”webhookForSlotFilling”:false,”lastUpdate”:1537522239,”fallbackIntent”:false,”events”:[],”userSays”:[{“id”:”49a680d8-b28e-47e2-ab75-aeba0bac5a63″,”data”:[{“text”:”yes”}],”isTemplate”:false,”count”:0,”updated”:1537509584,”isAuto”:false},{“id”:”c3c6b5a7-9038-4a03-a681-f37cb216a580″,”data”:[{“text”:”True”}],”isTemplate”:false,”count”:0,”updated”:1537509584,”isAuto”:false}],”followUpIntents”:[],”liveAgentHandoff”:false,”endInteraction”:true,”templates”:[]}’;

    When Im trying to post it says malformed response and its not overriding my default response for the “True” intent.
    Thanks in advance.:)

    Like

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