Laurence Daxes
Published © MIT

Alexa Pi YouTube Channel Subscribers

An interesing Skill for Alexa running over a Raspberry Pi, this skill able to Alexa to tell you the total of subscribers to your YT Channel.

IntermediateProtip2 hours1,438

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
USB Sound Dongle
×1
Speaker: 0.25W, 8 ohms
Speaker: 0.25W, 8 ohms
×1

Software apps and online services

AWS Lambda
Amazon Web Services AWS Lambda
Alexa Skills Kit
Amazon Alexa Alexa Skills Kit

Story

Read more

Schematics

Alexa AppKit Architecture

Code

IntentSchema.json

JSON
{
  "intents": [
    {
      "intent": "DxsIntent"
    },
    {
      "intent": "AMAZON.HelpIntent"
    }
  ]
}

SampleUtterances.txt

Plain text
DxsIntent Hello
DxsIntent Tell me
DxsIntent How many subscribers have the Channel
DxsIntent How many subscribers have my Channel
DxsIntent How many subscribers have the Laurence Urban's Channel
DxsIntent Tell me the number of subscribers
DxsIntent Tell me the number of my subscribers

index.js

JavaScript
/**
    Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.

    Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at

        http://aws.amazon.com/apache2.0/

    or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

/**
 * This simple sample has no external dependencies or session management, and shows the most basic
 * example of how to create a Lambda function for handling Alexa Skill requests.
 *
 * Examples:
 * One-shot model:
 *  User: "Alexa, tell Greeter to say hello"
 *  Alexa: "Hello World!"
 */

/**
 * App ID for the skill
 */
var APP_ID = undefined; //replace with "amzn1.echo-sdk-ams.app.[your-unique-value-here]";

/**
 * The AlexaSkill prototype and helper functions
 */
var AlexaSkill = require('./AlexaSkill');
var request = require('sync-request');


/**
 * DxsSkill is a child of AlexaSkill.
 * To read more about inheritance in JavaScript, see the link below.
 *
 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript#Inheritance
 */
var DxsSkill = function () {
    AlexaSkill.call(this, APP_ID);
};

// Extend AlexaSkill
DxsSkill.prototype = Object.create(AlexaSkill.prototype);
DxsSkill.prototype.constructor = DxsSkill;

DxsSkill.prototype.eventHandlers.onSessionStarted = function (sessionStartedRequest, session) {
    console.log("DxsSkill onSessionStarted requestId: " + sessionStartedRequest.requestId
        + ", sessionId: " + session.sessionId);
    // any initialization logic goes here
};

DxsSkill.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
    console.log("DxsSkill onLaunch requestId: " + launchRequest.requestId + ", sessionId: " + session.sessionId);
    var speechOutput = "Welcome to the Laurence & Urban Music Youtube Subscriber. You can say: Hello, or ask me: How many subscribers have the Channel";
    var repromptText = "You can say: Hello, or ask me: How many subscribers have the Channel";
    response.ask(speechOutput, repromptText);
};

DxsSkill.prototype.eventHandlers.onSessionEnded = function (sessionEndedRequest, session) {
    console.log("DxsSkill onSessionEnded requestId: " + sessionEndedRequest.requestId
        + ", sessionId: " + session.sessionId);
    // any cleanup logic goes here
};

DxsSkill.prototype.intentHandlers = {
    // register custom intent handlers
    "DxsIntent": function (intent, session, response) {
        
        var id_channel = 'UCR07D-ouTLdA2g46IjWTWvg';
        var key_api = 'AIzaSyBoaE3GVoyvATA...';
        var url_api = 'https://www.googleapis.com/youtube/v3/channels?part=statistics&id='+ id_channel +'&key=' + key_api;

        var res = request('GET', url_api );
        var data = JSON.parse(res.getBody('utf8'));
        //console.log(data.query.results.feed.entry.length);
        if(data.error){
            var speech = 'An error occurred while getting the information';
        }else{
            var item = data.items[0];
            //console.log(item.statistics.viewCount);
            var subscribers = item.statistics.subscriberCount;            
            var speech = 'The channel has ' + subscribers + ' subscribers';
        }        
        
        response.tellWithCard(speech, "Youtube Subscribers", speech);
        
    },
    "AMAZON.HelpIntent": function (intent, session, response) {
        response.ask("You can ask me: How many subscribers have the Channel", "You can ask me: How many subscribers have the Channel");
    }
};

// Create the handler that responds to the Alexa Request.
exports.handler = function (event, context) {    
    var dxsSkill = new DxsSkill();
    dxsSkill.execute(event, context);
};

response.json

JSON
{
 "kind": "youtube#channelListResponse",
 "etag": "\"mie-I9wWQF7ndS7wC10DLBkzLlg/MqGX2Ngzgzf66podQ_BwYBnywoM\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#channel",
   "etag": "\"mie-I9wWQF7ndS7wC10DLBkzLlg/A44MvMbIseZg-zhgBtvjJUEi6xI\"",
   "id": "UCR07D-ouTLdA2g46IjWTWvg",
   "statistics": {
    "viewCount": "3149920",
    "commentCount": "1",
    "subscriberCount": "960",
    "hiddenSubscriberCount": false,
    "videoCount": "34"
   }
  }
 ]
}

Credits

Laurence Daxes

Laurence Daxes

19 projects • 36 followers
Systems Engineer Bachellor, I Love technology, and IoT World https://youtube.com/c/DaxesHacks

Comments