Salesforce Alexa Integration

 

Will it not be interesting if Alexa can get to details of new cases from Salesforce??

Yes!! You heard it right. In this article, I will help you with the steps to integrate Salesforce with Amazon Alexa.

The scope of this document will be restricted to showcase one of the capability and not solve any business use case.

What is Alexa? Amazon Alexa, also known simply as Alexa, is a virtual assistant AI technology developed by Amazon, first used in the Amazon Echo smart speakers is capable of voice interaction, music playback, etc. Users can extend the Alexa capabilities by installing "skills" (additional functionality developed by third-party vendors, in other settings more commonly called apps such as weather programs and audio features).

Design - We will create a Java application, which will connect to Salesforce and Amazon Alexa through their respective APIs. We will use AWS Lambda to run our code. AWS Lambda lets you run code without provisioning or managing servers. We will create a custom Alexa skill (using Alexa developer console), which can be enabled in Alexa supported devices and takes user inputs/commands. The Alexa skill will handle the user request and direct it towards the AWS Lambda function to get you the response.

Prerequisites
API Connectivity  - We will use Salesforce SOAP API and Amazon Alexa skill kit SDK’s to establish a connection between Salesforce and Alexa. You can connect to Salesforce via other APIs provided by Salesforce like REST API etc.
Eclipse IDE + Java project - Let us start with a backend Java application that can handle requests from Alexa and connect Salesforce to retrieve appropriate data for user commands.
Prerequisites for Salesforce connection.
  • Login to Salesforce
  • Go to Set up and type API in a quick search
  • Click on Enterprise WSDL to download the WSDL file. You would need to generate required jar files using a Web service connector from Salesforce. More information in Web Service Connector here.
Once you have the Enterprise.jar file ready, you would need to import this to your Gradle project. You might also need dependencies for Alexa integration as below. More details about dependencies here. You would need ask-SDK and AWS-lambda-java dependencies for Alexa connectivity and Enterprise Jar(external Jar) you created in the above step and force WSC for salesforce connectivity.

        

For this use case, We will create below 4 java classes.

  •  LaunchRequestHandler.java - To handle initial user request
  • CaseRequestHandler.java  - To request salesforce case information
  • QuerySalesforce – Logic to retrieve Case information from Salesforce
  • SalesforceAlexaSkillStreamHandle – Include the above two handlers
        


        


For details on Amazon related classes like LaunchHandlerequest java class, refer to this document. Compile all the classes and run the build jar under a Gradle task to generate the jar file. We will upload this Jar as our Alexa skill backend logic at Amazon S3 location in some time. You can also use the Java Maven project to build the executable Jar with the above-mentioned dependencies.

Custom Alexa Skill - Next we will create a Custom Alexa skill that will redirect the user request to the above-created java application. Login to Alexa developer console from here and click on create the skill. You can give a skill name of your choice and choose a model as custom. As we will be using the AWS Lambda function to host our backend using Java, select ‘Provision your own’ option for the question ‘Choose a method to host your skill's backend resources’. 

       


On the left accordion, click invocation to change the skill invocation name. I have kept it as a ‘geeksoft salesforce’ as in the screenshot below. Alexa users say a skill's invocation name to begin an interaction with a particular custom skill. For example, for the invocation name is "geeksoft salesforce", users can say:

                 User: “Alexa, ask geeksoft salesforce how many new cases are assigned to me

        


Remember to hit the ‘save model’ button at the top to save your changes. This saves your data but does not build the model. We will be looking into the ‘Build Model’ in some time.

You will need to create an Intent that can respond to Salesforce queries. If you have already worked on Einstein BOT on Salesforce service cloud, Intents and building a model should not be a surprise to you. More about Salesforce bot Intents here.

Click on Interaction on the left accordion and then click on Intents. Click ‘Add Intents’ as in the below screenshot to create one. An intent represents an action that fulfills a user's spoken request.


        

For Salesforce Interaction, I have created Case intent as in the below screenshot. You can add some good numbers of utterances. I have included only 5 and should be good to start with. The utterances are a set of likely spoken phrases mapped to the intents.

          

Next, you need to retrieve a unique Id for your skill. Select Endpoint from left accordion and then select AWS Lambda ARN. Note down your skill ID which you need at a later point while linking Alexa skill to the AWS Lambda function. The Endpoint will receive POST requests when a user interacts with your Alexa Skill. 

         

