How to use the telegraf function in telegraf

To help you get started, we’ve selected a few telegraf examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github EdJoPaTo / telegraf-inline-menu / test / javascript-user-hints.ts View on Github external
test('menu.middleware fails with .init() hint', t => {
  const menu: any = new TelegrafInlineMenu('yaay')

  const bot = new Telegraf('')
  // Normally user would use bot.use.
  // But telegraf will later use .middleware() on it. in order to check this faster, trigger this directly
  t.throws(() => bot.use(menu.middleware()), /bot\.use\(menu\.init/)
})
github EdJoPaTo / telegraf-inline-menu / test / toggle.ts View on Github external
test('isSetFunc falsy is like false', async t => {
  const menu = new TelegrafInlineMenu('yaay')
  menu.toggle('toggle me', 'c', {
    setFunc: async () => Promise.reject(new Error('Nothing has to be set when only showing the menu')),
    // Returning undefined is not possible from TypeScript but from JavaScript
    isSetFunc: () => undefined as any
  })

  const bot = new Telegraf('')
  bot.use(menu.init({actionCode: 'a'}))

  bot.context.answerCbQuery = async () => true
  bot.context.editMessageText = async (_text, extra: InlineExtra) => {
    t.deepEqual(extra.reply_markup.inline_keyboard, [[{
      text: emojiFalse + ' toggle me',
      callback_data: 'a:c-true'
    }]])
    return true
  }

  await bot.handleUpdate({callback_query: {data: 'a'}} as Update)
})
github EdJoPaTo / telegraf-inline-menu / test / constructor.ts View on Github external
test('simple text without buttons', async t => {
  const menu = new TelegrafInlineMenu('yaay')

  const bot = new Telegraf('')
  bot.use(menu.init({actionCode: 'a'}))

  bot.context.answerCbQuery = async () => true
  bot.context.editMessageText = async (text, extra: InlineExtra) => {
    t.is(text, 'yaay')
    t.deepEqual(extra.reply_markup.inline_keyboard, [])
    return true
  }

  await bot.handleUpdate({callback_query: {data: 'a'}} as Update)
})
github EdJoPaTo / telegraf-inline-menu / test / submenu.ts View on Github external
test('submenu without back button', async t => {
  const menu = new TelegrafInlineMenu('foo')
  menu.submenu('Submenu', 'c', new TelegrafInlineMenu('bar'))

  const bot = new Telegraf('')
  bot.use(menu.init({actionCode: 'a'}))

  bot.context.answerCbQuery = async () => true
  bot.context.editMessageText = async (_text, extra: InlineExtra) => {
    t.deepEqual(extra.reply_markup.inline_keyboard, [])
    return true
  }

  await bot.handleUpdate({callback_query: {data: 'a:c'}} as Update)
})
github EdJoPaTo / telegraf-inline-menu / test / command.ts View on Github external
function createTestBot(t: ExecutionContext, command: string | string[]): Telegraf {
  const menu = new TelegrafInlineMenu('foo')
    .manual('bar', 'c')
  menu.setCommand(command)

  const bot = new Telegraf('')
  bot.context.reply = async (text, extra: InlineExtra) => {
    t.is(text, 'foo')
    t.deepEqual(extra.reply_markup.inline_keyboard, [[{
      text: 'bar',
      callback_data: 'a:c'
    }]])

    return DUMMY_MESSAGE
  }

  bot.use(menu.init({actionCode: 'a'}))
  return bot
}
github EdJoPaTo / telegraf-inline-menu / test / select.submenu.ts View on Github external
test('something that is not an action in dynamic menu throws error', t => {
  const menu = new TelegrafInlineMenu('foo')
  const submenu = new TelegrafInlineMenu('bar')
    .question('Question', 'q', {
      questionText: '42',
      setFunc: () => {}
    })
  menu.select('a', ['a', 'b'], {
    submenu
  })
  const bot = new Telegraf('')
  t.throws(() => {
    bot.use(menu.init())
  }, /dynamic.+question.+menu.+a/)
})
github EdJoPaTo / telegraf-inline-menu / test / select.ts View on Github external
test('option array menu', async t => {
  const menu = new TelegrafInlineMenu('foo')
  menu.select('c', ['a', 'b'], {
    setFunc: () => t.fail()
  })

  const bot = new Telegraf('')
  bot.use(menu.init({actionCode: 'a'}))

  bot.context.answerCbQuery = async () => true
  bot.context.editMessageText = async (_text, extra: InlineExtra) => {
    t.deepEqual(extra.reply_markup.inline_keyboard, [[
      {
        text: 'a',
        callback_data: 'a:c-a'
      }, {
        text: 'b',
        callback_data: 'a:c-b'
      }
    ]])
    return true
  }
github EdJoPaTo / telegraf-inline-menu / test / button.ts View on Github external
test('button updates menu', async t => {
  t.plan(2)
  const menu = new TelegrafInlineMenu('yaay')
  menu.button('hit me', 'c', {
    doFunc: () => t.pass()
  })

  const bot = new Telegraf('')
  bot.use(menu.init({actionCode: 'a'}))

  bot.context.answerCbQuery = async () => true
  bot.context.editMessageText = async (_text, extra: InlineExtra) => {
    t.deepEqual(extra.reply_markup.inline_keyboard, menuKeyboard)
    return true
  }

  await bot.handleUpdate({callback_query: {data: 'a:c'}} as Update)
})
github EdJoPaTo / telegraf-inline-menu / test / button-rows.ts View on Github external
test('joinLastRow', async t => {
  const menu = new TelegrafInlineMenu('yaay')
    .manual('hit me', 'c')
    .manual('hit me hard', 'd', {joinLastRow: true})

  const bot = new Telegraf('')
  bot.use(menu.init({actionCode: 'a'}))

  bot.context.answerCbQuery = async () => true
  bot.context.editMessageText = async (_text, extra: InlineExtra) => {
    t.deepEqual(extra.reply_markup.inline_keyboard, [[button1, button2]])
    return true
  }

  await bot.handleUpdate({callback_query: {data: 'a'}} as Update)
})
github rjmunhoz / carbon-telegram-bot / src / presentation / app.ts View on Github external
export async function factory (config: IAppConfig) {
  const bot = new Telegraf(config.telegram.token, { telegram: { webhookReply: false } })

  bot.use((ctx, next: any) => {
    Sentry.configureScope(scope => {
      setUser(ctx, scope)
      scope.setExtra('update', ctx.update)
    })

    next(ctx)
      .catch((err: any) => {
        console.error(`Error processing update ${ctx.update.update_id}: ${err.message}`)
        Sentry.captureException(err)
        if (ctx.chat && ctx.message) {
          ctx.telegram.sendMessage(ctx.chat.id, 'Error processing message', { reply_to_message_id: ctx.message.message_id })
            .catch(console.error)
        }
      })