简体   繁体   中英

How to link Google Sheets to Google Dialogflow to retrieve data from sheets?

I'm building a chatbot with Dialogflow for which I need to link Google Sheets which is going to act as a backend to retrieve data from sheets and display in chatbot when an intent is triggered. How do I do that any tutorial or documentation which can help?

If you are using NodeJS as your webhook,

You can use Google spreadsheet to store and get data,

const GoogleSpreadsheet = require('google-spreadsheet');
const sheets = new GoogleSpreadsheet(process.env.sheetid);
// sheetid can be found from sheet URL
const config = {
    client_email: process.env.client_email,
    private_key: process.env.private_key
}
// config detail you need to get from your service account
// share your client_email in your sheet with read and write permission
sheets.useServiceAccountAuth(config, () => {
    sheets.getInfo(async (err, info) => {
        sheet = info.worksheets
        // same way you will be getting your sheet data in sheet variable
        await addRows(sheet, 0, rowData)
        // rowData => your data
    })
});

function addRows(sheet, index, data) { // index is your sheet tab index
    return new Promise((resolve, reject) => {
        sheet[index].addRow(data, (err, row) => {
            if (err) return reject(err)
            resolve(row)
        })
    })
}

I do this with Vodo Drive , although only for Actions at the moment.

Make sure you understand how to use Google's OAuth to get authorization from the user to access their sheets using the Sheets API. This is the primary thing you need to understand before you can do this with Dialogflow.

The biggest problem you'll encounter using Dialogflow for a general bot integration is being able to know what Google Account they're using (and that you have the OAuth tokens for). If you're building for Actions, you have the ability to use Account Linking. Other integrations may pass along user information as well, but this isn't guaranteed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM