iOS

Integrating Alexa skills for IoT mobile solutions(iOS)

Abilash Balasubramanian

14 Nov 2017

Amazon Alexa overview

Amazon Alexa is an intelligent personal assistant developed by Amazon. It is capable of performing: voice-interaction, music, to-do lists, setting alarms, audio streaming podcast, playing books and weather, real-time traffic. Alexa you can also use to control several smart home devices which itself as a home automation system (IoT).

Most IoT devices with Alexa supported will allow users to activate the device using a wake-word, other devices (such as the companion mobile apps on both iOS/Android platform) uses a push button to activate Alexa’s voice service. Even it is possible to manufacture a commercial product with Alexa skills.

Developing Alexa skills

    Alexa skills can be thought of like App assistant for your phone. A new custom skill gives Alexa more capabilities like keeping you up to date information on your phone which includes, weather, flash news and also work well in smart home skills, i.e., turning on your lights or the ability to play music! With you.

There are different types of skills development provided by Amazon Alexa for a developer:

  • Custom Skills,

  • Smart Home Skills,

  • Flash Briefing Skills,

  • Video Skills

Experimenting an Amazon Alexa skill using the Alexa Skills Kit, Node.js, and AWS Lambda has become one of the popular ways create custom skills. The nonblocking input/output operation of Node.js is well suited for an Alexa skill development. Addition with node.js, AWS Lambda is free for the one million calls per month, which is good enough for most developers to get the best experience of serverless connection.

The advantage of AWS Lambda over other servers:

  1. AWS Lambda service reduces the complexity of setting up and managing Alexa skill endpoint:

  2. Lambda does not need any admin or a management person in charge to compute its service,

  3. This does not need an SSL certificate, which requires in any server operation,

  4. AWS Lambda supports codes include, Node.js, Python, Java or C#,

  5. Lambda runs your skill code only when you need it and scales with your request and requirement; this leads to the use of continuous run servers.

Interaction Model – Intents, Slots, speech Utterances:

The model configuration for a custom Alexa skill includes the following components requires:

  • An Intent Schema is a JSON structure which defined to be a set of custom intents service can process the response,

JSON Structure:

CqGY09DX0253DUwNYoSJbFlHv5q MCMUvhMeBrM1 5oCbBgakh5SuGBKlRu5siVkJ2blmIZ4rfa aXeyJXoTNjEUTKBcn6gy7rV

  • The Spoken Input Data:

    • Sample Utterances:

    • Custom Values (i.e., custom slots and custom slot types)

Now you can also create your custom interaction model and managing skill from developer portal.

For guidelines, please visit: https://developer.amazon.com/docs/custom-skills/register-and-manage-alexa-skills-in-the-developer-portal.html

Persistence: Some of the database service involved to persist the Alexa session attribute:

  1. Amazon DynamoDB:  Many of you would like to persist session attribute values into storage for further use. Alexa-SDK integrates directly with Amazon DynamoDB (a NoSQL database service) to enable you to do this with a single line of code.

DynamoDB code snippet:

exports.handler = function (event, context, callback) {
 
  var alexa = Alexa.handler(event, context, callback);
 
  alexa.appId = appId;
 
  alexa.dynamoDBTableName = 'YourTableName'; // That's it!
 
  alexa.registerHandlers(State1Handlers, State2Handlers);
 
  alexa.execute();
 
 };
  1. Firebase Real-time database: Store and sync data with NoSQL cloud database. Data is synced across all clients in Realtime.

Firebase Real-time DB code snippet:

var dbref;
 
 
 
 var Firebase = require(‘firebase’);
 
 
 
 exports.handler = function (event, context, callback) {
 
  var alexa = Alexa.handler(event, context, callback);
 
 
 
 var config = {
 
 
 
  apiKey: "apiKey",
 
 
 
  authDomain: "projectId.firebaseapp.com",
 
 
 
  databaseURL: "https://databaseName.firebaseio.com"
 
 
 
 };
 
 
 
  Firebase.initializeApp(config);
 
 
 
  alexa.appId = appId;
 
 
 
  var dbref = Firebase.database().ref('YourTableName/');
 
 
 
  alexa.registerHandlers(handlers);
 
 
 
  alexa.execute();
 
 };
 
 
 
 

Account Linking:

Account linking is needed for some custom skills, to connect the identity of the end user. This will create a link between the Amazon Alexa user and the product user (Companion app user). Some of the custom skills must require account linking is:

  • Smart home Skills,

  • Wallet accessing skills,

  • Personal/Messaging skills.

This account linking uses Authorization code grant flow(OAuth2) to connect Alexa end user with their cloud account.

For more details: https://developer.amazon.com/docs/custom-skills/link-an-Alexa-user-with-a-user-in-your-system.html

Testing a custom Alexa skill:

During the development process, there are multiple ways to test your new skill:

  • Service Simulator on the webpage.
  • Test by voice using an Alexa-enabled device(Alexa echo etc.),
  • Adding test event(JSON schema) inside AWS lambda function and run Lambda test,
  • Developer Alexa skill testing, once you complete your intent and speech utterances you can now able to do Beta testing for particular email-IDs.

Enable Alexa skill to Amazon account:

Skills are voice-driven capabilities that enhance the functionality of your Alexa devices.To enable Alexa skills:

  1. Open the Alexa app, go to the menu, and select Skills. Or, go to the Alexa Skills store on the Amazon website: https://www.amazon.com/skills.

  2. Browse for skills by category or use Search to find a specific skill.

