INTEGRATION ·
Salesforce Alexa Integration
Wouldn't it be interesting if Alexa could tell you about new cases in Salesforce? A step-by-step guide to connecting Salesforce and Amazon Alexa with Java and AWS Lambda.
Wouldn't it be interesting if Alexa could get you the details of new cases from Salesforce?
Yes, you heard it right. In this article, I will walk through the steps to integrate Salesforce with Amazon Alexa. The scope is limited to showcasing one capability, not solving a business use case.
What is Alexa?
Amazon Alexa is a virtual assistant AI technology developed by Amazon, first used in the Amazon Echo smart speakers. It is capable of voice interaction, music playback and more. Users can extend Alexa's capabilities by installing “skills”: additional functionality developed by third parties, more commonly called apps in other settings, such as weather programs and audio features.
Design
We will create a Java application that connects to Salesforce and Amazon Alexa through their APIs, and run the code on AWS Lambda, which runs code without provisioning or managing servers. We will create a custom Alexa skill in the Alexa developer console, which can be enabled on Alexa-supported devices and takes user commands. The skill handles the user's request and directs it to the AWS Lambda function to get the response.
Prerequisites
- Eclipse IDE or a Java 8 compatible runtime environment (or any similar IDE that supports Maven or Gradle)
- A Gradle project (Maven works as well)
- A Salesforce environment (a developer edition or sandbox)
- An Amazon Web Services (AWS) account
- The Amazon Developer Console
API connectivity: we will use the Salesforce SOAP API and the Alexa Skills Kit SDKs to connect Salesforce and Alexa. You can also connect to Salesforce through other Salesforce APIs, such as the REST API.
1. The Java project
Start with a back-end Java application that handles requests from Alexa and connects to Salesforce to retrieve the right data for each user command.
Prerequisites for the Salesforce connection
- Log in to Salesforce.
- Go to Setup and type “API” in Quick Find.
- Click Enterprise WSDL to download the WSDL file. Generate the required jar files with the Salesforce Web Service Connector (WSC).
Once the enterprise jar is ready, import it into your Gradle project. You also need the ask-sdk and aws-lambda-java dependencies for Alexa connectivity, the enterprise jar you created above, and force-wsc for the Salesforce connection.
For this use case, we create four Java classes:
- LaunchRequestHandler.java: handles the initial user request
- CaseRequestHandler.java: requests Salesforce case information
- QuerySalesforce: the logic that retrieves case information from Salesforce
- SalesforceAlexaSkillStreamHandler: includes the two handlers above
Compile the classes and run the build under a Gradle task to generate the jar file. We will upload this jar to an Amazon S3 location as the skill's back-end logic. You can also use a Maven project to build the executable jar with the same dependencies.
2. The custom Alexa skill
Next, create a custom Alexa skill that sends the user's request to the Java application. Log in to the Alexa developer console and click Create Skill. Give the skill a name of your choice and choose Custom as the model. Because the back end will be hosted on AWS Lambda in Java, choose “Provision your own” for “Choose a method to host your skill's backend resources”.
Under Invocation, set the skill invocation name. I used “geeksoft salesforce”. Alexa users say a skill's invocation name to start an interaction with that custom skill, for example: “Alexa, ask geeksoft salesforce how many new cases are assigned to me.”
Remember to click Save Model at the top. This saves your data but does not build the model; we will build it shortly.
Next, create an intent that responds to Salesforce queries. If you have worked with Einstein Bots in Salesforce Service Cloud, intents and model building will feel familiar. Under Interaction Model, click Intents, then Add Intent. An intent represents an action that fulfils a user's spoken request.
For the Salesforce interaction, I created a Case intent and added five utterances, which is enough to start with. Utterances are the likely spoken phrases mapped to an intent; you can add more.
Then retrieve the skill's unique ID: select Endpoint, then AWS Lambda ARN, and note down the skill ID. You need it later to link the skill to the Lambda function. The endpoint receives POST requests when a user interacts with your skill.
After saving the endpoint, open Interaction Model and click Build Model. Alexa runs background jobs to prepare the interaction model, much like building an Einstein Bot model in Salesforce, and validates and saves your changes.
3. The AWS Lambda function
Log in to your AWS account to create an IAM role and a Lambda function. An IAM role is an AWS identity with permission policies that set what it can and cannot do in AWS.
- Under Services, open IAM and click Roles to create a new role.
- Select AWS service as the type of trusted entity and Lambda as the use case.
- Attach the three required policies to the new role, then click Create Role.
- Back on the Services page, open Lambda and click Create Function. Select the new role under the execution role section.
- When the function is created, you land on its configuration tab. Click “+ Add trigger”, search for Alexa and select Alexa Skills Kit.
- Enter the skill ID you noted earlier and click Add.
- Upload the executable jar as the skill's back-end logic, either directly or from Amazon S3. I uploaded it to S3 and used the S3 location to update the function code. Click Save.
- Under Basic settings, replace the handler “example.Hello::handleRequest” with “com.geeksoft.handler.SalesforceAlexaSkillStreamHandler::handleRequest”.
Configure the handler to match your own Java class and method. This tells the Lambda runtime which handler method to invoke. Java handlers use the format package.Class::method; for example, com.main::Handler calls the Handler method defined in main.java.
4. Connect Lambda and the Alexa skill
Connect the function to the skill with its unique ARN: copy the ARN shown at the top right of the Lambda page, then in the Alexa developer console open Endpoint, paste the ARN as the default region and click Save Endpoints.
5. Test the skill
- In Lambda, click “Configure test events”, choose the Alexa Skills Kit “start session” event template, name the event and click Create.
- Click Test to run your back-end logic. If everything works, you see no errors.
- In the Alexa developer console, open the Test tab and set testing to Development. Use the Alexa simulator, with Skill I/O and Device Display ticked.
- Type a command, or hold the microphone button and speak. The response comes from your Java application; click play to hear the voice output.
- Ask about your Salesforce data: Alexa returns real-time results from Salesforce.
Testing from the mobile app
Now test from an Alexa-enabled device. Here we use the Alexa Android app (there is an equivalent for iOS). Log in with the same Amazon account you have used so far and enable your new skill from the Dev section. The skill is still in development: to make it available to other users, you first need Amazon certification and then publish it with the required details.
Tap the Alexa icon and ask the questions your custom skill can handle. For example:
- “Open geeksoft salesforce” returns “Welcome to Geeksoft Salesforce Demo”.
- “Ask geeksoft salesforce how many new cases do I have” returns “You have 6 New Cases in Salesforce”.
I hope this was helpful.
REFERENCES
First published on 28 September 2020 by GeekSoft Consulting.