Cloud Hub Alexa Skill Set -Command your IAAS to work for you
Tags: mulesofthackathon, alexa, anypointplatform, iaminvenio
Published: 2020-11-29 05:55:21

##Component Diagram

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/77kp6oz9723efg1efl4j.png)

##Sequence Diagram
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pjgzkh60i08to47jjmqt.png)

####Pre-requisite:
* [Amazon Developer Account](https://developer.amazon.com/en-US/alexa)
* [CloudHub Account](https://anypoint.mulesoft.com/login/)
* [VoiceFlow Account](https://www.voiceflow.com/)

##Alexa SDK
###The usecase of Alexa CloudHub Commander:
We have developed an Alexa skill which will make the life easier for the user just by listening to the command. Since it is a personalized skill it can hold the credentials and also maintains the session. The intuitive idea behind this is to command the IAAS for you. The user can ask for any information*(Ex: Server, API, application status, deployment status)* if authenticated otherwise need to authenticate first by providing their credentials.  Basically the skill is calling the CloudHub APIs internally and based on that provide the response. Apart from this some of the automation work can also be implemented in future such as deployment of the chatbot application in tomcat which is built on Spring Boot. (explained in the future extension section).

####Steps for Alexa Skill creation:
To create the Alexa skill there are several methods but in this case we are using voiceflow platform to create the skill and then uploading it to Alexa Developer Console. The reason behind using this platform to maintain the flow and also creating the SSML pattern in easiest way.

#####The steps to do this is as follows:
1.Login to your voiceflow account

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ckw7wwa4b2wujy6jzspp.png)

2.Create a new project.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/evb0js0a6lxvkbmehgn2.png)

3.Select the channel.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/evm9hmf49gy9ezbirtbn.png)

4.Give the invocation name and you are ready to create your skill.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/51lsqt18q9r7jq21e71k.png)

5.Create the flow of the skill using the available steps.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/exdscezf3f50557zmlg6.png)

6.A snapshot of CloudHub Alexa Skill Set flow.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vfxml48l8o0ufkb28t3l.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/m7et429tvmr6p799mv8o.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bfitp68jxx4qipnf1tr3.png)

7.Once the design is ready test it with using prototype tab.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mqnfuelw3cb9kopnq71v.png)

8.For the first session it will ask your credentials and then store it for another sessions. It also stores the bearer token for the session to call the CloudHub APIs.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/buth2vjhd90hp959pbvz.png)



9.Now you are ready to upload this skill on Alexa.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dnzz3jgbs8gnpok8ujkg.png)

10.Finally test is using the Alexa device or from the developer console.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9r19inezb9by97wgswtl.png)

>**Note** Sometimes it might not work properly from the console as it doesn't take the special character or capital letter used in the credentials. Will try to fix it soon.

####Endpoint:
For this we have used the HTTPs endpoint which is calling the CloudHub Apis.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1aal3buogolq3wd3ge2v.png)

#### Download the [source code](https://github.com/raz-inv/MuleSoft-Hackathon.git):

####Working of the program:
Steps are as follows:
1.For the first session, it asks users to link to the CloudHub account. Account linking can be invoked anytime by saying "Alexa! Link my CloudHub Account". The Alexa asks for user's credentials(User name and Password) and store it in session attributes. Next time if the users ask any query related to CloudHub, using these attributes Alexa directly response to those query. Once the user provides the credentials, the Alexa used these as utterances and then authenticate the user. After successful authentication user can asks any query related to CloudHub.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nagy9cy5t8cwjcf9aezn.png)

The post method is used to invoke the first API.
>https://anypoint.mulesoft.com/accounts/login?username={ch_user_name}&password={ch_password}

After successful authentication, it will generate a bearer_token which will be used as a parameter for invoking other APIs.
Copy the path of the response_token.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ga4o55np6xee55bky2tq.png)

Store this token as a variable and use it as an authorization token in the proceeding APIs.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tmmgv3yu7cunb9a4fbji.png)

2.Now call the account API to get the Organization Id linked to your CloudHub.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/736z1uu2rzxneqaxest0.png)

GET method is used to invoke this API.
>https://anypoint.mulesoft.com/accounts/api/me
Also use the same bearer_token as a Header parameter.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0zi7128jk6z55vtvdzsy.png)

