您可以通过使用按钮(或您选择的实现)并通过网络聊天发送活动来完成此操作取消/中断组件对话框 of the 13.核心机器人示例(下面的示例使用 JS SDK,但在其他 SDK 中也可用)。
网络聊天:当。。。的时候button
单击后,网络聊天将发送WEB_CHAT/SEND_ACTIVITY
其中包括文本“重置”。
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/[email protected]/dist/markdown-it.min.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/@babel/[email protected]/babel.min.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/[email protected]/runtime.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/[email protected]/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);
}
}
}
}
}
这是相当简单的,可能需要一些“清理”来满足您的需求,包括显示对话框正在重新启动的某种响应。
希望得到帮助!