Skip to content
Snippets Groups Projects
Commit 37755061 authored by Alex Ellis's avatar Alex Ellis
Browse files

Add Captains counter intent for Alexa

parent d181fed5
No related branches found
No related tags found
No related merge requests found
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
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"]
"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);
});
};
docker build -t captainsintent . ; docker service rm CaptainsIntent ; docker service create --network=functions --name CaptainsIntent captainsintent
{
"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"
}
}
"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;
}
};
\ No newline at end of file
{
"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"
}
#!/bin/sh
cat request.json | node handler.js -
{
"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": {}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment