首页 > 解决方案 > 如何保存来自用户的任何输入消息,这些消息通过谷歌表和对话流发送到我的在线聊天机器人

问题描述

我想将用户输入的每条消息保存到我的在线聊天机器人中。我的聊天机器人的目的是接收有关 covid19 的信息,因为我所在国家/地区的人们通常通过在线消息向其他人发送 covid 19 信息。他们可以将信息分享给我的聊天机器人,我会将信息保存在 google sheet 中,因为我希望我的聊天机器人易于使用。所以,我使用 google sheet 作为数据库并使用 dialog-flow 。但我不知道如何接收我之前所说的信息。

标签: google-apps-scriptgoogle-sheetsweb-applicationslinechatbot

解决方案


  1. 使用以下代码部署 Web 应用
  2. 在 LINE Developers / LINE Manager 中设置web app URLWebhook URL
function doPost(e) {
  const contents = e.postData.contents;
  const json = JSON.parse(contents);
  // console.log(json);
  const event = json["events"][0];
  if (!event["message"]) { return; }
  const userId = event.source.userId;
  const message = event.message.text;
  
  const id = 'Spreadsheet ID';
  const name = 'Sheet Name';
  const ss = SpreadsheetApp.openById(id);
  const sheet = ss.getSheetByName(name);
  sheet.appendRow([userId, message]);
}

推荐阅读