Telegraf: If condition in bot.use() or only a specific user shall be able to access bot commands

Created on 1 Jul 2017  Â·  3Comments  Â·  Source: telegraf/telegraf

Hey there
i want to check the user id when user do any task
i think the best way is in bot.use()
but it will remain in bot.use() and ignore entire code
what should i do? is there a better way to do?
Thanks
here is my code :

const Telegraf = require('telegraf')
const { Extra, Markup } = require('telegraf')
var token = '*************************************';
const bot = new Telegraf(token)
var request = require('request');
var inputdevices;
var inputdevicesstatus;
var X;
var Masterkey = '107867674';

bot.use((ctx) => {
    if (ctx.from.id == Masterkey) {
        // Continue to do codes
    }
    else {
        ctx.reply("your access denied")
    }
})

bot.command('start', (ctx) => {
    ctx.reply("به خانه هوشمند خوش آمدید")
        .then(() => ctx.reply('گزینه مورد نظر رو از منو انتخاب کنید', Markup
            .keyboard([
                ['کلید ها', 'سناریو', 'پلن ها'],
            ])
            //.oneTime()
            .resize()
            .extra()
        ))
})
question

Most helpful comment

See cascading approach in documentation

bot.use((ctx, next) => {
    if (ctx.from.id == Masterkey) {
       return next()
    }    
    ctx.reply("access denied")
})

Also you can use acl factory:

const { Composer } = require('telegraf')
bot.use(Composer.acl(Masterkey, () => ctx.reply("access denied")))

All 3 comments

@aminjoharinia never post your bot token in public! anyone can steal and use it. aquire a new one in botfather under api token. if i understand your question right: if only a specific user shall be able to access your commands in your bot use this: (create a new case for every command)

var token = 'YOUR_NEW_TOKEN'
const bot = new Telegraf(token)
const { Extra, memorySession, Markup } = Telegraf
var request = require('request');
var inputdevices;
var inputdevicesstatus;
var X;
var Masterkey = 107867674;
bot.use(memorySession())

bot.use((ctx) => {
    console.log('test')
    if (ctx.from.id === Masterkey) {
        ctx.session.do = ctx.message.text
        switch (ctx.session.do) {
            case '/start':
                ctx.reply("به خانه هوشمند خوش آمدید")
                    .then(() => ctx.reply('گزینه مورد نظر رو از منو انتخاب کنید', Markup
                        .keyboard([
                            ['کلید ها', 'سناریو', 'پلن ها'],
                        ])
                        //.oneTime()
                        .resize()
                        .extra()
                    ))
                break
            default:
                ctx.reply('Unknown command')
                break
        }
    } else {
        ctx.reply('Access denied')
    }
})

or add

if (ctx.from.id === Masterkey) {
    // okay
} else { /* deniend */ }

in with every command

Hi
@bostrot
Thanks for your answer
Sorry about token :( , big mistake
you understand my question very well " if only a specific user shall be able to access your commands in your bot "
i have +1000 lines of code and it is so hard to make switch for each of them
I was added " if else " in every command
but i think there should be a much simpler solution for it
it's for home automation and should work for specific people
Thanks

See cascading approach in documentation

bot.use((ctx, next) => {
    if (ctx.from.id == Masterkey) {
       return next()
    }    
    ctx.reply("access denied")
})

Also you can use acl factory:

const { Composer } = require('telegraf')
bot.use(Composer.acl(Masterkey, () => ctx.reply("access denied")))
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  Â·  3Comments

supermomme picture supermomme  Â·  3Comments

Blaumaus picture Blaumaus  Â·  3Comments

Stephirio picture Stephirio  Â·  3Comments

MohGanji picture MohGanji  Â·  3Comments