Created
March 6, 2022 22:16
-
-
Save petitroto/81e9475b8da2a906957ea81e646ea698 to your computer and use it in GitHub Desktop.
Botpress Webchatを開くたびに新しい会話から始める方法
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
/** | |
* 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() |
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
<!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