如何与谷歌表沟通电报机器人

时间:2017-10-15 07:08:20

标签: google-apps-script

我有与电报机器人和谷歌电子表格沟通的问题,昨天我使用它,并且工作非常好,但今天它无法正常工作。 我创建另一个谷歌帐户和另一个博特,但不工作。 这是我的谷歌脚本:

var token="123197063:AAH04kulz7tRqPz3vbDcgYdVje18WH2Pv-4";
var telegramUrl= "https://api.telegram.org/bot"+token;
var webAppUrl = "https://script.google.com/macros/s/AKfycbwqvJWsWcm_5_Y1vhYEkSN2G9dxiDBzQIvYvbte-3_HfGcGFN3a/exec";

function getMe(){
  var url = telegramUrl+"/getMe";
  var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());

}

function setWebhook() {
  var url = telegramUrl+"/setWebhook?url="+webAppUrl;
  var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());

}

function doGet(e){
  return HtmlService.createHtmlOutput("hi this is my first project");

}

function dePost(e){
  GmailApp.sendEmail(Session.getEffectiveUser().getEmail(), "message from bot", JSON.stringify(e, null, 4));

}

在这段代码中,当我在电报机器人中写任何东西时,谷歌表必须发送电子邮件给我,但它今天无法正常工作。 这是我的机器人地址:@irmec_bot

你知道它不起作用吗? 请帮帮我 感谢

2 个答案:

答案 0 :(得分:2)

您可以按照此视频教程中的说明操作:How to connect your Telegram Bot to a Google Spreadsheet (Apps Script) Script in Description

// 
// FILL IN THE GLOBAL VARIABLES token, webAppUrl and ssId
//

var token = ""; // FILL IN YOUR OWN TOKEN
var telegramUrl = "https://api.telegram.org/bot" + token;
var webAppUrl = ""; // FILL IN YOUR GOOGLE WEB APP ADDRESS
var ssId = ""; // FILL IN THE ID OF YOUR SPREADSHEET

function getMe() {
  var url = telegramUrl + "/getMe";
  var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());
}

function setWebhook() {
  var url = telegramUrl + "/setWebhook?url=" + webAppUrl;
  var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());
}

function sendText(id,text) {
  var url = telegramUrl + "/sendMessage?chat_id=" + id + "&text=" + text;
  var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());
}

function doGet(e) {
  return HtmlService.createHtmlOutput("Hi there");
}

function doPost(e) {
  // this is where telegram works
  var data = JSON.parse(e.postData.contents);
  var text = data.message.text;
  var id = data.message.chat.id;
  var name = data.message.chat.first_name + " " + data.message.chat.last_name;
  var answer = "Hi " + name + ", thank you for your comment " + text;
  sendText(id,answer);
  SpreadsheetApp.openById(ssId).getSheets()[0].appendRow([new Date(),id,name,text,answer]);

  if(/^@/.test(text)) {
    var sheetName = text.slice(1).split(" ")[0];
    var sheet = SpreadsheetApp.openById(ssId).getSheetByName(sheetName) ? SpreadsheetApp.openById(ssId).getSheetByName(sheetName) : SpreadsheetApp.openById(ssId).insertSheet(sheetName);
    var comment = text.split(" ").slice(1).join(" ");
    sheet.appendRow([new Date(),id,name,comment,answer]);
  }
}

这里还有一个额外的参考资料,可能也有帮助:Telegram Bot with Apps Script

答案 1 :(得分:2)

如果这是同一漫游器的新脚本,则应将网络挂钩设置为新链接。

1)作为应用发布后,复制给定的url(类似> https://script.google.com/macros/s/...

2)替换第3行var webAppUrl = INSERT_URL_HERE

上的网址

3)保存脚本

4)通过单击运行>运行功能> setWebhook(),运行setWebhook()函数

希望这会有所帮助!