英文:
Telegram message relay bot
问题
下午好。我正在使用 node-telegram-bot-api 和 sequelize 编写一个 Telegram 机器人。机器人面临的任务之一是自动将消息从一个频道转发或复制到另一个频道。在两个频道中,机器人都被分配为管理员。不幸的是,使用的解决方案都不起作用。我将频道设为私人和公开,还尝试将频道的 id 更改为 "@channelusername",但都没有奏效。我使用的示例:
可能的问题是什么?
示例 1:
const TelegramBot = require('node-telegram-bot-api');
const token = 'YOUR_TELEGRAM_BOT_TOKEN';
const bot = new TelegramBot(token, { polling: true });
const sourceChannelId = -1001234567890;
const targetChannelId = -1009876543210;
bot.on('message', (msg) => {
if (msg.chat.id === sourceChannelId) {
bot.forwardMessage(targetChannelId, sourceChannelId, msg.message_id);
}
});
示例 2:
const TelegramBot = require('node-telegram-bot-api');
const sourceChannelId = -1001234567890;
const targetChannelId = -1009876543210;
const bot = new TelegramBot('YOUR_TELEGRAM_BOT_TOKEN', { polling: true });
bot.on('message', (message) => {
if (message.chat.id === sourceChannelId) {
const notification = `New message in the channel ${message.chat.title}:\n\n${message.text}`;
bot.sendMessage(targetChannelId, notification);
}
});
英文:
Good afternoon. I am writing a telegram bot using node-telegram-bot-api and sequelize. One of the tasks that the bot faces is to automatically forward or copy messages from one channel to another. In both channels, the bot is assigned as an administrator. Unfortunately, none of the solutions used worked. I made the channels both private and public, I also tried changing the id to "@channelusername" of the channel, none of the options worked. Examples that I used:
What could be the problem?
Ex 1:
const TelegramBot = require('node-telegram-bot-api');
const token = 'YOUR_TELEGRAM_BOT_TOKEN';
const bot = new TelegramBot(token, { polling: true });
const sourceChannelId = -1001234567890;
const targetChannelId = -1009876543210;
bot.on('message', (msg) => {
if (msg.chat.id === sourceChannelId) {
bot.forwardMessage(targetChannelId, sourceChannelId, msg.message_id);
}
});
Ex 2:
const TelegramBot = require('node-telegram-bot-api');
const sourceChannelId = -1001234567890;
const targetChannelId = -1009876543210;
const bot = new TelegramBot('YOUR_TELEGRAM_BOT_TOKEN', { polling: true });
bot.on('message', (message) => {
if (message.chat.id === sourceChannelId) {
const notification = `Новое сообщение в канале ${message.chat.title}:\n\n${message.text}`;
bot.sendMessage(targetChannelId, notification);
}
});
答案1
得分: 0
感谢您联系。似乎您正在监听 message
更新。到达频道的消息不被视为消息,而是视为 channel_post
更新。
因此,仅监听频道帖子的相同代码将运行良好:
bot.on('channel_post', (msg) => {
if (msg.chat.id === sourceChannelId) {
bot.forwardMessage(targetChannelId, sourceChannelId, msg.message_id);
}
});
祝好
英文:
thanks for reaching out. It seems oblivious that you're listening to message
updates. The messages that arrive in channels are not considered messages, instead, they're treated as channel_post
updates.
So, simply the same code with listening to channel posts would work perfectly fine:
bot.on('channel_post', (msg) => {
if (msg.chat.id === sourceChannelId) {
bot.forwardMessage(targetChannelId, sourceChannelId, msg.message_id);
}
});
Regards
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论