Created
June 5, 2020 09:17
-
-
Save etrex/e520c8059828456a92661b3614a5dd8d to your computer and use it in GitHub Desktop.
Another sample code for Scene
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('step0', async function Step0(context, props) { | |
// leave Scene | |
if (context.event.payload && context.event.payload === 'CANCEL_SCENE') { | |
await context.sendText('Leaving scene'); | |
return; | |
} | |
// scene handler | |
if (!props.input) { | |
await context.sendText('Please provide me with me your member number:', { | |
quickReplies: [cancelScene], | |
}); | |
return prompt('input'); | |
} | |
let card = props.input; | |
context.setState({ | |
sceneData: { ...context.state.sceneData, card }, | |
}); | |
// next step | |
return getAction('step1'); | |
}); | |
registerAction('step1', async function Step1(context, props) { | |
// leave Scene | |
if (context.event.payload && context.event.payload === 'CANCEL_SCENE') { | |
await context.sendText('Leaving scene'); | |
return; | |
} | |
// scene handler | |
if (!props.input) { | |
await context.sendText('gimme sum', { | |
quickReplies: [cancelScene], | |
}); | |
return prompt('input'); | |
} | |
let sum = props.input; | |
context.setState({ | |
sceneData: { ...context.state.sceneData, sum }, | |
}); | |
// next step | |
return getAction('step2'); | |
}); | |
registerAction('step2', async function Step2(context, props) { | |
// leave Scene | |
if (context.event.payload && context.event.payload === 'CANCEL_SCENE') { | |
await context.sendText('Leaving scene'); | |
return; | |
} | |
// scene handler | |
if (!props.input) { | |
await context.sendText('ok transferring, sure?', { | |
quickReplies: [ | |
{ | |
contentType: 'text', | |
title: 'Yes', | |
payload: 'YES_SCENE', | |
}, | |
cancelScene, | |
], | |
}); | |
return prompt('input'); | |
} | |
let confirm = context.event.payload; | |
context.setState({ | |
sceneData: { ...context.state.sceneData, confirm }, | |
}); | |
// next step | |
return getAction('finish'); | |
}); | |
registerAction('finish', async function finish(context) { | |
// scene handler | |
const confirm = context.state.sceneData.confirm; | |
if (confirm === 'YES_SCENE') { | |
let sum = context.state.sceneData.sum; | |
await context.sendText('On it.... Might take a couple of sec... '); | |
setTimeout(() => { | |
context.sendText('You have been successfully charged with ' + sum); | |
}, 3000); | |
} | |
context.setState({ | |
sceneData: undefined, | |
}); | |
// 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('step0'); | |
} | |
}); | |
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