Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

telegraf-question

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

telegraf-question

Ask question with async/await

latest
Source
npmnpm
Version
1.1.4
Version published
Maintainers
1
Created
Source

Telegraf-Question

This package allows you to ask users questions and get an answer with Promise.

Install

$ npm i telegraf-question

$ yarn add telegraf-question

Documentation

TelegrafQuestion(config?: {
    cancelTimeout?: number; // Throw null if user doesn't reply; leave blank to wait forever.
})
TelegrafContext.ask(
    question: { text: string; extra?: ExtraReplyMessage } | string,
    cancel?: ((ctx: TelegrafContext) => Promise<boolean> | boolean) | string | null,
    type: 'text' | 'number' | 'callback_query' = 'text',
    errorText: { text: string; extra?: ExtraReplyMessage } | string | null = null,
    filter?: (ctx: TelegrafContext) => Promise<boolean> | boolean
): Promise<TelegrafContext | null> // returns null on cancel

Simple Example

import Telegraf, { Markup } from "telegraf";
import TelegrafQuestion from "telegraf-question";

let username = 'user';

let bot = new Telegraf(<BOT_TOKEN>);
bot.use(TelegrafQuestion({
    cancelTimeout: 300000 // 5 min
}));

bot.action('change_username', async (ctx, next) => {
    ctx.answerCbQuery();
    let newUsername = await ctx.ask('Send new username:');
    if (newUsername === null) {
        return next();
    }
    username = newUsername.message.text;
    next();
});

bot.use((ctx) => {
    ctx.reply(`Hi ${username}.`, Markup.inlineKeyboard([
        [Markup.callbackButton('Change username', 'change_username')],
    ]).extra());
});

bot.launch();

Full Example

import Telegraf, { Markup } from "telegraf";
import TelegrafQuestion from "telegraf-question";

let username = 'user';
let age = 0;
let online = false;

let bot = new Telegraf(<BOT_TOKEN>);
bot.use(TelegrafQuestion());

bot.action('change_username', async (ctx, next) => {
    ctx.answerCbQuery();
    let newUsername = await ctx.ask('Send new username:');
    username = newUsername.message.text;
    next();
});

bot.action('change_age', async (ctx, next) => {
    ctx.answerCbQuery();
    let newAge = await ctx.ask(
        {
            text: 'Send new age:',
            extra: {
                reply_markup: Markup.keyboard(['Cancel']).resize(),
            }
        },
        'Cancel',
        'number',
        'Please send a whole number.',
        (ctx) => {
            let num = parseInt(ctx.message?.text);
            return num >= 0;
        }
    );

    if (newAge !== null) {
        age = parseInt(newAge.message.text);
    }
    next();
});

bot.action('change_online', async (ctx, next) => {
    ctx.answerCbQuery();
    let status = await ctx.ask(
        {
            text: '<b>Select status:</b>',
            extra: {
                reply_markup: Markup.inlineKeyboard([
                    [
                        Markup.callbackButton('Online', 'online'),
                        Markup.callbackButton('Offline', 'offline'),
                    ],
                    [Markup.callbackButton('Cancel', 'cancel_status')]
                ]),
                parse_mode: 'HTML'
            }
        },
        (ctx) => ctx.callbackQuery?.data === 'cancel_status',
        'callback_query',
        null,
        (ctx) => ['online', 'offline'].some(el => ctx.callbackQuery.data === el),
    );
    
    if (status !== null) {
        status.answerCbQuery();
        online = status.callbackQuery.data === 'online';
    }
    next();
});

bot.use((ctx) => {
    ctx.reply(`Hi ${username}.\nAge: ${age}\nOnline: ${online ? 'Yes' : 'No'}`, Markup.inlineKeyboard([
        [Markup.callbackButton('Change username', 'change_username')],
        [Markup.callbackButton('Change age', 'change_age')],
        [Markup.callbackButton('Change online', 'change_online')],
    ]).extra());
});

bot.launch();

Keywords

telegraf

FAQs

Package last updated on 09 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts