您可以通过使用按钮(或您选择的实现)以及通过网络聊天发送活动以及13.core-bot 示例的cancel / interrupt component dialog 来执行此操作(下面的示例使用 JS SDK,但在其他示例中可用SDK)。
网络聊天:当点击button 时,网络聊天会发送WEB_CHAT/SEND_ACTIVITY,其中包含“reset”文本。
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/markdown-it@8.4.2/dist/markdown-it.min.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/@babel/standalone@7.8.7/babel.min.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/regenerator-runtime@0.13.3/runtime.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-redux@7.1.0/dist/react-redux.min.js"></script>
<script crossorigin="anonymous" src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
<style>
html,
body {
height: 100%
}
body {
margin: 0
}
#webchat {
height: 100%;
width: 100%s;
}
</style>
</head>
<body>
<div id="webchat" role="main"></div>
<button id='resetBtn' type="button">Reset Dialog</button>
<script type="text/babel" data-presets="es2015,react,stage-3">
( async function () {
// 'use strict';
const { ReactDOM: { render }, WebChat: { ReactWebChat } } = window;
const store = window.WebChat.createStore( {}, ( { dispatch } ) => next => action => {
return next( action );
} );
const res = await fetch( 'http://localhost:3500/directline/token', { method: 'POST' } );
let { token } = await res.json();
await render(
<ReactWebChat
directLine={await window.WebChat.createDirectLine( {token} )}
store={store}
/>,
document.getElementById( 'webchat' )
);
document.querySelector( '#webchat > *' ).focus();
const resetBtn = document.getElementById( 'resetBtn' );
resetBtn.addEventListener( 'click', ( e ) => {
e.preventDefault();
store.dispatch( {
type: 'WEB_CHAT/SEND_MESSAGE',
payload: {
text: `Reset`
}
} )
} )
} )().catch( err => console.error( err ) );
</script>
</body>
</html>
cancelAndHelpDialog.js:当机器人接收到“重置”活动时,它会调用cancelAllDialogs(true) 清除堆栈并将机器人返回到初始对话框。
async interrupt(innerDc) {
if (innerDc.context && innerDc.context.activity) {
const { activity } = innerDc.context;
if (activity.text) {
const text = activity.text;
const activityText = text.toLowerCase();
let message = '';
switch (activityText) {
case 'reset': {
message = 'Request approved: resetting dialog';
const resetMessage = MessageFactory.text(message, message, InputHints.IgnoringInput);
resetMessage.channelData = { action: 'dialog_reset' };
await innerDc.context.sendActivity(resetMessage);
return await innerDc.cancelAllDialogs(true);
}
case 'help':
case '?': {
message = 'Show help here';
await innerDc.context.sendActivity(message, message, InputHints.ExpectingInput);
return { status: DialogTurnStatus.waiting };
}
case 'cancel':
case 'quit': {
message = 'Cancelling...';
await innerDc.context.sendActivity(message, message, InputHints.IgnoringInput);
return await innerDc.beginDialog(EXIT_DIALOG);
}
}
}
}
}
这是相当简单的,可能需要进行一些“清理”以满足您的需求,包括显示对话框正在重新启动的某种响应。
希望有帮助!