首页 > 解决方案 > 使用任何服务器数据库进行谷歌助理操作

问题描述

我正在使用谷歌助手来创建自己的操作,我是新手。我想使用自己的数据库并将用户响应保存到数据库。有人可以帮我吗?

标签: databasegoogle-assistant-sdk

解决方案


早上好,高塔姆!您的确切履行代码将根据您选择的数据库和云环境而有所不同。就我而言,我为 Google Assistant 构建了一个操作,该操作在 GCP Cloud Firestore NoSQL 数据库中创建了一个条目。

此示例代码在我的数据库中创建一个条目:

app.intent('write to the wall', (conv) => {
    console.log(conv);
    console.log(conv.input.raw);
    var userReply = conv.input.raw;

    function writeToDb(userReply){

        const dialogflowAgentRef = db.collection('dialogflow').doc('agent');

        return db.runTransaction(t => {
          t.set(dialogflowAgentRef, {entry: userReply});
          conv.ask('The wall now reads, ' + userReply + '\n\n');
          conv.close("Thanks for visiting!");
          return Promise.resolve('Write complete');
        }).then(doc => {
          console.log(`Wrote "${userReply}" to the Firestore database.`);
        }).catch(err => {
          console.log(`Error writing to Firestore: ${err}`);
        //   console.log(`Failed to write "${userReply}" to the Firestore database.`);
        });
    }

    return writeToDb(userReply);

});

您的数据库create功能可能看起来略有不同,但希望能帮助您入门……前提是您选择使用Actions on Google 代码实验室中介绍的技术堆栈:Node.js、Dialogflow 和 Firebase


推荐阅读