Store this Organization_Id as a another path variable and use this as a header parameter for proceeding APIs.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nc7mrr2g9oywi25vsy6m.png)

3. Now call the environment API to get the Environment Id.

GET method is used to invoke this API.
>https://anypoint.mulesoft.com/accounts/api/organizations/{org_id}/environments

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fpmkb49o5cjr0rk0c1ub.png)

It will generate an enviornment_id.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/myjs2qwqugb03ehp5lud.png)

Store this environment_id as another path variable to use it in the APIs which requires env_id as a header parameter.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/45wcsc0k9cd4c058m41e.png)

4.Now finally to get into the Run Time manager of the anypoint platform and to know about the deployed applications in your CloudHub we use the applications API.

GET method is used to invoke this API.
>https://anypoint.mulesoft.com/cloudhub/api/v2/applications

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/06z1fhh04djy2l48gg80.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8m9zoxrsqcbv30xbrbao.png)

Now your Alexa is ready to response for any query related to CloudHUb. From here we can either invoke [CloudHub APIs](https://anypoint.mulesoft.com/exchange/portals/anypoint-platform/f1e97bc6-315a-4490-82a7-23abe036327a.anypoint-platform/cloudhub-api/minor/1.0/pages/home/) or [ARM REST Services](https://anypoint.mulesoft.com/exchange/portals/anypoint-platform/f1e97bc6-315a-4490-82a7-23abe036327a.anypoint-platform/arm-rest-services/minor/1.0/pages/home/).

* Error Handling:
The nature of robustness is also taken in consideration with error handling. If user tries to invoke any API which is not linked with the CloudHub Account, the Alexa tries to search the response from other APIs. For eg: User can get the server details from the Server as well as the Server Groups API.
If the authorization fails Alexa sends a respond by saying "Something went wrong! Please try again". And the Alexa again generates the bearer_token to get the response from API.

####AnyPoint Studio platform:
We have tested our skill for several CloudHub APIs..Some of them are mentioned below:
1.[Login] (https://anypoint.mulesoft.com/accounts/login)
2.[Accounts](https://anypoint.mulesoft.com/accounts/api/me)
3.[Organizations](https://anypoint.mulesoft.com/accounts/api/organizations/<<org_id>>/environments) 
4.[Applications](https://anypoint.mulesoft.com/cloudhub/api/v2/applications)
5.[Servers](https://anypoint.mulesoft.com/hybrid/api/v1/servers)

>CloudHub APIs Reference:
https://anypoint.mulesoft.com/exchange/portals/anypoint-platform/f1e97bc6-315a-4490-82a7-23abe036327a.anypoint-platform/cloudhub-api/minor/1.0/console/summary/

* Snapshot of applications already deployed in our anypoint platform

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2zhtxtu8krb87zohn5mq.png)

####Testing of ARM REST SERVICES.
Now lets test our skill with Server API.

GET method is used to invoke this API.
>https://anypoint.mulesoft.com/hybrid/api/v1/servers

This header contains all the three parameters (environment_id, organization_id and bearer_token).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6ksnorpafvbv0yxiufng.png)

Now click on the "Test Request" tab.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t7dco702b2q6go34j3lu.png)

Provide the values of all the parameters which you got earlier and then "Send Request".

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kuqexpppdoz75cp4t67i.png)

Hurrah! The API is working. Now you are ready to run your CloudHub Alexa Skill Set.

####Running the CloudHub Alexa Skill Set:
* Once you clone this skill from Github deploy it in the Alexa Developer Console and then change the endpoint as HTTPS.
* Invoke your skill using the invocation name(Alexa open first project). 
* In the permission tab gives the "customer name" permission for personalization of the skill.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/88yzzbi03ggyq5fbjupo.png)

* Save and build your model to reflect the changes.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ux95ypz22leracue5ek9.png)

After successful building you are ready to test this skill either from the console or from your Alexa device.

* Snapshot of testing the CloudHub Alexa Skill Set:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/52348wbsnyyqbqllvg51.png)




###Future Extension of the CloudHub Alexa Skill Set:
In future we can link the AIML based chatbot which internally calls the CloudHub APIs and using the NGROK we can call our services. It will integrate the Alexa SDK with the chatbot and gives user seamless effect of interaction. To know more about it please [read](https://dev.to/vikashinvenio/control-your-mule-infra-via-bot-2pbl) this post.