Skip to content

Instantly share code, notes, and snippets.

@petitroto
Created March 6, 2022 22:16
Show Gist options
  • Save petitroto/81e9475b8da2a906957ea81e646ea698 to your computer and use it in GitHub Desktop.
Save petitroto/81e9475b8da2a906957ea81e646ea698 to your computer and use it in GitHub Desktop.
Botpress Webchatを開くたびに新しい会話から始める方法
/**
* Webchatの×ボタン押下時にセッションを削除するフック
*/
const resetSession = async () => {
if (event.type === 'reset-session-trigger') {
const sessionId = bp.dialog.createId(event)
await bp.dialog.deleteSession(sessionId, event.botId)
event.setFlag(bp.IO.WellKnownFlags.SKIP_DIALOG_ENGINE, true)
}
}
return resetSession()
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Example</title>
</head>
<body>
<script src="http://localhost:3000/assets/modules/channel-web/inject.js"></script>
<script>
window.botpressWebChat.init({
host: 'http://localhost:3000',
botId: 'example'
});
window.addEventListener('message', function (event) {
// Webchatを開くたびにproactive-triggerを送る
if ((event.data.payload || {}).type === 'visit' || event.data.name === 'webchatOpened') {
window.botpressWebChat.sendEvent({
type: 'proactive-trigger',
channel: 'web',
payload: {text: 'dummy'}
});
}
// Webchatを閉じるたびにreset-session-triggerを送る
if (event.data.name === 'webchatClosed') {
window.botpressWebChat.sendEvent({
type: 'reset-session-trigger',
channel: 'web',
payload: {text: 'dummy'}
});
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment