To build great Alexa skills, developers need to focus on building out the conversation. The goal of this tutorial is to show you how easy and fast it is to make data from any system accessible to an Alexa skill using ApiOmat.

To keep things simple, we’ll use a pre-made WordPress module to access data and pass it to Alexa using the ApiOmat Java SDK. In the end, users will be able to ask Alexa what our latest blog post is. Here’s what will be covered:

  • Setting up a new ApiOmat Backend
  • Connecting ApiOmat to WordPress using a module
  • Generating our new backend’s API & Java SDK
  • Using the SDK in an Alexa Skill to get information from WordPress
  • Testing the Skill with the Alexa Developer Console

 

Setting up the ApiOmat Backend

To get started, we’ll quickly set up a new backend in ApiOmat with the name “AlexaSkillBlog”.

Setting up the new ApiOmat Backend

Give your backend a name and click the plus icon.

Connecting to WordPress

Connect ApiOmat to Wordpress

Scroll down to the WordPress Module.

After creating the backend, we can add the WordPress Module from the Module market by clicking the plus and entering our WordPress Login, password and XML RPC URL.

URL for XML RPC

Enter your WordPress Credentials here and hit save.

Generating the REST API and SDKs

Once we’ve added the module, we can click on deploy and ApiOmat will update our backend – generating a REST API with the classes from the WordPress Module.

Generating the REST Api

Click Deploy.

ApiOmat Backend and API are deployed

Backend and API are deployed.

We could work directly with the generated API, but since we will be building our Alexa skill with an Amazon Lambda in Java (you can also do this in node.Js), we can use our backend’s individually generated Java SDK. Simply click on the Java logo on the SDK screen, hit download and import the zip file’s contacts into project.

Generate and download JAVA SKD for Alexa Skill

Click on SDKs, select Java and download SDK.

Setting up the Alexa Project

Since our Java project will be hosted as an Amazon Lambda function, it needs to be a Maven Project and we can import the Amazon Skills Kit by adding the following dependency to our maven project.

<dependencies>
     <dependency>
         <groupId>com.amazon.alexa</groupId>
         <artifactId>alexa-skills-kit</artifactId>
         <version>1.2</version>
     </dependency>
</dependencies>

Once we’ve done this, we can create a new class in our java project called “ApiOmatBlogSkillSpeechlet.java” which will implement the Speechlet class and its methods from the Amazon Skills Kit. The Speechlet file is where our logic will determine what the user’s intent is and what our response should be.

Before moving on, we’ll set up our new skill in the Alexa Skill Developer Console.

Since the majority of our blog articles are in German, we’ll be build a German skill. On the Interaction model, we need to define the Intent Schema and sample utterances. To keep the tutorial as simple as possible, we’ll use the follow intent Schema with the single intent of GetNewestBlogPost:

{
  "intents": [
    {
      "intent": "GetNewestBlogPost"
    }
  ]
}

For the sample utterances, we can use the following:
GetNewestBlogPost    Was ist neu Bei ApiOmat
GetNewestBlogPost    Was gibt’s neues bei ApiOmat
After that, we can hit next and go back to our java project.

In the Speechlet class we created, we need use the onIntent method so that our skill recognizes the request Alexa sends us when the user says “Was gibt’s neues bei ApiOmat?” (What’s new at ApiOmat?). When you have various intents, it will be a bit more complex, but the following code is enough to get started:

public SpeechletResponse onIntent(IntentRequest intentRequest, Session session) throws SpeechletException {
     Intent intent = intentRequest.getIntent();
     String intentName = (intent != null) ? intent.getName() : null;

     if ("GetNewestBlogPost".equals(intentName)) {
         return getLatestPost();
        } else {
         throw new SpeechletException("Invalid Intent");
     }
 }

As we can see, if the intent is equal to what we defined in the Alexa skill, we will call the method getLatest Blog Post. This is a new method in the Speechlet class that will we create that will define the String we will send back to Alexa. Within this method, we’ll also make a call to ApiOmat using the Java SDK to get the most recent blog article and add the title of it to the string we return to Alexa.

ApiOmat SDKs are really simple to use, take a look at our Android/Java cheat sheet here.

private SpeechletResponse getLatestPost() {

    //The start of our response 
    String starter = "Der neueste ApiOmat Blog Artikel ist: ";
    String title = ""; 

    //Create a new User or Sign-in and set up the connection to our backend
    //Note: the User class is from the ApiOmat SDK. The Alexa Skill Kit also has its own user.
     User user = new basics.User("username", "password");
     Datastore.configureWithCredentials(User.baseURL, User.apiKey, user.getUserName(), user.getPassword(), User.sdkVersion, User.system);

    //  Sign the user in, if user not in backend, create new user           
    try { 
       user.loadMe(); 
    } catch (ApiomatRequestException e) {
         try {
             user.save();
         } catch (ApiomatRequestException e1) {
             e1.printStackTrace();
         }
         e.printStackTrace();}

    //Get a lists of Posts from our WordPress Module
    //getPosts with the ApiOmat SDK query to get newest article
     try {
         List<Post> posts = Post.getPosts("limit 1 order by dateCreated DESC");
         Post newestPost = posts.get(0);
         title = newestPost.getTitle();
     } catch (ApiomatRequestException e){
         e.printStackTrace();
     }

     String speechText = "";
     if (title.length() > 1) {
         speechText = starter + title;
     } else { speechText = "Sorry, ich konnte nichts finden"; }

     // Create the Simple card content.
     SimpleCard card = new SimpleCard();
     card.setTitle("ApiOmat Blog");
     card.setContent(speechText);

     // Create the plain text output.
     PlainTextOutputSpeech speech = new PlainTextOutputSpeech();
     speech.setText(speechText);

    // Create reprompt
    Reprompt reprompt = new Reprompt();
    reprompt.setOutputSpeech(speech);

   return SpeechletResponse.newAskResponse(speech, reprompt, card); }

That’s all we have to write in our speechlet, there’s just one more thing to do before we can compile our project – create a class that implements the Amazon Skills Kit SpeechletStreamRequestHandler class This will simply handle the requests our Lambda receives and pass them on to the speechlet class we define.

public class ApiOmatBlogSpeechletStreamRequestHandler extends SpeechletRequestStreamHandler {
     private static final Set<String> supportedApplicationIds = new HashSet<String>();
     static {
         /*
          * This Id can be found on https://developer.amazon.com/edw/home.html#/ "Edit" the relevant
          * Alexa Skill and put the relevant Application Ids in this Set.
          */
         supportedApplicationIds.add("INSERT YOUR SKILL ID HERE");
     }

     public ApiOmatBlogSpeechletStreamRequestHandler() {
         super(new ApiOmatBlogSpeechlet(), supportedApplicationIds);
     }
 }

That’s all the code we have to write! Now we can compile our project as a jar-with-dependencies, which is required to run a lambda skill. After we upload our jar as an Amazon Lambda function for our skill, we can provide its ID to our Skill in the Alexa Skill Console. Once we’ve done this we can test our skill. If you have your own Echo or Echo dot, Amazon has a guide for setting up a test device. You can also use the test page in the Amazon Skill Console as shown below. Just enter an utterance and hit the button to ask your skill – the JSON requests from Alexa and the response from your Lambda are shown below.

{
  "version": "1.0",
  "response": {
    "outputSpeech": {
      "type": "PlainText",
      "text": "Der neueste ApiOmat Blog Artikel ist: COMPUTERWOCHE-Roundtable: Digitalisierung fordert auch die IT-Dienstleister heraus"
    },
    "card": {
      "content": "Der neueste ApiOmat Blog Artikel ist: COMPUTERWOCHE-Roundtable: Digitalisierung fordert auch die IT-Dienstleister heraus",
      "title": "ApiOmat Blog",
      "type": "Simple"
    },
    "reprompt": {
      "outputSpeech": {
        "type": "PlainText",
        "text": "Der neueste ApiOmat Blog Artikel ist: COMPUTERWOCHE-Roundtable: Digitalisierung fordert auch die IT-Dienstleister heraus"
      }
    },
    "shouldEndSession": false
  },
  "sessionAttributes": {}
}

There we have it.

Share this Article

Leave a Comment

About EASY SOFTWARE
Die EASY SOFTWARE ist Ihr Partner für eine durchgängige Digitalisierung von Geschäftsprozessen. Als Anbieter ebenso zukunftsweisender wie erfolgreicher Softwareprodukte und Servicelösungen, macht EASY Ihr Geschäft einfacher, effizienter und transparenter.
EASY SOFTWARE

That was EASY WORLD 2020
100% Digital

A day full of knowledge transfer, exchange and inspiration for your digitization. All lectures of EASY WORLD 2020 are available as recordings on our website. Register once and take a look at them at any time.

Watch the videos
This might also interest you:
Legacy systems must be replaced - on to new shores
Replacing legacy systems
Customer portal benefits that delight users on any device
How customer portals help with digital customer retention
Processing incoming invoices with SAP - makes life easier
Processing Incoming Invoices in SAP: Three Challenges, One Digital Solution
Back to topic Next Post