Skip to content

Instantly share code, notes, and snippets.

@petitroto
Created August 24, 2022 21:08
Show Gist options
  • Save petitroto/710ff07730075546c07cdb6762b3dac6 to your computer and use it in GitHub Desktop.
Save petitroto/710ff07730075546c07cdb6762b3dac6 to your computer and use it in GitHub Desktop.
BotpressのフックからURLをブラウザで開くサンプル
// 'before incoming middleware' hook
if (event.preview === 'foo') {
const data = {url: 'http://example.com'}
const event2Browser = bp.IO.Event({
type: 'data',
channel: 'web',
direction: 'outgoing',
target: event.target,
botId: event.botId,
payload: {
data
}
})
return bp.events.sendEvent(event2Browser)
}
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>WebChat</title>
</head>
<body>
<script src="http://localhost:3000/assets/modules/channel-web/inject.js"></script>
<script>
window.botpressWebChat.init({
host: 'http://localhost:3000',
botId: 'mybot'
});
window.addEventListener('message', function (event) {
if (event.data.type === 'data') {
window.open(event.data.data.url)
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment