我有一个正在工作的 Google API.ai 项目,我与之交谈,要求它打开一个组件,它将 json 发布到 webhook URL,我的 nodejs 解析 json 以在树莓上执行 python 脚本。问题是,显然只是调用欢迎意图会触发 json 帖子并切换 python 脚本以打开组件。奇怪的是,if 语句确定要调用哪个 python 脚本所需的组件状态在欢迎意图状态下是空的。
这是我的 node.js:
'use strict';
require('dotenv').config();
const PythonShell = require('python-shell');
const fs = require('fs');
const express = require('express');
const bodyParser= require('body-parser');
const path = require('path')
const app = express();
process.env.DEBUG = 'actions-on-google:*';
let Assistant = require('actions-on-google').ApiAiAssistant;
app.use(bodyParser.json({type: 'application/json'}));
const GENERATE_ANSWER_ACTION = 'generate_answer';
const EXECUTE_HOME_COMMAND = 'execute_home_command';
const switches = [];
var readableStream = fs.createReadStream('saveState.json');
var data = ''
readableStream.on('data', function(chunk) {
data+=chunk;
});
readableStream.on('end', function() {
var parsed = JSON.parse(data);
for (var i=0;i<parsed.switches.length;i++){
switches.push(new Switch(parsed.switches[i]))
}
});
function Switch(switchValues){
this.id = switchValues.id || "sw"
this.state = switchValues.state || "off"
this.name = switchValues.name || "switch"
this.toggle = function(){
if(this.state === "on"){
this.setState("off")
}
else{
this.setState("on");
}
}
this.setState = function(state){
var str = state === "on" ? onString(this.id[2]) : offString(this.id[2]);
PythonShell.run(str, function (err) {
if (!process.env.DEV){
if (err) throw err;
}
});
this.state = state
}
this.setState(this.state);
}
function onString(number){
return './public/python/sw' + number + '_on.py'
}
function offString(number){
return './public/python/sw' + number + '_off.py'
}
function getSwitch(string){
return switches.filter(function(element){
return element.id === string;
})[0]
}
function saveState (){
var formattedState = {
switches: switches
}
fs.writeFile('./saveState.json', JSON.stringify(formattedState) )
}
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
res.sendFile('index');
})
// Switch Routes for API
app.get('/api/switches', function(req, res){
res.send(switches);
})
app.get('/api/switches/:id', function(req, res){
var found = getSwitch(req.params.id);
res.json(found);
})
app.post('/api/switches/:id', function(req, res){
console.log('headers: ' + JSON.stringify(req.headers));
console.log('body: ' + JSON.stringify(req.body));
const assistant = new Assistant({request: req, response: res});
let soc = assistant.getArgument('state-of-component')
function generateAnswer(assistant) {
console.log('genera answer');
assistant.ask('I\'m thinking of a number from 0 and 100. What\'s your first guess?');
}
function executeHomeCommand(assistant) {
console.log('revisear guess');
console.log(soc);
if (soc === "on") {
console.log('SUCCESS soc=ON');
} else {
console.log('FAILUER soc=OFF');
}
}
// //MAP ACTIONS to functions
let actionMap = new Map();
actionMap.set(GENERATE_ANSWER_ACTION, generateAnswer);
actionMap.set(EXECUTE_HOME_COMMAND, executeHomeCommand);
assistant.handleRequest(actionMap);
if (req.query.password === process.env.PASS){
var foundSwitch = getSwitch(req.params.id);
if (soc === "on") {
foundSwitch.setState("on");
console.log('SWITCHING ON');
} else {
foundSwitch.setState("off");
console.log('SWITCHING OFF');
}
saveState();
console.log("postSwitch "+JSON.stringify(foundSwitch));
res.json(foundSwitch);
}
else {
console.log("invalid password")
res.send("try again")
}
})
app.listen(process.env.PORT, function(){
console.log('Listening on port ' + process.env.PORT);
})
我以为是我的 app.js 代码在欢迎意图上发布帖子,但我不太确定,因为在欢迎意图上,组件状态为空,没有价值,所以 soc 是!=“on”,所以if 语句工作正常。问题在于:
if (soc === "on") {
foundSwitch.setState("on");
console.log('SWITCHING ON');
代码也在欢迎意图上被调用,它不应该。