如何在没有第三方解决方案的情况下将 WhatsApp 数据直接发送到 Google Sheets
我想从 WhatsApp 消息中将分离的数据输入到 Google 电子表格,而无需任何第三方解决方案,仅使用脚本和 Webhooks 请更新我如何使用 WhatsApp Api 执行此操作。即使我不知道 Whatsapp 是否提供任何 API。
作为一名 Javascript 开发人员,我会使用 whatsapp-web.js 和 google-spreadsheet。
类似这样的事情:
const { GoogleSpreadsheet } require('google-spreadsheet');
const { JWT } require('google-auth-library');
// Initialize auth - see https://theoephraim.github.io/node-google-spreadsheet/#/guides/authentication
const serviceAccountAuth = new JWT({
// env var values here are copied from service account credentials generated by google
// see "Authentication" section in docs for more info
email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
key: process.env.GOOGLE_PRIVATE_KEY,
scopes: [
'https://www.googleapis.com/auth/spreadsheets',
],
});
const doc = new GoogleSpreadsheet('<the sheet ID from the url>', serviceAccountAuth);
const async saveMessage = (text) => {
await doc.loadInfo(); // loads document properties and worksheets
const sheet = doc.sheetsByIndex[0]; // or use `doc.sheetsById[id]` or `doc.sheetsByTitle[title]`
const rows = await sheet.getRows(); // can pass in { limit, offset }
rows[rows.length - 1].set('message') = test;
await rows[rows.length - 1].save(); // save updates on a row
}
const { Client } = require('whatsapp-web.js');
const client = new Client();
client.on('qr', (qr) => {
// Generate and scan this code with your phone
console.log('QR RECEIVED', qr);
});
client.on('ready', () => {
console.log('Client is ready!');
});
client.on('message', async msg => {
saveMessage(msg.body);
});
client.initialize();