将电报bot与Google Apps脚本连接 [英] Connect telegram bot with google apps script

查看:260
本文介绍了将电报bot与Google Apps脚本连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在电报机器人上设置了一个机器人,并通过遵循教程.这是代码:

I have setup a bot on telegram bot and connected it with google spreadsheets via apps script by following this tutorial. Here is the code:

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]);
  }
}

现在我遇到了以下问题;我使用机器人来存储来自家庭自动化系统的消息.因此,我通过HTTP GET请求将这些消息从系统发送到电报机器人:

Now I encountered the following issue; I use my bot to store messages from my home automation system. Therefore I send those messages from the system to telegram bot via HTTP GET request:

https://api .telegram.org/bot [BOT_API_KEY]/sendMessage?chat_id = [MY_CHANNEL_NAME]& text = [MY_MESSAGE_TEXT]

当前,这些通过http get请求发送的消息似乎已被脚本忽略. Anyoene知道我该如何解决这个问题?

Currently these messages sent through http get request seem to be ignored by the script. Does anyoene know how I can solve this issue?

推荐答案

从您的问题和评论来看,您似乎很努力地将信息从脚本发送到Telegram上的bot.这是执行此操作的步骤:

Judging from your question and comments, it seems you are struggling with sending info from your script to your bot on Telegram. Here are the steps to do that:

1.- 创建机器人:在Telegram的搜索中查找@BotFather.单击开始,编写/newbot,并为其命名和用户名.您应该获得一个令牌来访问HTTP API.保存此令牌.

1.- Create a bot: on Telegram's search look for @BotFather. Click start, write /newbot, give it a name and a username. You should get a token to access the HTTP API. Save this token.

2.--在Telegram上通过其用户名找到您的机器人.给它写一些东西,例如'测试'.稍后会派上用场.

2.- Find your bot on Telegram with its username. Write something to it e.g. 'test'. This will come in handy later.

3.- 从您的代码对机器人的测试访问

var token = "123456:kioASDdjicOljd_ijsdf"; // Fill this in with your token
var telegramUrl = "https://api.telegram.org/bot" + token;

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

您应该得到类似以下内容的东西:

You should get something resembling this:

{"ok":true,"result":{"id":<somenumber>,"is_bot":true,"first_name":"<name of your bot>","username":"<username of your bot>","can_join_groups":true,"can_read_all_group_messages":false,"supports_inline_queries":false}}

4.-编写 sendMessage 函数

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

您要发送的已知文本,例如测试机器人",但chat_id未知.我们从哪里得到的?

Known is the text you want to send e.g. 'testing bot', but chat_id is unknown. Where do we get this?

5.- 找到chat_id .在运行此功能之前,请确保至少已在Telegram上向机器人写入了一条消息(第2步)

5.- Find the chat_id. Before running this function, make sure that you have at least written one message to your bot on Telegram (step 2)

 function getChat_id(){
   var res = UrlFetchApp.fetch(telegramUrl+"/getUpdates").getContentText();
   var res = JSON.parse(res);
   Logger.log(res.result[0].message.chat.id.toString());
}

6.- 使用在步骤5中找到的chat_id和要发送的消息运行sendMessage .

这篇关于将电报bot与Google Apps脚本连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