如何将电报数据保存在 google 电子表格中?

How to save telegram data in a google spreedsheet?

我使用 telegraf 构建了一个简单的电报机器人,并使用此代码记录了我需要的特定信息:

bot.on('text', (ctx, next) => {
  console.log(`[text] ${ ctx.message.chat.id } ${ ctx.from.username } ${ ctx.message.chat.first_name+ " " + ctx.message.chat.last_name } ${ ctx.message.text }`);
   return next();
});

结果,日志是这样的

[text] 563789142 xMA3x Mohammed Abbas /start

现在我想将该信息保存在 google spreadsheet 中,我遵循了 this 教程并且能够将引号标记的值推送到 spreadsheet 而已,但我不知道如何将 console.log 结果推入 spreddsheet

无论如何,这是我的代码

const { Telegraf } = require('telegraf');
const bot = new Telegraf("xyz")
const { google } = require("googleapis");
const keys = require("./Keys.json")

bot.on('text', (ctx, next) => {
  console.log(`[text] ${ ctx.message.chat.id } ${ ctx.from.username } ${ ctx.message.chat.first_name+ " " + ctx.message.chat.last_name } ${ ctx.message.text }`);
   return next();
});

bot.start((ctx) => ctx.reply('Welcome'))
bot.help((ctx) => ctx.reply('Send me a sticker'))
bot.on('sticker', (ctx) => ctx.reply(''))
bot.hears('hi', (ctx) =>  ctx.reply('Hey there'))


const client = new google.auth.JWT(
  keys.client_email, 
  null, 
  keys.private_key, 
  ["https://www.googleapis.com/auth/spreadsheets"]
);


client.authorize(function(err){

    if(err){
      console.log(err);
      return;
    } else {
      console.log("connected");
      gsrun(client);
    }

});

async function gsrun(cl){

  const gsapi = google.sheets({version:"v4", auth: cl});
    
  const updateOptions = {
    spreadsheetId: "xyz",
    range: "Sheet1",
    valueInputOption: "RAW",
    insertDataOption: "INSERT_ROWS",
    resource: {
      values:[
        ["this is working"]
      ]}
  };
 let res = await gsapi.spreadsheets.values.append(updateOptions);
 console.log(res);
}
bot.launch()

因此,正如您看到的那样,“这是有效的”已成功推送到传播中sheet,但是当我尝试添加另一个值时,例如 ctx.message.chat.id 它给我 ReferenceError: ctx is not defined

那么如何让 google sheet API 识别 telegraf 命令?或者更笼统地说,我如何将 *ctx.message.chat.id、ctx.from.username..etc * 信息(来自电报)保存到 spreedhsset 中?

ctx 存在于您的机器人挂钩中,因此要将信息保存到 sheet,您必须在相关挂钩中调用您的 googlesheets 函数。

可能的更新:

const { Telegraf } = require('telegraf');
const bot = new Telegraf("xyz")
const { google } = require("googleapis");
const keys = require("./Keys.json")

const client = new google.auth.JWT(
  keys.client_email, 
  null, 
  keys.private_key, 
  ["https://www.googleapis.com/auth/spreadsheets"]
);

async function gsrun(cl, data){

    const gsapi = google.sheets({version:"v4", auth: cl});
      
    const updateOptions = {
      spreadsheetId: "xyz",
      range: "Sheet1",
      valueInputOption: "RAW",
      insertDataOption: "INSERT_ROWS",
      resource: {
        values:[
          [data]
        ]}
    };
   let res = await gsapi.spreadsheets.values.append(updateOptions);
   console.log(res);
  }

const saveMetadataToSheets = (data) => {
    client.authorize(function(err){
        if(err){
          console.log(err);
          return;
        } else {
          console.log("connected");
          gsrun(client, data);
        }
    });
}


bot.on('text', (ctx, next) => {
    const data = `[text] ${ ctx.message.chat.id } ${ ctx.from.username } ${ ctx.message.chat.first_name+ " " + ctx.message.chat.last_name } ${ ctx.message.text }`
    console.log(data);
    // pass any data that you need to save to the sheets
    saveMetadataToSheets(data)
     return next();
  });
  
bot.start((ctx) => ctx.reply('Welcome'))
bot.help((ctx) => ctx.reply('Send me a sticker'))
bot.on('sticker', (ctx) => ctx.reply(''))
bot.hears('hi', (ctx) =>  ctx.reply('Hey there'))
bot.launch()