如何解决如何在Telegraf库中使用Telegram官方api
我正在尝试使用telegraf library和npm创建电报机器人
问题是我找不到在Telegraf API中使用所有channels.getFullChannel
或channels.getMessages
之类的电报方法的方法...
电报官方文档中有一些我想使用的方法,例如this one,但我不知道如何使用电报实例访问它。
我将此用于我的代码:
const Telegraf = require('Telegraf');
const bot = new Telegraf(process.env.TOKEN);
bot.command('logmessages',async (ctx) => {
const res = ctx.telegram.channels.getMessages('@username')
console.log(res)
ctx.reply('check out console')
})
但是控制台中显示的是:
TypeError: Cannot read property 'getMessages' of undefined
at /app/node_modules/telegraf/composer.js:143:56
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async Promise.all (index 0)
我应该提到 ,在官方文档中指出这些方法可由Bots访问。
我也测试过
ctx.channels.getMessages('@username')
也不起作用
解决方法
你好,你应该修改你的类构造函数并将 channelMode 设置为 True
const bot = new Telegraf(Token,{
channelMode: true
})
那么你应该使用事件来获取类似这样的频道消息
bot.on('channel_post',(ctx) => {
console.log(ctx.channelPost);
})
它将记录所有频道中的每个频道帖子。您可以使用 Simple if 条件来记录您想要的此频道
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。