Skip to content

Instantly share code, notes, and snippets.

@etrex
Created June 5, 2020 08:37
Show Gist options
  • Save etrex/c52b450c04bcb6d07fac9b044428bd0f to your computer and use it in GitHub Desktop.
Save etrex/c52b450c04bcb6d07fac9b044428bd0f to your computer and use it in GitHub Desktop.
Bottender Conversation for Scene
const { router, text, messenger } = require('bottender/router');
const {
run,
registerAction,
getAction,
prompt,
} = require('@bottender/proposal-conversation');
async function SayHi(context) {
await context.sendText('Hi!');
}
async function UNKNOWN(context) {
console.log('unknown text: ' + context.event.text);
await context.sendButtonTemplate(
"Hi! I'm sorry, but i'm just a bot and i don't know how to answer that.\nPlease let me call the human and he will shortly respond to your question.",
[
{
type: 'postback',
title: 'TRANSFER',
payload: 'TRANSFER',
},
]
);
}
let cancelScene = {
contentType: 'text',
title: 'Cancel',
payload: 'CANCEL_SCENE',
};
registerAction('TransferScene', async function HandlePostback(context, props) {
// leave Scene
if (context.event.payload && context.event.payload === 'CANCEL_SCENE') {
await context.sendText('Leaving scene');
return;
}
if (!props.card) {
await context.sendText('Please provide me with me your member number:', {
quickReplies: [cancelScene],
});
return prompt('card');
}
if (!props.sum) {
// step 0
console.log('step 0');
await context.sendText('gimme sum', {
quickReplies: [cancelScene],
});
return prompt('sum');
}
if (!props.confirm) {
// step 1
console.log('step 1');
await context.sendText('ok transferring, sure?', {
quickReplies: [
{
contentType: 'text',
title: 'Yes',
payload: 'YES_SCENE',
},
cancelScene,
],
});
return prompt('confirm');
}
if (context.event.payload && context.event.payload === 'YES_SCENE') {
// step 2
console.log('step 2');
let sum = props.sum;
await context.sendText('On it.... Might take a couple of sec... ');
setTimeout(() => {
context.sendText('You have been successfully charged with ' + sum);
}, 3000);
}
// leave Scene
console.log('all steps used, kinda time to leave scene');
await context.sendText('Leaving scene');
});
registerAction('HandlePostback', async function HandlePostback(context) {
let payload = context.event.postback.payload;
// this starts the scene!
if (payload === 'TRANSFER') {
return getAction('TransferScene');
}
});
module.exports = run(function App() {
return router([
// return the `SayHi` action when receiving "hi" text messages
text('hi', SayHi),
// return the `SayHello` action when receiving "hello" text messages
text('*', UNKNOWN),
messenger.postback(getAction('HandlePostback')),
]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment