Giter Site home page Giter Site logo

alexaskillhandler's Introduction

AlexaSkillHandler

A simple handler library for fast and simple skill development in java.

How to use

Creating your application

Create a new instance of SkillPipeline, add your SkillHandler's and pipe the verified json requests from Amazon via the methode pipe(String) into your SkillPipeline object.

SkillPipeline myPipeline = new SkillPipeline();
myPipeline.addHandler(new SkillHandler(){
  public SkillResponse handle(SkillRequest request){
    if(request.getRequest().getType()==SkillRequest.RequestType.IntentRequest){
      if(request.getRequest().getIntent().getName().equals("YourIntent")){
        return SkillResponse.builder().speech("Hello World").build();
      }
    }
    return null;
  }
});
String jsonResponse = myPipeline.pipe(jsonRequest);

Building your application

To build your application you need to add this library and google's gson library to the build path and include both into your built application. The minimum required language level is 8. You can let maven automatically manage the required dependencies for you.

<repositories>
  <repository>
    <id>bebendorf</id>
    <url>http://repo.bebendorf.eu/</url>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>eu.bebendorf</groupId>
    <artifactId>AlexaSkillHandler</artifactId>
    <version>1.0</version>
    <scope>compile</scope>
  </dependency>
  <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.2</version>
    <scope>compile</scope>
  </dependency>
</dependencies>

Example using SparkJava

import static spark.Spark.*;

public class Example {
    public static void main(String [] args){
        SkillPipeline exampleSkill = new SkillPipeline(); //Initialize the pipeline
        exampleSkill.addHandler(new SkillHandler() { //Add the handler
            public SkillResponse handle(SkillRequest request) {
                if(request.getRequest().getType()!=SkillRequest.RequestType.IntentRequest) //Only handle the IntentRequest request type
                    return null;
                SkillRequest.Intent intent = request.getRequest().getIntent();
                if(!intent.getName().equals("AddIntent")) //Only handle the AddIntent
                    return null;
                if(!(intent.verifySlot("addend1")&&intent.verifySlot("addend2"))) //Check if both slots are given and have a value
                    return SkillResponse.builder().speech("You have to give me 2 numbers!").build();
                int a1 = intent.getSlot("addend1").asInt();
                int a2 = intent.getSlot("addend2").asInt();
                int sum = a1 + a2;
                return SkillResponse.builder().speech(a2+" added to "+a1+" equals "+sum).build(); //Build and return the response
            }
        });
        post("/",(request, response) -> { //Map the post request
            // <-- HERE YOU SHOULD VERIFY THE REQUEST IS VALID AND AUTHORIZED
            return exampleSkill.pipe(request.body()); //Pipe the request into the pipeline
        });
        init(); //Initialize the webserver
    }
}

Used libraries

alexaskillhandler's People

Contributors

janholger avatar

Stargazers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.