Created
January 16, 2019 20:47
-
-
Save slvnperron/b5212b34a2ed8d0bc25ff934c9a1790a to your computer and use it in GitHub Desktop.
Haytham jumpTo (bp 10)
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
/** | |
* Description of the action goes here | |
* @param {String} params.name=value Description of the parameter goes here | |
* @param {Number} [params.age] Optional parameter | |
*/ | |
async function yourCustomAction(state, event, params) { | |
return state; | |
} | |
/** | |
* Jumps to a specific flow/node | |
* @param {String} params.flow=nameOfFlow The name of the flow you want to jumpTo | |
* @param {Number} [params.node=entry] Optional node to flow to inside the flow | |
*/ | |
async function jumpToFlow(state, event, params) { | |
const originalEvent = { ...event }; | |
delete originalEvent.bp; | |
console.log(event); | |
await event.bp.middlewares.sendIncoming({ | |
sessionId: event.user.id, | |
user: { id: event.user.userId }, | |
platform: event.platform, | |
text: "jumpTo", | |
type: "internal::jumpTo", | |
raw: { | |
flow: params.flow.endsWith(".flow.json") | |
? params.flow | |
: params.flow + ".flow.json", | |
node: params.node, | |
originalEvent | |
} | |
}); | |
} | |
module.exports = { yourCustomAction, jumpToFlow }; |
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
bp.hear({ type: "internal::jumpTo" }, async (event, next) => { | |
try { | |
const originalEvent = { ...event.raw.originalEvent, bp: event.bp }; | |
const stateId = event.sessionId || event.user.id; | |
await event.bp.dialogEngine.jumpTo( | |
stateId, | |
event.raw.flow, | |
event.raw.node, | |
{ resetState: false } | |
); | |
await event.bp.dialogEngine.processMessage(stateId, originalEvent); // Continue processing | |
} catch (e) { | |
await event.reply("#builtin_text", { text: `Failed to transition.` }); | |
console.log(e); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment