Skip to content

Instantly share code, notes, and snippets.

@ikr4-m
Created July 22, 2020 13:28
Show Gist options
  • Save ikr4-m/ae69ddc0a7763fc149a664f1c71bc1b2 to your computer and use it in GitHub Desktop.
Save ikr4-m/ae69ddc0a7763fc149a664f1c71bc1b2 to your computer and use it in GitHub Desktop.
Simple Example for Telegraf API
const { Telegraf } = require('telegraf')
const Markup = require('telegraf/markup')
const BaseScene = require('telegraf/scenes/base')
const WizardScene = require('telegraf/scenes/wizard')
const Stage = require('telegraf/stage')
const session = require('telegraf/session')
require('dotenv/config')
require('console-stamp')(console)
const bot = new Telegraf(process.env.TOKEN)
// Pilihan dalam chat menggunakan BaseScene
const pilihSesuatu = new BaseScene('PILIH_SESUATU')
pilihSesuatu.enter(
/**
* @param {import('telegraf/typings/context').TelegrafContext} ctx
*/
ctx => {
ctx.session.myData = {}
ctx.reply('Silahkan pilih!', Markup.inlineKeyboard([
Markup.callbackButton('Satu', 'ANGKA_Satu'),
Markup.callbackButton('Dua', 'ANGKA_Dua'),
Markup.callbackButton('Tiga', 'ANGKA_Tiga')
]).extra())
})
pilihSesuatu.action('ANGKA_Satu', ctx => {
ctx.session.myData.angka = 'Satu'
return ctx.scene.leave()
})
pilihSesuatu.action('ANGKA_Dua', ctx => {
ctx.session.myData.angka = 'Dua'
return ctx.scene.leave()
})
pilihSesuatu.action('ANGKA_Tiga', ctx => {
ctx.session.myData.angka = 'Tiga'
return ctx.scene.leave()
})
pilihSesuatu.leave(ctx => {
const angka = ctx.session.myData.angka
if (!angka) return
ctx.reply(`Anda memilih angka ${ctx.session.myData.angka}`)
})
pilihSesuatu.use(ctx => {
ctx.replyWithMarkdown('Anda memilih inputan yang salah!')
return ctx.scene.leave()
})
// Pilihan pake Keyboard menggunakan WizardScene
const judulFilm = new WizardScene(
'PILIH_FILM',
/**
* @param {import('telegraf/typings/context').TelegrafContext} ctx
*/
ctx => {
ctx.reply(
'Pilih Genre Film!',
Markup.keyboard([
['Action', 'Drama'],
['Romance', 'Horror']
]).oneTime().resize().extra()
)
ctx.wizard.state.nama = ''
return ctx.wizard.next()
},
async ctx => {
const daftar = ['Action', 'Drama', 'Romance', 'Horror']
ctx.wizard.state.nama = ctx.message.text
if (daftar.includes(ctx.wizard.state.nama)) {
await ctx.reply(`Anda memilih ${ctx.wizard.state.nama}`)
} else {
await ctx.reply('Pilihan tidak ditemukan!')
}
return ctx.scene.leave()
}
)
// Sebut nama pake WizardScene beserta multistate
const tandingAngka = new WizardScene(
'TANDING_ANGKA',
/**
* @param {import('telegraf/typings/context').TelegrafContext} ctx
*/
async ctx => {
ctx.wizard.state = {}
await ctx.replyWithMarkdown('*[TANDING PELI]*\nMasukkan nama pertama:')
return ctx.wizard.next()
},
/**
* @param {import('telegraf/typings/context').TelegrafContext} ctx
*/
async ctx => {
ctx.wizard.state.orangPertama = ctx.message.text
await ctx.reply('Masukkan nama kedua:')
return ctx.wizard.next()
},
/**
* @param {import('telegraf/typings/context').TelegrafContext} ctx
*/
async ctx => {
ctx.wizard.state.orangKedua = ctx.message.text
const ratePertama = Math.floor(Math.random() * 10)
const rateKedua = Math.floor(Math.random() * 10)
let result = ''
if (ratePertama === rateKedua) result = 'Seri!'
else if (ratePertama > rateKedua) result = `${ctx.wizard.state.orangPertama} menang!`
else if (ratePertama < rateKedua) result = `${ctx.wizard.state.orangKedua} menang!`
await ctx.replyWithMarkdown(
`*Result*\n\n${ctx.wizard.state.orangPertama}\n8${'='.repeat(ratePertama)}D\n${ctx.wizard.state.orangKedua}\n8${'='.repeat(rateKedua)}D\n\n${result}`
)
return ctx.scene.leave()
}
)
const stage = new Stage([pilihSesuatu, judulFilm, tandingAngka])
bot.use(session())
bot.use(stage.middleware())
bot.command('pilih', Stage.enter('PILIH_SESUATU'))
bot.command('judulFilm', Stage.enter('PILIH_FILM'))
bot.command('tanding', Stage.enter('TANDING_ANGKA'))
bot.launch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment