hon*_*ill 2 google-sheets google-apps-script telegram-bot
我已经在电报机器人上设置了一个机器人,并按照本教程通过应用程序脚本将其与谷歌电子表格连接。这是代码:
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]);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我遇到了以下问题;我使用我的机器人来存储来自家庭自动化系统的消息。因此,我通过 HTTP GET 请求将这些消息从系统发送到电报机器人:
目前,这些通过 http get 请求发送的消息似乎被脚本忽略了。有谁知道我该如何解决这个问题?
从您的问题和评论来看,您似乎正在努力将信息从脚本发送到 Telegram 上的机器人。以下是执行此操作的步骤:
1.-创建一个机器人:在 Telegram 搜索中查找@BotFather。单击开始,写入 /newbot,为其指定名称和用户名。您应该获得一个令牌来访问 HTTP API。保存此令牌。
2.-在 Telegram 上找到您的机器人及其用户名。向其中写入一些内容,例如“测试”。这稍后会派上用场。
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());
}
Run Code Online (Sandbox Code Playgroud)
你应该得到类似这样的东西:
{"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}}
Run Code Online (Sandbox Code Playgroud)
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());
}
Run Code Online (Sandbox Code Playgroud)
您想要发送的文本(例如“测试机器人”)已知,但 chat_id 未知。我们从哪里得到这个?
5.-找到 chat_id。在运行此功能之前,请确保您至少已在 Telegram 上向您的机器人写入一条消息(第 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());
}
Run Code Online (Sandbox Code Playgroud)
6.-使用您在步骤 5 中找到的 chat_id 和您要发送的消息运行 sendMessage 。