【问题标题】:Can't get conv.data to save a parameter in Actions on Google无法获取 conv.data 以在 Actions on Google 中保存参数
【发布时间】:2019-01-16 08:38:39
【问题描述】:

如您所见:Console Log from SaveLocation - AskLocationPermission 它不会将 location_type 保存到 conv.data。虽然用户存储效果很好,但我想正确使用conv.dataconv.user.storage。您可以在Example of conversation 中看到参数已完成,但它没有保存到conv.data

当用户说他想将此位置保存为他的家或工作时,它应该如何工作,如果他有家庭或工作,它应该查看conv.user.storage。如果不请求他的位置许可并添加它,否则它应该询问他是否确定要覆盖它。

app.intent('SaveLocation - AskLocationPermission', (conv, {location_type}) => {
    if (storage.isLocationAlreadySaved(conv, location_type)) {
        return fun.talkAsk(conv, i18n.__("LOCATIONS.SAVE_OVERRIDE", location_type))
    }
    storage.setSaveLocationType(conv, location_type);
    console.log("SaveLocation - AskLocationPermission");
    console.log(storage.getLocationType(conv));
    return conv.ask(new Permission({
        context: i18n.__("PERMISSION_LOCATION.SAVE_LOCATION", location_type),
        permissions:
            ['DEVICE_PRECISE_LOCATION'],
    }));
});

app.intent('SaveLocation - PermissionHandler', (conv, params, permissionGranted) => {
    if (!permissionGranted) return fun.talkAsk(conv, i18n.__("ERROR.PERMISSION_NOT_GRANTED", storage.getLocationType(conv)))
    storage.setUserLocation(conv);
    storage.saveLocation(conv);
    return fun.talkAsk(conv, i18n.__("LOCATIONS.SAVE_COMPLETE", storage.getLocationType(conv)))
});

app.intent('SaveLocation - Yes', (conv) => {
    console.log("SaveLocation - Yes");
    console.log(storage.getLocationType(conv));
    return conv.ask(new Permission({
        context: i18n.__("PERMISSION_LOCATION.SAVE_LOCATION", storage.getLocationType(conv)),
        permissions:
            ['DEVICE_PRECISE_LOCATION'],
    }));
});

app.intent('SaveLocation - No', (conv) => {
    return fun.talkAsk(conv, i18n.__("LOCATIONS.SAVE_DENIED", storage.getLocationType(conv)))
});

存储文件:

let setSaveLocationType = function (conv, locationType) {
    conv.data.locationType = locationType;
};

let getLocationType = function (conv) {
    return conv.data.locationType;
};

let isLocationAlreadySaved = function (conv, locationType) {
    console.log("isLocationAlreadySaved");
    if (locationType === "work") {
        console.log(getWorkLocationLat(conv))
        return getWorkLocationLat(conv) !== 0.0
    } else if (locationType === "home") {
        console.log(getHomeLocationLat(conv));
        return getHomeLocationLat(conv) !== 0.0
    }
    return false
};

let saveLocation = function (conv) {
    let locationType = getLocationType(conv);
    if (locationType === "work") {
        conv.user.storage.workLocationLatitude = getUserLatitude(conv);
        conv.user.storage.workLocationLongitude = getUserLongitude(conv);
    } else if (locationType === "home") {
        conv.user.storage.homeLocationLatitude = getUserLatitude(conv);
        conv.user.storage.homeLocationLongitude = getUserLongitude(conv);
    }
};


let getWorkLocationLat = function (conv) {
    return conv.user.storage.workLocationLatitude
};
let getWorkLocationLng = function (conv) {
    return conv.user.storage.workLocationLongitude
};
let getHomeLocationLat = function (conv) {
    return conv.user.storage.homeLocationLatitude
};
let getHomeLocationLng = function (conv) {
    return conv.user.storage.homeLocationLongitude
};

How I imagined flow of the conversation for this intent

【问题讨论】:

  • 你解决了吗?我也有同样的问题...

标签: dialogflow-es actions-on-google dialogflow-es-fulfillment


【解决方案1】:

看起来您实际上没有定义“setUserLocation”函数,但您尝试从“SaveLocation - PermissionHandler”意图处理程序中调用它。

【讨论】:

  • setUserLocation 与它没有任何关系:)。之所以有效,是因为它保存在 conv.user.storage 中,但它应该保存在 conv.data 中,因为位置会随着时间而变化。问题是关于 conv.data 和函数 storage.setSaveLocationType(conv, location_type);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多