Car*_*oni 4 javascript geocoding geolocation google-geocoding-api dialogflow-es
我正在尝试在对话流的填充中使用来自 Google 的地理编码 API。
详细说明:我希望用户给我他的地址并找到经纬度。
我从官方 github 存储库的 README 中获取了这段代码:
googleMapsClient.geocode({address: '1600 Amphitheatre Parkway, Mountain View, CA'})
.asPromise()
.then((response) => {
console.log(response.json.results);
})
.catch((err) => {
console.log(err);
});
Run Code Online (Sandbox Code Playgroud)
并以这种方式在我的意图处理程序中使用它:
function findAddress(agent){
var intent = new Intent(agent);
parametersCustom.address = agent.parameters.address+', '+agent.parameters["geo-city"];
agent = intent.finalSetup();
return googleMapsClient.geocode({address: parametersCustom.address}).asPromise()
.then((response) => {
console.log(response.json.results[0].geometry.location);
})
.catch((err) => {
console.log(err);
});
}
Run Code Online (Sandbox Code Playgroud)
但是当我在 DialogFlow 的内联编辑器上运行它时,我收到了这个错误:
Error: No responses defined for platform: null
at V2Agent.sendResponses_ (/srv/node_modules/dialogflow-fulfillment/src/v2-agent.js:243:13)
at WebhookClient.send_ (/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:505:17)
at promise.then (/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:316:38)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
Run Code Online (Sandbox Code Playgroud)
我已经以正确的方式进行了设置(根据官方文档):
const googleMapsClient = require('@google/maps').createClient({
key: 'your API key here',
Promise: Promise
});
Run Code Online (Sandbox Code Playgroud)
我在这里缺少什么?
经过很多麻烦后,我让它工作了:
当承诺被解决时,意图处理程序需要你返回一个代理。
function findAddress(agent){
var intent = new Intent(agent);
parametersCustom.address = agent.parameters.address+', '+agent.parameters["geo-city"];
agent = intent.finalSetup();
return googleMapsClient.geocode({address: parametersCustom.address}).asPromise()
.then((response) => {
console.log(response.json.results[0].geometry.location);
parametersCustom.location = response.json.results[0].geometry.location;
parametersCustom.formatted_address = response.json.results[0].formatted_address;
return agent.add('Formatted address' +parametersCustom.formatted_address+' Lat and long ='+parametersCustom.location);
})
.catch((err) => {
console.log(err);
});
}
Run Code Online (Sandbox Code Playgroud)
这样,代理将在收到来自地理编码 API 的响应后立即回复用户。
| 归档时间: |
|
| 查看次数: |
234 次 |
| 最近记录: |