ABSYZ ABSYZ

  • Home

    Welcome to ABSYZ

  • About us

    Who We Are

  • Our Expertise

    What We Do

  • Our Approach

    How We Do It

  • Products

    What We Made

  • Industries

    Who We Do It For

  • Clients

    Whom We Did It For.

  • Article & Blogs

    What Experts Think

  • Careers

    Join The Team

  • Get In Touch

    Let’s Get Started

ABSYZ

Google Assistant Integration with Salesforce – Part 2

Home / Article & Blogs / Apex / Google Assistant Integration with Salesforce – Part 2

Google Assistant Integration with Salesforce – Part 2

By ypranav inApex, Integration, REST, Salesforce

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.

[sourcecode language=”java”]
@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;
}
}
[/sourcecode]

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.

[sourcecode language=”java”]
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);
}
}
[/sourcecode]

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:

 

The second step is to build you App:

The third step is get ready for Deployment:

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

If any doubts feel free to reach out to us.

DialogflowGoogle AssistantGoogle Assistant IntegrationIntegration with Google AssistantIntegration with SalesforceIntent creation in Dialogflow
44
Unlike this post
2 Posts
ypranav

Search Posts

Archives

Categories

Recent posts

Service Cloud for customer delight

Service Cloud for customer delight

Salesforce Financial Services Cloud

Salesforce Financial Services Cloud

Salesforce Field Service Lightning

Salesforce Field Service Lightning

Connecting the dots with Customer 360

Connecting the dots with Customer 360

Customer delight with Commerce Cloud

Customer delight with Commerce Cloud

  • Previous PostHighlights of Winter'19 Release Notes: Part II
  • Next PostManaging Duplicate Records in Salesforce

Related Posts

REST API call from Einstein Analytics Dashboard
Apex REST Salesforce Salesforce Einstein Wave Analytics

REST API call from Einstein Analytics Dashboard

Create/Update Salesforce Picklist definitions using metadata API
Integration Metadata API Salesforce

Create/Update Salesforce Picklist definitions using metadata API

13 Comments

  1. Akash
    Reply
    18 September 2018

    What is text in line no.6?

    Reply
  2. Vignesh
    Reply
    20 September 2018

    What is the text in line no.19 in postwebhook class?Please explain me how to get that response by using any third party api client?

    Reply
    • ypranav
      Reply
      20 September 2018

      Hi Vignesh, its a response of the intent, when you hit the intent (from the postman tool or anyother ) using a particular intent id.

      you can use this URL :https://api.dialogflow.com/v1/intents/(mention here the Intent id)

      Reply
      • Vignesh
        Reply
        20 September 2018

        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.

        Reply
      • Radha
        Reply
        26 October 2019

        Hi
        What is str1 and listof ? It throws some error.

        Reply
  3. Vignesh
    Reply
    21 September 2018

    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.:)

    Reply
  4. Vignesh
    Reply
    24 September 2018

    HI Pranav ,Iam able to override data in google intent only by using postman and not through postwebhook.Where am I going wrong?

    Reply
  5. ypranav
    Reply
    26 September 2018

    Check the debug log whether it is hitting the salesforce with correct data or not when you try from Assistant.

    Reply
    • mrin
      Reply
      29 February 2020

      can u give full example

      Reply
  6. Yogesh Patil
    Reply
    22 November 2018

    Hi I want to test my intent from simulator. However every time it asks for the name and the process stops. Please help

    Reply
  7. arunmp2004 (@arunmp2004)
    Reply
    5 March 2019

    Hi, Pranav,

    Thank you so much for taking time and sharing this.
    This is much cleaner approach than I did with integrating with AWS Lambda in between and authenticating via OAuth 2.0, using a custom app in salesforce and REST API.

    Now, can you share a Part 3 with secured connections, so that we know when sales person has logged in via their phone/google assistant?
    So that we can securely fetch only records based on their hierarchy or at least greet them with their names.

    Account Linking from Dailog flow is the closest solution I have found, but not sure yet if it works with Salesforce directly.

    Any guidance in this directly will be very helpful. If you prefer we can have a call or webex as well.

    Thank you!
    Arun
    Google Assistant Integration for salesforce promotions /release mgmt – https://www.youtube.com/watch?v=E1u301IhvnM&t=377s

    Reply
  8. Rajat verma
    Reply
    23 July 2019

    Hi guys…I need to send given JSON response from dialogflow to salesforce API(Oauth2.0). How can i do that?
    {

    “title”: “”,

    “firstName”: “”,

    “lastname”: “”,

    “city”: “”,

    “prjcode”: “”, ( Share SFDC Project ID)

    “comment”: “”,

    “visitDate”: “2015-07-07T11:11:11”,

    “addCode”: “”,

    “type”: “”, ( Enquiry Type)

    “Src”: “”, ( Enquiry Source)

    “CountryCode”: “”, (example +91)

    “rating”: “Warm”,

    “projectType”: “Residential”,

    “budget”: “”,

    “EnquiryType”: “”,

    “phoneNo”: “”,

    “state”: “”,

    “Country”: “”,

    “websiteName”: “” (Pass the landing page URL)

    }

    Reply
  9. Tester
    Reply
    29 February 2020

    where is the value of listof ,str1 in postwebhook is defined ?

    Reply

Leave a Reply (Cancel reply)

Your email address will not be published. Required fields are marked *

*
*

ABSYZ Logo
  • Home
  • About us
  • Article & Blogs
  • Careers
  • Get In Touch
  • Our Expertise
  • Our Approach
  • Products
  • Industries
  • Clients
  • White Papers

ABSYZ Software Consulting Pvt. Ltd.
USA: 49197 Wixom Tech Dr, Wixom, MI 48393, USA
M: +1.415.364.8055

India: 6th Floor, SS Techpark, PSR Prime, DLF Cyber City, Gachibowli, Hyderabad, Telangana – 500032
M: +91 79979 66174

Copyright ©2020 Absyz Inc. All Rights Reserved.

youngsoft
Copy