【问题标题】:how to use Telegram official api inside telegraf library如何在 Telegraf 库中使用 Telegram 官方 API
【发布时间】:2020-12-23 16:55:29
【问题描述】:

我正在尝试使用 telegraf library 和 npm 创建一个电报机器人

问题是我找不到在 telegraf api 中使用所有电报方法的方法,例如 channels.getFullChannelchannels.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')

也没有用

【问题讨论】:

  • 您有任何代码或任何错误可以添加吗?
  • 我编辑了问题!

标签: telegram telegram-bot telegraf telegraf.js


【解决方案1】:

你好,你应该修改你的类构造函数并将 channelMode 设置为 True

const bot =  new Telegraf(Token, {

channelMode: true

})

那么你应该使用事件来获取类似的频道消息

bot.on('channel_post', (ctx) => {
 

console.log(ctx.channelPost);

})

它将记录所有频道中的每个频道帖子。您可以使用 Simple if 条件来记录您想要的此频道

【讨论】:

  • 这会在发送新消息时返回它们......我需要的是访问频道历史记录中的所有消息
猜你喜欢
  • 2021-06-13
  • 2019-04-27
  • 2023-03-28
  • 2019-06-24
  • 2021-11-28
  • 1970-01-01
  • 2022-01-11
  • 2020-09-15
  • 1970-01-01
相关资源
最近更新 更多