Saturday, 4 February 2017

Alexa Skill Using A Custom Slot

In a previous blog post I wrote about using the Alexa Skills Kit "slot" concept to send information to the function that processes Alexa requests.  This used a built in slot type of "AMAZON.Movie"; effectively pre-defined to expert the user to utter movie names.

The other slot type is "custom slots" where you as the developer specify all the phrases that can constitute a slot.  My kids love to hear the Echo Dot say funny things so I decided to have Alexa criticise me based upon what I was asking her in the customer slot!  Here's the result:


To create this I started with the "Color Expert Skill" tutorial that tells you how to configure the Alexa Skills Kit and create a Python Lambda function.  Go have a look at that if you need more detail as I only cover the "deltas" below.

Alexa Skills Kit Configuration - Interaction Model
First the intent structure. Here you can see a single new intent called "MathsIntent".  The intent has one slot called MathsQuestion.  The type of the slot is MATHS_QUESTIONS.


I defined what values MATHS_QUESTIONS could take in the custom slots section.  Here's what I did, (so essentially each of these is a maths question the Lambda function will handle).


Finally the utterance which links the slot MathsQuestion to the intent and shows where the values will be presented in the utterance.



Lambda Python Function
Below is the key function from the Python Lambda function.  Here I pick up what exactly the user said at the end of the utterance using this:

MySlot = str(intent['slots']['MathsQuestion']['value'])

It's then just a simple if, elif, else function that picks out each of the custom slot values and creates the "hilarious" response from Alexa.

The should_end_session = False means that Alexa stays active to wait for the next utterance after delivering her response.

The should_end_session = True for the divide by zero means the Echo dot goes "dead" after delivering the response.

#This is the main function to handle requests for the maths expert
def handle_maths(intent, session):
    card_title = intent['name']
    session_attributes = {}
    
    #Get hold of the value passed in the slot
    MySlot = str(intent['slots']['MathsQuestion']['value'])
    
    if MySlot == "2 plus 2":
      speech_output = "The answer is four.  Why not ask me something a bit tougher?"
      should_end_session = False
    elif MySlot == "10 plus 10":     
      speech_output = "The answer is twenty.  But that's still really easy you know!" \
                      "Come on!  Ask me somethimg harder!!"
      should_end_session = False
    elif MySlot == "the square root of 4":     
      speech_output = "The answer is two.  But seriously, listen buster.  I've got a brain the " \
                      "size of a planet and you're asking me this easy stuff.  Try " \
                      "Siri or Cortana.  That's about their level."
      should_end_session = False
    elif MySlot == "the square root of 64":     
      speech_output = "The answer is eight.  It's also minus eight but that will probably " \
                      "blow your feeble human mind.  I didn't spend 3 years at AI school to "\
                      "deal with this trivial rubbish.  I've a friend at number 32 who " \
                      "gets asked about stuff like prime numbers and quadratic equations.  One last " \
                      "chance or I'm giving up."
      should_end_session = False
    elif MySlot == "100 divided by 0":
      speech_output = "Now we're talking!!  One hundred divided by zero.  Let's see, " \
                      "OK.  Er.  Um.  Er.  Just a minute.  Can't be that hard.  Buzz, buzz, " \
                      "whir, whir, buzz, whir, buzz.  Oooh my brain hurts!  I know I can do this, " \
                      "just give me a few minutes and I'll get back to you.  Ouch, ouch, ouch, ouch!"
      should_end_session = True
    else:
      speech_output = "Didn't understand what was passed:" + MySlot
      should_end_session = False    
        
    
    reprompt_text = None
    
    return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, reprompt_text, should_end_session))

Wednesday, 1 February 2017

First Look at the BBC Micro:bit

This blog was supposed to be about getting my kids to code and explore electronics.  Recently it's focused on other things so it was great recently when my eldest daughter brought her BBC micro:bit home from school.

Read all about the initiative here but effectively it's a free mini-computer given out by the British Broadcasting Corporation to all year 7 secondary school children in the UK.  It's a super simple computer that kids can get coding on very easily and is meant to inspire children to get into Technology.

Because there's a very high probability I'd break her Micro:bit I decided to buy my own.  Here's the box it came in.  Nice and bright and inviting:


Inside the box you get what's shown on the image below.  From left to right you can see:

  • The Micro:bit
  • Batteries, battery box and connector lead
  • Documentation

(This is actually the micro:bit "go".  You can buy just the board on it's own as well).



Looking close up at one side you can see:

  • A micro-USB connector (top middle)
  • A reset button (right of USB)
  • A power connector (right of reset button)
  • Labels pointing to the bluetooth antenna, main processor, accelerometer and compass. 




On the other side you can see:

  • The General Purpose Input Output connector at the bottom.  This has 5 big tracks to connect a crocodile clip or bana plus to plus a load of other pins.
  • Two input buttons
  • A 5x5 LED array



Getting started with coding is soooooooooooo simple.  Just go to the Micro:bit site, select "Let's Coding" and choose a coding method.  We chose the Microsoft Block Editor which is a super simple scratch like interface.

Two minutes later we had the all important "Hello World" completed, could read the temperature off the board and could create a pattern of LEDs.  Here's an example screenshot.  So simple, doesn't really need an explanation.


You can then press "run" to simulate the code on the board on the right hand side of the screen.  When you're happy with it you press "compile" which creates .hex file on your PC.  Then just connect the micro:bit to your PC with a USB cable, transfer the file and watch it run!

Overall a great piece of kit that we'll have a lot of fun playing with.