Skip to content

Instantly share code, notes, and snippets.

@fletcherist
Created July 3, 2018 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fletcherist/bcc4b46746d3814a5b3b28293033d49e to your computer and use it in GitHub Desktop.
Save fletcherist/bcc4b46746d3814a5b3b28293033d49e to your computer and use it in GitHub Desktop.
alice.welcome((ctx) => {
ctx.confirm({
reply: '18 есть?',
onYes: (ctx) => ctx.reply('добро пожаловать'),
onNo: (ctx) => ctx.reply('в другой раз'),
})
})
function aliceConfirmMiddleware() {
const store = new Map()
return (ctx) => {
// просто патчим метод (ну только по-другому, конечно же)
// это псевдокод
ctx.confirm = ({
reply,
yes = button('да'),
no = button('нет'),
onYes,
onNo,
}) => {
// ...
store.set(ctx.sessionId, {
yes,
no,
onYes,
onNo,
})
ctx.reply(reply)
}
// чекаем текущую команду
const data = store.get(ctx.sessionId)
if (data) {
// чекаем прошлый ответ (вообще там нажатие кнопки, ну да ладно)
if (ctx.req.payload === data.yes) { }) {
return data.onYes()
} else {
return data.onNo()
}
}
return ctx
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment