簡體   English   中英

如何在 Telegraf 庫中使用 Telegram 官方 api

[英]how to use Telegram official api inside telegraf library

我正在嘗試使用電報庫和 npm 創建電報機器人

問題是我找不到一種方法來使用所有電報方法,如在電報 API 中使用channels.getFullChannelchannels.getMessages ......

電報官方文檔中有一些方法我想使用它們,就像這個一樣,但我不知道如何使用電報實例訪問它。

我將它用於我的代碼:

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)

我應該提到,在官方文檔中指出這些方法可以被機器人訪問。

我也測試過

ctx.channels.getMessages('@username')

也沒有用

你好,你應該修改你的類構造函數並將 channelMode 設置為 True

const bot =  new Telegraf(Token, {

channelMode: true

})

那么你應該使用事件來獲取類似這樣的頻道消息

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

console.log(ctx.channelPost);

})

它將記錄所有頻道中的每個頻道帖子。 您可以使用 Simple if 條件記錄您想要的此頻道

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM