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

Einstein Intent and Einstein Sentiment Analysis on Facebook Posts

Home / Article & Blogs / Apex / Einstein Intent and Einstein Sentiment Analysis on Facebook Posts

Einstein Intent and Einstein Sentiment Analysis on Facebook Posts

By shruthimikkilineniabsyzcom inApex, Integration, REST, Salesforce Einstein, Trigger

Einstein Language

Einstein Language Is used to build natural language processing into your apps and unlock insights within text. Einstein Language contains two NLP services:

  • Einstein Sentiment
  • Einstein Intent
Einstein Intent

—Categorize unstructured text into user-defined labels to better understand what users are trying to accomplish. Leverage the Einstein Intent API to analyze text from emails, chats, or web forms to

  • Determine which products prospects are interested in, and send customer inquiries to the appropriate sales person.
  • Route service cases to the correct agents or departments, or provide self-service options.
  • Understand customer posts to provide personalized self-service in your communities.
Einstein Sentiment

—Classify the sentiment of text into positive, negative, and neutral classes to understand the feeling behind text. You can use the Einstein Sentiment API to analyze emails, social media, and text from chat to:

  • Identify the sentiment of a prospect’s emails to trend a lead or opportunity up or down.
  • Provide proactive service by helping dissatisfied customers first or extending promotional offers to satisfied customers.
  • Monitor the perception of your brand across social media channels, identify brand evangelists, and monitor customer satisfaction.

 

Lets suppose I have a Facebook Page for an E-Commerce site. If any user sharing their feedback through posts or comments on the page, We can retrieve the post and comments to Salesforce and find Intent of the post and the Sentiment of the comments.

This can be achieved by following these steps:

step 1. Create a Facebook page.

step 2. Create a Facebook app using Facebook Developers account.

To Connect with Facebook we Should use Graph API, To use Graph API we need to Create a Facebook Developers account using Facebook credentials,

To create a Facebook app

  • login to https://developers.facebook.com/
  • click on My Apps -> Add a New App.

Enter basic information and make the App Live, If the app is not Live we can’t use it for Communicating with Facebook.

Now click on Tools & Support and go to Graph API Explorer, here we can generate Access token and test the app.

Steps to create Access token:
  • select you app from Application drop down.
  • click on Get Token and select Get User Access Token.
  • select the permissions required and click Get Access Token.
  • To get Page Access token click on Get Token and select you Page from Page Access Tokens.
  • Now you can see your page name on the drop down list, click on that and select Request publish_pages.

 

step 3. Create an Object on Salesforce to save posts and their comments.

Create an object with fields to store posts, comments, Sentiment, Intent.

step 4. Create remote site setting on Salesforce.

Go to Remote Site Settings -> New Remote Site

enter site name, remote site URL as https://graph.facebook.com , check Active checkbox and Save.

step 5. Make a callout to Facebook to retrieve posts and comments.

In Einstein_Facebook class we are getting post and comments from Facebook, and extracting posts and comments from returned JSON and creating records.

 

[sourcecode language=”java”]
public class Einstein_Facebook {

public static void sendRequest(){

//getting accesstoken from custom lable.
String accessToken = Label.Facebook_Access_Token;
HttpRequest request = new HttpRequest();
request .setEndpoint(‘https://graph.facebook.com/v2.12/391648194596048?fields=posts{message,comments,type}&access_token=’+accessToken);
request .setMethod(‘GET’);
//Making request
Http http = new Http();
HTTPResponse response= http.send(request );
//getting post and comments in response.
String data=response.getBody();

//getting post and comments from JSON rceived in response.
List line=data.split(‘,’);
List posts=new List();
List postAndComments=new List();
for(String l:line){
posts.add(l.substringBetween(‘{“message”:”‘,'”‘));
string substring=l.substringBetween(‘”message”:”‘,'”‘);
if(substring!=null)
postAndComments.add(substring);
}
//creating a Map of posts and comments.
Map PostComments =new Map();
for(String p:posts){
if(p!=null){
List empty=new List();
PostComments.put(p,empty);
}
}
String key;
Listcomments=new List();
for(string messages : postAndComments){

//checking if the message exists in posts and adding as key.
if(messages!=null){
if(PostComments.containsKey(messages)){
key = messages ;
}

//else adding to comments to corresponding post.
else{
comments=PostComments.get(key);
comments.add(messages);
PostComments.put(key, comments);
}
}
}

//querying all the Posts and comments
List existingPosts = [SELECT id,Comments__c,Posts__c FROM Einstein_Facebook__c];
List listDelete = new List();
for(Einstein_Facebook__c Post:existingPosts){

//checking if post already exists and adding to a list.
if(PostComments.containsKey(Post.Posts__c)){
listDelete.add(Post);
}
}

//deleting the list.
delete listDelete;

List  PostCommentList=new List();
//Iterating through Map to create records.
for(string post : PostComments.keySet()){
for(string comments : PostComments.get(post)){
Einstein_Facebook__c Facebookcomment=new Einstein_Facebook__c();
Facebookcomment.Posts__c=post;
Facebookcomment.Comments__c=comments ;
PostCommentList.add(Facebookcomment);}
}

//Inserting List of  posts and comments.
insert PostCommentList;

}

}
[/sourcecode]

step 6. Write a trigger to get Intent and sentiment of post and comments.

Trigger to fire after insert on Facebook posts and comments:

[sourcecode language=”java”]

trigger EinsteinFB_Trigger on Einstein_Facebook__c (after insert) {    For(Einstein_Facebook__c post : Trigger.New)

{

String  postId = post.Id;

Einstein_Handler.getProbabilityforFB(postId);

}

}

[/sourcecode]

Handler for the trigger:

Here we are using EinsteinVision_Admin EinsteinVision_Sentiment classes to get Intent and Sentiment of posts and comments which we explained in detail in our previous blog on Einstein Intent and Einstein Sentiment.

[sourcecode language=”java”]
global class Einstein_Handler{

@future(callout=true)
Public static void getProbabilityforFB(String body){
Einstein_Facebook__c fd=[select Posts__c, Comments__c from Einstein_Facebook__c where id=:body];

String intentLabel = EinsteinVision_Admin.getPrediction(fd.Posts__c);
String sentimentLabel = EinsteinVision_Sentiment.findSentiment(fd.Comments__c);

fd.Feedback_Type__c=intentLabel;
fd.Sentiment__c=sentimentLabel;
update fd;
}

}
[/sourcecode]

This is a report to show analysis of Facebook post and comments.

facebook report

Einstein IntentEinstein LanguageFacebookSalesforceSentiment Analysis
87
Like this post
1 Post
shruthimikkilineniabsyzcom

Search Posts

Archives

Categories

Recent posts

Audit Trail in Marketing Cloud

Audit Trail in Marketing Cloud

Salesforce Integrator & Expert Consultant

Top 5 Factors to evaluate before choosing a Salesforce Integrator

Top 5 Factors to evaluate before choosing a Salesforce Integrator

Significance of UI/UX Design

Significance of UI/UX Design

Cyber-security in an uncertain world

Cyber-security in an uncertain world

  • Previous PostEinstein Intent Analysis Using Einstein Language on Salesforce Chatter
  • Next PostLightning Components As Flow Screens

Related Posts

Cyber-security in an uncertain world
Apex

Cyber-security in an uncertain world

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

4 Comments

  1. Jagadish Lutimath
    Reply
    26 March 2018

    Hi,

    I am getting Error: Compile Error: Unexpected token ‘List’. at line 17 column 10

    Is this code works just by copy and pasting?

    Reply
    • shruthimikkilineniabsyzcom
      Reply
      26 March 2018

      Please check if you are getting response in line 14, keep a debug statement and check.

      Reply
  2. nanda
    Reply
    11 June 2019

    Hi, I am stuck at the Publish the Page, where it is expecting me to register and validate the app and is expecting me to be a valid business unit. I am doing this for a demo as an individual. Is there an alternative way out to get this working? Any help is much appreciated.

    Reply
  3. Avi
    Reply
    17 August 2019

    Hi, many information are missing to replicate. Can you add what you stored in custom label?
    Even apex class returns many errors and cannot be saved.

    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