how to use Telegram official api inside telegraf library

903 views Asked by At

I'm trying to create a telegram bot using telegraf library and npm

and the problem is I can't find a way to use all telegram methods like channels.getFullChannel or channels.getMessages inside telegraf api...

there are methods in telegram official docs that I want to use them, like this one, but I don know how to access it with telegraf instance.

i use this for my code:

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

but what is get in the console is:

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)

I should mention that in the official docs is pointed that these methods are accessible by Bots.

I also tested

ctx.channels.getMessages('@username')

didn't work either

1

There are 1 answers

1
Ruzi On

hello u should modify Your Class Constructor and set channelMode True

const bot =  new Telegraf(Token, {

channelMode: true

})

then u should use event for geting channel message something like that

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

console.log(ctx.channelPost);

})

it will log every channel post in all channel. You can use Simple if condition for log this channel u want