ABSYZ ABSYZ

  • Home

    Home

  • 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
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.

Testimonials-bg

If any doubts feel free to reach out to us.

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

Search Posts

Archives

Categories

Recent posts

BioAsia 2023 in Hyderabad: An Annual International Event

BioAsia 2023 in Hyderabad: An Annual International Event

The Role Of Marketing in Small & Medium Enterprises

The Role Of Marketing in Small & Medium Enterprises

Salesforce For Retail: How Salesforce CRM Can Help Retailers

Salesforce For Retail: How Salesforce CRM Can Help Retailers

What is ChatGPT & How Does It Work?

What is ChatGPT & How Does It Work?

What Is Graphic Design? (Executive Summary 2023)

What Is Graphic Design? (Executive Summary 2023)

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

Related Posts

Salesforce For Retail: How Salesforce CRM Can Help Retailers
Salesforce

Salesforce For Retail: How Salesforce CRM Can Help Retailers

Introduction To Copado Devops Tool
Salesforce

Introduction To Copado Devops Tool

What is Salesforce Code Builder?
Salesforce

What is Salesforce Code Builder?

Automation in Healthcare And Its Benefits
Health Cloud Salesforce

Automation in Healthcare And Its Benefits

9 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
  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
  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. 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

Leave a Reply (Cancel reply)

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

*
*

ABSYZ Logo

INDIA | USA | UAE

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

Copyright ©2022 Absyz Inc. All Rights Reserved.

youngsoft
Copy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “ACCEPT ALL”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. Privacy Policy
Cookie SettingsREJECT ALLACCEPT ALL
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.

CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.

Functional

Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.

Performance

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Analytics

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.

Advertisement

Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.

Others

Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.

SAVE & ACCEPT