Add Captains counter intent for Alexa

This commit is contained in:
Alex Ellis
2016-12-30 20:06:02 +00:00
parent d181fed5fd
commit 3775506184
9 changed files with 164 additions and 0 deletions

View File

@ -0,0 +1,12 @@
apk add nodejs curl --update
time cat request.json | node handler.js -
cd /root
time cat request.json | node handler.js -
time (cat request.json | node handler.js -)
time (cat request.json | node handler.js )
cat > run.sh
chmod +x run.sh
.r/
./r
./run.sh
time ./run.sh

View File

@ -0,0 +1,12 @@
FROM alpine:latest
RUN apk --update add nodejs
COPY ./fwatchdog /usr/bin/
COPY package.json .
COPY handler.js .
COPY parser.js .
COPY sample.json .
RUN npm i
ENV fprocess="node handler.js"
CMD ["fwatchdog"]

View File

@ -0,0 +1,48 @@
"use strict"
let fs = require('fs');
let sample = require("./sample.json");
let cheerio = require('cheerio');
let Parser = require('./parser');
var request = require("request");
const getStdin = require('get-stdin');
getStdin().then(content => {
let request = JSON.parse(content);
handle(request, request.request.intent);
});
function tellWithCard(speechOutput) {
sample.response.outputSpeech.text = speechOutput
sample.response.card.content = speechOutput
sample.response.card.title = "Captains";
console.log(JSON.stringify(sample));
process.exit(0);
}
function handle(request, intent) {
createList((sorted) => {
let speechOutput = "There are currently " + sorted.length + " Docker captains.";
tellWithCard(speechOutput);
});
}
let createList = (next) => {
let parser = new Parser(cheerio);
request.get("https://www.docker.com/community/docker-captains", (err, res, text) => {
let captains = parser.parse(text);
let valid = 0;
let sorted = captains.sort((x,y) => {
if(x.text > y.text) {
return 1;
}
else if(x.text < y.text) {
return -1;
}
return 0;
});
next(sorted);
});
};

View File

@ -0,0 +1 @@
docker build -t captainsintent . ; docker service rm CaptainsIntent ; docker service create --network=functions --name CaptainsIntent captainsintent

View File

@ -0,0 +1,17 @@
{
"name": "CaptainsIntent",
"version": "1.0.0",
"description": "",
"main": "handler.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cheerio": "^0.22.0",
"get-stdin": "^5.0.1",
"request": "^2.79.0"
}
}

View File

@ -0,0 +1,32 @@
"use strict"
module.exports = class Parser {
constructor(cheerio) {
this.modules = {"cheerio": cheerio };
}
sanitize(handle) {
let text = handle.toLowerCase();
if(text[0]== "@") {
text = text.substring(1);
}
if(handle.indexOf("twitter.com") > -1) {
text = text.substring(text.lastIndexOf("\/")+1)
}
return {text: text, valid: text.indexOf("http") == -1};
}
parse(text) {
let $ = this.modules.cheerio.load(text);
let people = $("#captians .twitter_link a");
let handles = [];
people.each((i, person) => {
let handle = person.attribs.href;
handles.push(this.sanitize(handle));
});
return handles;
}
};

View File

@ -0,0 +1,24 @@
{
"session": {
"sessionId": "SessionId.8b812aa4-765a-47b6-9949-b203e63c5480",
"application": {
"applicationId": "amzn1.ask.skill.72fb1025-aacc-4d05-a582-21344940c023"
},
"attributes": {},
"user": {
"userId": "amzn1.ask.account.AETXYOXCBUOCTUZE7WA2ZPZNDUMJRRZQNJ2H6NLQDVTMYXBX7JG2RA7C6PFLIM4PXVD7LIDGERLI6AJVK34KNWELGEOM33GRULMDO6XJRR77HALOUJR2YQS34UG27YCPOUGANQJDT4HMRFOWN4Z5E4VVTQ6Z5FIM6TYWFHQ2ZU6HQ47TBUMNGTQFTBIONEGELUCIEUXISRSEKOI"
},
"new": true
},
"request": {
"type": "IntentRequest",
"requestId": "EdwRequestId.cabcff07-8aaa-4fe6-aca0-94440051fc89",
"locale": "en-GB",
"timestamp": "2016-12-30T19:57:45Z",
"intent": {
"name": "CaptainsIntent",
"slots": {}
}
},
"version": "1.0"
}

View File

@ -0,0 +1,2 @@
#!/bin/sh
cat request.json | node handler.js -

View File

@ -0,0 +1,16 @@
{
"version": "1.0",
"response": {
"outputSpeech": {
"type": "PlainText",
"text": "There's currently 6 people in space"
},
"card": {
"content": "There's currently 6 people in space",
"title": "People in space",
"type": "Simple"
},
"shouldEndSession": true
},
"sessionAttributes": {}
}