【问题标题】:How to forward message from channel to the groups on telegram bot?如何将消息从频道转发到电报机器人上的群组?
【发布时间】:2019-08-28 18:47:32
【问题描述】:

我正在尝试为我的机器人实现一个功能。功能,如果频道写任何消息,它将被转发到机器人已经存在的组。

当新用户加入群组时,尝试使用作用域方法,该方法在欢迎消息中起到了魅力。

//index.js

const Telegram = require('telegram-node-bot'),

tg = new Telegram.Telegram('MYAPI', {
workers: 1
});

const ForwardController = require('./controllers/forward')

tg.router.when(new Telegram.TextCommand('/info', 'infoCommand'), new InfoController())
.otherwise(new ForwardController());

//forward.js

const Telegram = require('telegram-node-bot');

class ForwardController extends Telegram.TelegramBaseController {
handle(scope) {
    if ('channel' == scope.message.chat._type) {
        scope.api.forwardMessage(scope.message._chat._id, _forwardFromChat._text);
      }
   }
}

module.exports = ForwardController;

我尝试了许多组合,但消息从未转发...该机器人已经是频道的管理员,并且也被放入了组中。 (还用机器人打开了私人消息,所以我认为它也应该在那里转发)

【问题讨论】:

    标签: node.js telegram telegram-bot


    【解决方案1】:

    查看API reference for the library,文档页面似乎已关闭,因此 Github 是您的朋友。

    您正在进行的forwardMessage 调用具有不正确的参数并且正在访问私有类变量。它还返回一个承诺,因此您应该等待该承诺或将.then 链接到它。您可以在 Scope 实例本身上使用类方法。

    应该更像:

    // using async/await - note the containing function must be async for this approach
    const result = await forwardMessage(<id of chat here>, scope.message().id());
    
    // or to chain a .then
    forwardMessage(<id of chat here>, scope.message().id())
      .then(result => /* do something with result */)
      .catch(err => /* handle the error */);
    

    这将使用Scopes 实例方法并为您处理发送当前聊天的ID,您只需要发送消息的聊天ID,然后将&lt;id of chat here&gt; 替换为该ID .

    【讨论】:

    • 不幸的是不能使用 await 它抛出错误:scope.forwardMessage(idofgroup, scope._message._messageId); 我觉得有些东西不见了..
    • link 花了很长时间后,我认为图书馆里面有些东西搞砸了,需要修复,所以我也在 github 上打开了问题.. [error] No processor found for update: Update { 这个错误是我认为的问题..
    猜你喜欢
    • 1970-01-01
    • 2020-08-25
    • 2023-02-02
    • 2017-04-27
    • 2016-05-03
    • 2020-08-25
    • 2018-04-01
    • 2022-01-27
    • 1970-01-01
    相关资源
    最近更新 更多