Exemplo n.º 1
9
})

calculator.on('sub', (ctx) => {
  ctx.session.value = (ctx.session.value || 0) - ctx.state.amount
  return editText(ctx)
})

calculator.on('clear', (ctx) => {
  ctx.session.value = 0
  return editText(ctx)
})

calculator.otherwise((ctx) => ctx.reply('🌯'))

function editText (ctx) {
  if (ctx.session.value === 42) {
    return ctx.answerCbQuery('Answer to the Ultimate Question of Life, the Universe, and Everything', true)
      .then(() => ctx.editMessageText('🎆'))
  }
  return ctx.editMessageText(`Value: <b>${ctx.session.value}</b>`, markup).catch(() => undefined)
}

const bot = new Telegraf(process.env.BOT_TOKEN)
bot.use(session({ ttl: 10 }))
bot.start((ctx) => {
  ctx.session.value = 0
  return ctx.reply(`Value: <b>${ctx.session.value}</b>`, markup)
})
bot.on('callback_query', calculator)
bot.startPolling()
Exemplo n.º 2
-6
  (ctx) => {
    ctx.reply('Step 1')
    ctx.wizard.next()
  },
  (ctx) => {
    if (ctx.message && ctx.message.text !== 'ok') {
      return ctx.replyWithMarkdown('Send `ok`')
    }
    ctx.reply('Step 2 ')
    ctx.wizard.next()
  },
  (ctx) => {
    ctx.reply('Step 3')
    ctx.wizard.next()
  },
  (ctx) => {
    ctx.reply('Step 4')
    ctx.wizard.next()
  },
  (ctx) => {
    ctx.reply('Done')
    ctx.scene.leave()
  }
)

const bot = new Telegraf(process.env.BOT_TOKEN)
const stage = new Stage([superWizard], { default: 'super-wizard' })
bot.use(session())
bot.use(stage.middleware())
bot.startPolling()