Sunday, 22 July 2018

Einstein Bots with Apex Class | How to Call Apex Class From Einstein Bots


In the last post we discussed how to setup Einstein Bots. In this post we will call apex class from Einstein Bots.

Step 1) Setup Live Agent . You can check this post to setup Live Agent.
Step 2) Enable Einstein Bots. Take a help from this post.
Step 3) Create one Apex Class with "@InvocableMethod" Method.


public with Sharing Class ChatBotTemperature
{
    public class ChatBotTempOutput{
        @InvocableVariable(required= true)
        public String City ;

        @InvocableVariable(required= true)
        public Integer Temp ;
    }

    public class ChatBotTempInput{
        @InvocableVariable(required= true)
        public String City ;
    }

    @InvocableMethod(Label ='Get Temperature' description='return temp')
    public static List<ChatBotTempOutput> getTemperature(List<ChatBotTempInput> input)
    {
         List<ChatBotTempOutput> lstOutput = new List<ChatBotTempOutput>();
         for( ChatBotTempInput ChatBI : input){
             ChatBotTempOutput obj = new ChatBotTempOutput();
             obj.City = ChatBI.City;
             obj.Temp = 10; // You can make some callout to get real Tem
             lstOutput.add(obj);
         }
         return  lstOutput;     
    }
}


Step 4) Add The Apex class in  "sfdc.chatbot.service.permset" permission set. This is the Important part.


Step 5) Setup Einstein Bot

Create new bot
 Create Dialog

Add Question
Add New Slot to get City Value
Add Action. 
 All Input and Output Invocable Variable will come after selecting Action (Invocable Method).
Show Response.

Step 6) Here is output 


How to setup Bot. Please check below recording


Please share your feedback and comment.

Thanks
Amit Chaudhary