Once you have saved the endpoint, click on Interaction Model on the left accordion, and then hit the build model button. Alexa will run background jobs to get your AI Interaction model ready. This should look similar to the Salesforce Einstein bot Build model. Your changes will be validated and saved while building an Interaction model.

        

AWS Lambda function - Once you are done with the above step, you need to login to the AWS account to create aa IAM role and Lambda function. The IAM role is AWS identity with permission policies that determine what the identity can and cannot do in AWS.Login here using your credentials, and then click on IAM under services as in the screenshot below.

         

Click on roles to create a new role. 

         

While creating a new role, select the type of trusted entity as AWS service and use case as Lambda(as in the below screenshot). 

       

Include below 3 required policies in your new role as in the screenshot below before you hit the create role button.

        

Now go back to the services page to create Lambda functions. Click on Lambda as in the screenshot below.

        

Click on create a function and provide the below information as in the screenshot. You may also need to select the new role you created in the above steps under the permission/execution role section.

        

On successful creation of the Lambda function, you will land on the configuration tab as below. Hit the ‘+ Add trigger’ button as in the below screenshot. 

        

Type in Alexa to find the Alexa skills kit as below.

        

Select the Alexa skill kit from the drop and populate the skill Id you have noted down from the above step(which you have noted down) before you click the add button.

        

Let's upload the executable jar we created in the above steps as backend logic for your Alexa skill. You can either upload the Jar directly or use the Amazon S3 bucket. I have uploaded the Jar at Amazon S3, hence I will be using the S3 location which I have noted down to update the function code.     

        

Click on save once you update the Jar file location from the S3 bucket.

        

Now scroll to the button of the Lambda function page to update the handle information at Basic settings. Replace ‘example. Hello::handleRequest’ with ‘com.geeksoft.handler.SalesforceAlexaSkillStreamHandler::handleRequest’. 

        

You would need to configure the handler information based on your Java class and methods. Here you are telling the Lambda runtime which handler method to invoke by setting the handler parameter on your function's configuration. The following handler formats are supported in Java: package. Class:: method – Full format. For example, com.main:: Handler calls the Handler method defined in main.java.

         

Now time to connect the AWS Lambda function with Alexa skill. You can do this by unique ARN Id. Copy the ARN number from the top right below your name(as in the screenshot below).

        

And update the ARN Id at Alexa developer console. Go to the Endpoint section on the left accordion and paste the ARN at the default region as below and click on save endpoint. 

        

Time to Test your Alexa skill: Click on ‘configure test events’ as in the below screenshot.

        

Select the Alexa skill kit start a session as an event template as below, provide an event name, and click the ‘create’ button at the bottom.

        

Once the Test event configuration is saved, click on the test button to test your backend logic. If everything works as expected you should see no error. 

        

         

If everything looks good with backend execution, you can test your backend logic via the Alexa developer console. Click on Test Tab as below and then click on ‘Development’ at Alexa developer console.

        

We will use an Alexa simulator for our testing. Make sure skill I/O and Device Display are checked.

        

You can either type or hold the mic button to provide a voice command to Alexa skills you developed.

        

If you notice, you will get your response in Alexa from your Java application. You can click the play button on the right corner to hear the voice output.     

        

Now time to test salesforce connection and commands.

        

you will notice Alexa will return realtime results from Salesforce for your question.

        

Test from Mobile App: Now it’s time to test the same from Alexa enabled devices. In this use case, we will use the Alexa Android app. Install the Alexa Android app from Google play store or a similar one from the apple store for IOS. Log in to the app using the same amazon account you have been using so far. 

      

Once you log in successfully in the app, enable the skill you have recently created available at the DEV section. The Alexa skill which you have created is still under development. If you wish to make your skill available to other users, you first need to get it certified from Amazon and then publish by providing more required information. More details can be found here. Tap on Alexa Icon to ask those questions which your custom skill can handle or connect to salesforce.

                                                            

For example :

Open geeksoft salesforce” will return “Welcome to Geeksoft Salesforce Demo

ask geeksoft salesforce how many new cases do I have” will return “ You have 6 New Cases in Salesforce


I hope this was helpful.










Comments

Popular posts from this blog

RPA Trends to Watch in 2022

The Future & Scope of Robotics in the Healthcare Industry

SAP Goes On to Reimagine Services and Support Portfolio for Success in the Cloud Technology