【发布时间】:2019-09-25 07:32:53
【问题描述】:
如何通过在 wc-shellinput 中使用 disabled 来禁用用户输入,仅当某些事件从 MessagesController 或任何对话框触发时,并在用户完成所需操作时启用它。 如果我想在与聊天机器人对话期间浏览 Web 应用程序怎么办? 这种类型的事件处理如何可能?
【问题讨论】:
标签: c# botframework direct-line-botframework
如何通过在 wc-shellinput 中使用 disabled 来禁用用户输入,仅当某些事件从 MessagesController 或任何对话框触发时,并在用户完成所需操作时启用它。 如果我想在与聊天机器人对话期间浏览 Web 应用程序怎么办? 这种类型的事件处理如何可能?
【问题讨论】:
标签: c# botframework direct-line-botframework
根据this WebChat sample,这是运行它的方法:
请参阅this sample 了解更多信息。
var message = context.MakeMessage();
禁用输入
message.ChannelData = new { chatBox = "disable" }
启用输入
message.ChannelData = new { chatBox = "enable" }
发送消息
await context.PostAsync(message);
channelData.chatBox 创建事件(在 index.html 脚本标签中)const store = window.WebChat.createStore(
{},
({ dispatch }) => next => action => {
if (action && action.payload && action.payload.activity && action.payload.activity.channelData && action.payload.activity.channelData.chatBox) {
const event = new Event(`chatBox`);
event.data = action.payload.activity.channelData.chatBox;
window.dispatchEvent(event);
}
return next(action);
}
);
window.addEventListener('chatBox', ({ data }) => {
const chatBox = document.querySelector(`[data-id="webchat-sendbox-input"]`);
switch(data){
case 'enable':
chatBox.disabled = false;
break;
case 'disable':
chatBox.disabled = true;
break;
}
});
已启用:
已禁用:
请务必确保在禁用后根据需要重新启用它!
这几乎是一样的。您报告说 BotChat 没有检测到所有消息,所以我改用了事件。
创建/发送事件
var disable = new Activity()
{
Type = ActivityTypes.Event,
Value = new { chatBox = "disable" }
};
var enable = new Activity()
{
Type = ActivityTypes.Event,
Value = new { chatBox = "enable" }
};
await turnContext.SendActivityAsync(disable);
在 BotChat 中监听事件
botConnection.activity$
.subscribe(function (activity) {
if (activity.type === 'event' && activity.value.chatBox) {
controlInput(activity.value.chatBox);
}
});
function controlInput(action) {
const chatBox = document.querySelector(`[class="wc-shellinput"]`);
switch(action) {
case 'enable':
chatBox.disabled = false;
break;
case 'disable':
chatBox.disabled = true;
break;
}
}
注意:如果您使用的是标准 BotChat css,则输入颜色不会改变。您需要自己添加 css。大致如下:
input:disabled {
background-color: black !important;
}
【讨论】:
botConnection.activity$ .subscribe(function(activity){ if(activity.type == "message"){ console.log(activity.type) } });
private async Task ShowAccountCard(IDialogContext context){ Attachment attachment = new Attachment(){ Content = await GetCardText("accountOptions"), ContentType = AdaptiveCard.ContentType }; var message = context.MakeMessage(); message.Attachments.Add(attachment); await context.PostAsync(message); context.Wait(MessageReceivedAsync1); } 显示卡片但调用了 MessageReceivedAsync1 却没有获得用户输入并单击提交,为什么?