amazon-echo-skills-screens-01.jpeg

Integration of your Alexa Voice(AVS) to an iOS app:

To integrate with Alexa Voice Services(AVS), you first need to go to the Amazon Developer Portal and get certain specific keys from security profile:

  • Application Type ID,

  • Bundle Id,

  • API Key

To get started:  Register your product.

  1. Application Type ID, Bundle Id, and API Key: Under security, profile section creates an iOS app for your unique.

  • iOSSettings

Be sure to keep track of the Application Type ID, API Key, and Bundle Id. We are going to need these later on when we setup our iOS app.

  2. Download LoginWithAmazon framework from Amazon portal,

  3. Include LoginWithAmazon.framework, under Target section, select Build Phases. Expand Link Binary with Libraries and click the plus sign to add a LoginwithAmazon library.

  4. Adding your API key to your iOS app info.plist:

APIKey

”Your API key”

Open source code edit of info.plist and add above the line and replace Your API key with API key which you got from creating product-> Security profile-> iOS app.

  5. Creating URL schema: The value is your bundle ID with amzn- prepended (for example, amzn-com.example.app). You can find your bundle ID listed as Bundle identifier in the property list.

Screen Shot 2017-11-13 at 4.47.33 PM.png

Code snippet for Login with Amazon:

- (void)loginWithAmazon
 
 
 
 {
 
 
 
  [AIMobileLib authorizeUserForScopes:@[@"alexa:all",@"profile"] delegate:self options:@{kAIOptionScopeData:SCOPE_DATA}];
 
 
 
 }
 
 
 
 - (void)logout
 
 
 
 {
 
 
 
  [AIMobileLib clearAuthorizationState:self];
 
 
 
 }
 
 
 
 #pragma mark - Amazon failure delegate
 
 
 
 - (void)requestDidFail:(APIError *)errorResponse
 
 
 
 {
 
 
 
  NSLog(@"APIError === %@",errorResponse.error.message);
 
 
 
 }
 
 
 
 #pragma mark - Amazon success delegate
 
 
 
 - (void)requestDidSucceed:(APIResult *)apiResult;
 
 
 
 {
 
 
 
  switch (apiResult.api) {
 
 
 
  case kAPIAuthorizeUser:
 
 
 
  {
 
 
 
  [AIMobileLib getAccessTokenForScopes:@[@"alexa:all",@"profile"] withOverrideParams:nil delegate:self];
 
 
 
  }
 
 
 
  break;
 
 
 
  case kAPIGetAccessToken:
 
 
 
  {
 
 
 
  NSString *accessToken = apiResult.result;
 
 
 
  NSLog(@"Login success with access token === %@",accessToken);
 
 
 
  [self fetchUser];
 
 
 
  }
 
 
 
  break;
 
 
 
  default:
 
 
 
  break;
 
 
 
  }
 
 
 
 }
 
 
 
 Fetching user details to retrieve raw data from DB: 
 
 
 
 - (void)fetchUser
 
 
 
 {
 
 
 
  [AMZNUser fetch:^(AMZNUser *user, NSError *error) {
 
 
 
  if (error) {
 
 
 
  } else if (user) {
 
 
 
  }
 
 
 
  }];
 
 
 
 }

Alexa voice service(AVS) for an iOS app:

  • Integrate Alexa Into Your Product: Use the Alexa Voice Service (AVS) to add intelligent voice control to any connected product that has a microphone and speaker which includes a mobile phone.

Note: You have to call the Timezone involved Alexa HTTP API to get a successful callback. For Instance, if you are creating a Custom skill for US region, then you need to call the US http API inside the app.

AVS exposes HTTP API service which used for multipart messages encoded for HTTP/2  provided by Amazon which available for following countries includes:

BwsD lIU8EysbgQZQuKxGulDMqUQSwa6MtNtfICaAqLRuGGzyajxyhbovUarzOK0CXWkYbYfjSq79YPopU0tXHhAzaME Gu7yJXi SECJ0bV7pvGMIafXcJ1TXpoB4 igyL oU

Structuring event request format for AVS:

    AVS connection is established, will help to communicate with your client Alexa using HTTP/2 multipart message.

The encoded multipart message is formed from two different parts:

  • JSON formatted event,

  • Captured Audio from device microphone.

9SfgVlrn8rw2Mt jqEnunnMKgQn 6G66oR3nc00owtBIxR3rtwIJ 0onYsjmO9F Qv0RORuz1jXUASOGatcfh2dBO6ABrndf y2d4sPtaxlp VjnrEmfnEXKboEZoZAMcZFWNNw

AVS HTTP/2 message header schema:

 

8rKAsz P1BrCGBzXF5CC2Iv3cRBjT31hpT1ySdh3hGBJg aUZgx 9SduT QkdVVY87l1dgwiKW

AVS HTTP/2 multipart message schema:

AVS encoded multipart message composed of 1 or more JSON formatted events and their associated raw data(Binary format) audio attachment.

Multipart JSON format:

YeB5rB9 I1zL1gVh2b95XZtx7HFKqbcOQ8 vfa

 

More information on how HTTP/2 API works in AVS: https://developer.amazon.com/docs/alexa-voice-service/api-overview.html

 

Complete working with AVS inside the mobile app:

alexa_skills_kit_architecture_diagram.png

Related read:

1) Alexa Account Linking: Follow this 4 step Linking Process

2) Top 15 and best Alexa skills that every Echo user should know

3) How to use Alexa on the Apple Watch