Telegram机器人使用Google应用脚本无法工作。

huangapple go评论68阅读模式
英文:

Telegram bot using google apps script doesn't work

问题

这是我的代码:

var apiToken = "token";
var appUrl = "https://script.google.com/macros/s/link/exec";
var apiUrl = "https://api.telegram.org/bot"+apiToken;
var commands = {
  "/start": commandStart,
};

function setWebhook(){
  var url = apiUrl + "/setWebhook?url="+appUrl;
  var res = UrlFetchApp.fetch(url);
  Logger.log(res);
}

function deleteWebhook(){
  var res = UrlFetchApp.fetch(apiUrl+"/deleteWebhook");
  Logger.log(res);
}

function doPost(e){

  var webhookData = JSON.parse(e.postData.contents);
  var from = webhookData.message.from.id;
  var text = webhookData.message.text;
  if (text.charAt(0) == "/") {
    commands[text](from);
  } else {
    sendMessage("You said: "+text, from);
  }
}

function doGet(e){
  return ContentService.createTextOutput("Method GET not allowed");
}

function sendMessage(txt, receiver) {
  var encoded = encodeURIComponent(txt);
  var url  = apiUrl+"/sendMessage?parse_mode=HTML&chat_id="+receiver+"&text="+encoded;
  var response = UrlFetchApp.fetch(url);
  Logger.log(response);
}

function commandStart(receiver){
  sendText("Hello, skinbag", receiver);
}

我已经尽量只返回翻译好的部分代码,如果你需要进一步的帮助,请告诉我。

英文:

here's my code:

var apiToken = "token";
var appUrl = "https://script.google.com/macros/s/link/exec";
var apiUrl = "https://api.telegram.org/bot"+apiToken;
var commands = {
  "/start": commandStart,
};

function setWebhook(){
  var url = apiUrl + "/setWebhook?url="+appUrl;
  var res = UrlFetchApp.fetch(url);
  Logger.log(res);
}

function deleteWebhook(){
  var res = UrlFetchApp.fetch(apiUrl+"/deleteWebhook");
  Logger.log(res);
}

function doPost(e){

  var webhookData = JSON.parse(e.postData.contents);
  var from = webhookData.message.from.id;
  var text = webhookData.message.text;
  if (text.charAt(0) == "/") {
    commands[text](from);
  } else {
    sendMessage("You said: "+text, from);
  }
}

function doGet(e){
  return ContentService.createTextOutput("Method GET not allowed");
}

function sendMessage(txt, receiver) {
  var encoded = encodeURIComponent(txt);
  var url  = apiUrl+"/sendMessage?parse_mode=HTML&chat_id="+receiver+"&text="+encoded;
  var response = UrlFetchApp.fetch(url);
  Logger.log(response);
}

function commandStart(receiver){
  sendText("Hello, skinbag", receiver);
}

I've been trying to make it work for 5 hours now. Everything seems right, but it just doesn't work.
By doesn't work I mean that it doesn't send any messages

But if I change sendMessage method by hard-coding my own id and calling method directly by "execute" button it actually sends me a message

function sendMessage() {
  var encoded = encodeURIComponent("SomeMessage");
  var url  = apiUrl+"/sendMessage?parse_mode=HTML&chat_id="+MyId+"&text="+encoded;
  var response = UrlFetchApp.fetch(url);
  Logger.log(response);
}

I think something might be off with doPost() method.
I tried re-establishing webhook by calling "deteleWebhook" and then "setWebhook", I tried simplifying the code to just "doPost" and "sendMessage" with my id (still didn't work), I tried reading docs (didn't find anything), I tried googling a lot.
I just don't know why it won't work

答案1

得分: 0

不要忘记在部署时使其对所有人可用。那就是问题所在。我只使其对我可用。

是的,我很愚蠢。但也许它能帮助将来的某人。

英文:

Don't forget to make it avaiable to everyone when deploying. That was the problem. I made it avaiable to me only

Yes, I am stupid. But maybe it can help someone in the future

huangapple
  • 本文由 发表于 2023年5月26日 10:18:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定