mirror of
https://github.com/openfaas/faas.git
synced 2025-06-10 17:26:47 +00:00
Add changecolorintent
This commit is contained in:
parent
8734a3a5ac
commit
4573f14f65
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
fwatchdog
|
||||
**/node_modules/
|
||||
|
@ -116,10 +116,9 @@ func makeProxy(metrics metrics.MetricOptions) http.HandlerFunc {
|
||||
len(alexaService.Request.Intent.Name) > 0 {
|
||||
fmt.Println("Alexa skill detected")
|
||||
invokeService(w, r, metrics, alexaService.Request.Intent.Name, requestBody)
|
||||
|
||||
} else {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte("Provide an x-function header."))
|
||||
w.Write([]byte("Provide an x-function header or a valid Alexa SDK request."))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
12
sample-functions/ChangeColorIntent/Dockerfile
Normal file
12
sample-functions/ChangeColorIntent/Dockerfile
Normal file
@ -0,0 +1,12 @@
|
||||
FROM alpine:latest
|
||||
RUN apk --update add nodejs
|
||||
COPY ./fwatchdog /usr/bin/
|
||||
|
||||
COPY package.json .
|
||||
COPY handler.js .
|
||||
COPY sendColor.js .
|
||||
COPY sample.json .
|
||||
|
||||
RUN npm i
|
||||
ENV fprocess="node handler.js"
|
||||
CMD ["fwatchdog"]
|
40
sample-functions/ChangeColorIntent/handler.js
Normal file
40
sample-functions/ChangeColorIntent/handler.js
Normal file
@ -0,0 +1,40 @@
|
||||
"use strict"
|
||||
let fs = require('fs');
|
||||
let sample = require("./sample.json");
|
||||
let SendColor = require('./sendColor');
|
||||
let sendColor = new SendColor("alexellis.io/tree1")
|
||||
|
||||
var content = '';
|
||||
process.stdin.resume();
|
||||
process.stdin.on('data', function(buf) { content += buf.toString(); });
|
||||
process.stdin.on('end', function() {
|
||||
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 = "Christmas Lights";
|
||||
console.log(JSON.stringify(sample));
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
function handle(request,intent) {
|
||||
let colorRequested = intent.slots.LedColor.value;
|
||||
let req = {r:0,g:0,b:0};
|
||||
if(colorRequested == "red") {
|
||||
req.r = 255;
|
||||
} else if(colorRequested== "blue") {
|
||||
req.b = 255;
|
||||
} else if (colorRequested == "green") {
|
||||
req.g = 255;
|
||||
}
|
||||
else {
|
||||
tellWithCard("I heard "+colorRequested+ " but can only do: red, green, blue.", "I heard "+colorRequested+ " but can only do: red, green, blue.");
|
||||
}
|
||||
var speechOutput = "OK, "+colorRequested+".";
|
||||
sendColor.sendColor(req, () => {
|
||||
tellWithCard(speechOutput);
|
||||
});
|
||||
}
|
1
sample-functions/ChangeColorIntent/oneshot.txt
Normal file
1
sample-functions/ChangeColorIntent/oneshot.txt
Normal file
@ -0,0 +1 @@
|
||||
docker build -t changecolorintent . ; docker service rm ChangeColorIntent ; docker service create --network=functions --name ChangeColorIntent changecolorintent
|
15
sample-functions/ChangeColorIntent/package.json
Normal file
15
sample-functions/ChangeColorIntent/package.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "src",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"mqtt": "^2.0.1"
|
||||
}
|
||||
}
|
16
sample-functions/ChangeColorIntent/sample.json
Normal file
16
sample-functions/ChangeColorIntent/sample.json
Normal 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": {}
|
||||
}
|
40
sample-functions/ChangeColorIntent/sendColor.js
Normal file
40
sample-functions/ChangeColorIntent/sendColor.js
Normal file
@ -0,0 +1,40 @@
|
||||
"use strict"
|
||||
|
||||
var mqtt = require('mqtt');
|
||||
|
||||
class Send {
|
||||
constructor(topic) {
|
||||
this.topic = topic;
|
||||
}
|
||||
|
||||
sendIntensity(req, done) {
|
||||
var ops = { port: 1883, host: "iot.eclipse.org" };
|
||||
|
||||
var client = mqtt.connect(ops);
|
||||
|
||||
client.on('connect', () => {
|
||||
console.log("Connected");
|
||||
let payload = req;
|
||||
let cb = () => {
|
||||
done();
|
||||
};
|
||||
client.publish(this.topic, JSON.stringify(payload), {qos: 1}, cb);
|
||||
});
|
||||
}
|
||||
|
||||
sendColor(req, done) {
|
||||
var ops = { port: 1883, host: "iot.eclipse.org" };
|
||||
|
||||
var client = mqtt.connect(ops);
|
||||
let cb = () => {
|
||||
done();
|
||||
};
|
||||
client.on('connect', () => {
|
||||
console.log("Connected");
|
||||
let payload = req;
|
||||
client.publish(this.topic, JSON.stringify(payload), {qos: 1}, cb);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Send;
|
1
sample-functions/HostnameIntent/oneshot.txt
Normal file
1
sample-functions/HostnameIntent/oneshot.txt
Normal file
@ -0,0 +1 @@
|
||||
docker build -t hostnameintent . ; docker service rm HostnameIntent ; docker service create --network=functions --name HostnameIntent hostnameintent
|
@ -13,8 +13,8 @@ import (
|
||||
func main() {
|
||||
s := &http.Server{
|
||||
Addr: ":8080",
|
||||
ReadTimeout: 2 * time.Second,
|
||||
WriteTimeout: 2 * time.Second,
|
||||
ReadTimeout: 500 * time.Millisecond,
|
||||
WriteTimeout: 1 * time.Second,
|
||||
MaxHeaderBytes: 1 << 20,
|
||||
}
|
||||
|
||||
@ -39,4 +39,3 @@ func main() {
|
||||
|
||||
log.Fatal(s.ListenAndServe())
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user