我必须定期(每 15 分钟)向 BOT 发送修复消息。据我所知,我应该创建一个 BOT 来做到这一点。据我所知,BOT 无法在 Telegram 中向另一个 BOT 发送任何消息。因此,我决定将修复消息从聊天转发到与目标 BOT 的聊天。
我发现我应该做一个新的BOT并写一段代码。另外,我找到了所需的代码片段,如下所示,但它需要源聊天 ID、目标聊天 ID 和 message_id,我不知道如何找到它们。我使用了用户名@get_id_bot 的 BOT,但它给我的是用户 ID,而不是聊天 ID。请帮我完成任务。 预先感谢您的任何帮助和建议。
var telegramToken = 'YOUR_TELEGRAM_BOT_TOKEN';
var sourceChatId = 'SOURCE_CHAT_ID'; // The chat ID from where messages will be forwarded
var destinationChatId = 'DESTINATION_CHAT_ID'; // The chat ID of the bot to which messages will be forwarded
function forwardMessageToBot(messageId) {
var url = 'https://api.telegram.org/bot' + telegramToken + '/forwardMessage';
var payload = {
'chat_id': destinationChatId,
'from_chat_id': sourceChatId,
'message_id': messageId
};
// Use UrlFetchApp to forward the message to the Telegram Bot
var response = UrlFetchApp.fetch(url, {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(payload)
});
}
function createTrigger() {
// Delete existing triggers to avoid duplicates
var existingTriggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < existingTriggers.length; i++) {
if (existingTriggers[i].getHandlerFunction() === 'forwardMessageToBot') {
ScriptApp.deleteTrigger(existingTriggers[i]);
}
}
// Create a new time-driven trigger that runs at the desired interval
ScriptApp.newTrigger('forwardMessageToBot')
.timeBased()
.everyMinutes(15) // Set the frequency here (15 minutes in this case)
.create();
}
// This function should be called with the specific message ID you want to forward
function setupForwardingForMessage(messageId) {
forwardMessageToBot(messageId);
createTrigger();
}
我不太清楚你的问题。我想您正在寻找查找源 chat_id 和目标 chat_id 的方法。
您可以使用您提到的相同机器人 (@get_id_bot) 来获取聊天 ID。
我可以看到,当您将 @get_id_bot 添加到群组或频道时,它会自动将聊天 ID 发送到添加的群组/频道。但是,为了将机器人添加到频道/组,您可能需要管理员权限。获得聊天 ID 后,您可以在代码中使用它并完成任务。
此外,要获取消息 ID,您只需点击要转发的消息并复制群组和频道上的消息链接即可。该链接看起来像这样:
https://t.me/c/<some_Id>/141
。最后一部分 141
是特定消息的消息 ID。
希望这个答案。
如果您还有其他问题,请告诉我。