Telegram消息中继机器人

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

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

祝好 Telegram消息中继机器人

英文:

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 Telegram消息中继机器人

huangapple
  • 本文由 发表于 2023年3月31日 15:51:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896097.html
匿名

发表评论

匿名网友

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

确定