对于 MS Botframework 网络聊天,有没有办法在网络聊天中呈现之前拦截用户消息并进行更改?
问问题
149 次
1 回答
2
使用该createStore()
方法很容易做到这一点。
在位于您页面的网络聊天脚本中,使用上述方法创建商店。在其中,action.type
将 'WEB_CHAT/SEND_MESSAGE' 匹配。这将在显示之前捕获通过网络聊天组件传递的每条消息。
请注意,此更改的文本(或您正在更改的任何值)是发送给机器人的内容。action
是根对象。action.payload
,有效地表示活动。这是您可以找到文本值等的地方。
在if
语句中,执行您要进行的任何更改,然后返回action
对象。
最后,将store
对象包含在renderWebChat
组件中。这应该设置你。
在下面的示例中,我将文本附加到文本字段,在呈现和显示之前对其进行更改。
<script>
( async function () {
const res = await fetch( 'http://somesite/directline/token', { method: 'POST' } );
const { token } = await res.json();
// We are using a customized store to add hooks to connect event
const store = window.WebChat.createStore( {}, ( { dispatch } ) => next => action => {
if ( action.type === 'WEB_CHAT/SEND_MESSAGE' ) {
action.payload.text = action.payload.text + ' (Hello from behind the curtain)'
}
return next( action );
} );
window.WebChat.renderWebChat( {
directLine: window.WebChat.createDirectLine( { token } ),
userID: 'user123',
username: 'johndoe',
botAvatarInitials: 'BB',
userAvatarInitials: 'JD',
store
}, document.getElementById( 'webchat' ) );
document.querySelector( '#webchat > *' ).focus();
} )().catch( err => console.error( err ) );
</script>
希望有帮助!
于 2019-06-28T19:29:22.897 回答