From 377550618483154d2b596a70fbe36c4836309684 Mon Sep 17 00:00:00 2001 From: Alex Ellis Date: Fri, 30 Dec 2016 20:06:02 +0000 Subject: [PATCH] Add Captains counter intent for Alexa --- sample-functions/CaptainsIntent/.ash_history | 12 +++++ sample-functions/CaptainsIntent/Dockerfile | 12 +++++ sample-functions/CaptainsIntent/handler.js | 48 ++++++++++++++++++++ sample-functions/CaptainsIntent/oneshot.txt | 1 + sample-functions/CaptainsIntent/package.json | 17 +++++++ sample-functions/CaptainsIntent/parser.js | 32 +++++++++++++ sample-functions/CaptainsIntent/request.json | 24 ++++++++++ sample-functions/CaptainsIntent/run.sh | 2 + sample-functions/CaptainsIntent/sample.json | 16 +++++++ 9 files changed, 164 insertions(+) create mode 100644 sample-functions/CaptainsIntent/.ash_history create mode 100644 sample-functions/CaptainsIntent/Dockerfile create mode 100644 sample-functions/CaptainsIntent/handler.js create mode 100644 sample-functions/CaptainsIntent/oneshot.txt create mode 100644 sample-functions/CaptainsIntent/package.json create mode 100644 sample-functions/CaptainsIntent/parser.js create mode 100644 sample-functions/CaptainsIntent/request.json create mode 100755 sample-functions/CaptainsIntent/run.sh create mode 100644 sample-functions/CaptainsIntent/sample.json diff --git a/sample-functions/CaptainsIntent/.ash_history b/sample-functions/CaptainsIntent/.ash_history new file mode 100644 index 00000000..b7b9e18e --- /dev/null +++ b/sample-functions/CaptainsIntent/.ash_history @@ -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 diff --git a/sample-functions/CaptainsIntent/Dockerfile b/sample-functions/CaptainsIntent/Dockerfile new file mode 100644 index 00000000..5e5a08cb --- /dev/null +++ b/sample-functions/CaptainsIntent/Dockerfile @@ -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"] diff --git a/sample-functions/CaptainsIntent/handler.js b/sample-functions/CaptainsIntent/handler.js new file mode 100644 index 00000000..7b9b3b30 --- /dev/null +++ b/sample-functions/CaptainsIntent/handler.js @@ -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); + }); +}; diff --git a/sample-functions/CaptainsIntent/oneshot.txt b/sample-functions/CaptainsIntent/oneshot.txt new file mode 100644 index 00000000..8d847158 --- /dev/null +++ b/sample-functions/CaptainsIntent/oneshot.txt @@ -0,0 +1 @@ +docker build -t captainsintent . ; docker service rm CaptainsIntent ; docker service create --network=functions --name CaptainsIntent captainsintent diff --git a/sample-functions/CaptainsIntent/package.json b/sample-functions/CaptainsIntent/package.json new file mode 100644 index 00000000..050cd418 --- /dev/null +++ b/sample-functions/CaptainsIntent/package.json @@ -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" + } +} diff --git a/sample-functions/CaptainsIntent/parser.js b/sample-functions/CaptainsIntent/parser.js new file mode 100644 index 00000000..70ef7284 --- /dev/null +++ b/sample-functions/CaptainsIntent/parser.js @@ -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; + } +}; \ No newline at end of file diff --git a/sample-functions/CaptainsIntent/request.json b/sample-functions/CaptainsIntent/request.json new file mode 100644 index 00000000..475cc811 --- /dev/null +++ b/sample-functions/CaptainsIntent/request.json @@ -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" +} diff --git a/sample-functions/CaptainsIntent/run.sh b/sample-functions/CaptainsIntent/run.sh new file mode 100755 index 00000000..9a4d296f --- /dev/null +++ b/sample-functions/CaptainsIntent/run.sh @@ -0,0 +1,2 @@ +#!/bin/sh +cat request.json | node handler.js - diff --git a/sample-functions/CaptainsIntent/sample.json b/sample-functions/CaptainsIntent/sample.json new file mode 100644 index 00000000..27fc86ca --- /dev/null +++ b/sample-functions/CaptainsIntent/sample.json @@ -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": {} +}