简体   繁体   English

如何从botframework v3 c#发送事件并使用Direct Line在客户端javascript上收听?

[英]How send event from botframework v3 c# and listen at client side javascript using Direct Line?

How to disable user input by using disabled at wc-shellinput only when some event fires from MessagesController or from any dialog and enable it when desired action accomplished by user. 如何仅在某些事件从MessagesController或任何对话框触发时使用wc-shellinput处于禁用状态时禁用用户输入,并在用户完成所需操作时启用它。 What if I want to navigate web app during conversation with chatbot? 如果我想在与chatbot对话期间浏览网络应用程序,该怎么办? How this type of event handling possible? 这种类型的事件处理可能如何?

Here's how you can get it running, based off of this WebChat sample : 以下是基于此WebChat示例的运行方式

Add custom ChannelData to your outgoing activity (in bot code) 将自定义ChannelData添加到您的传出活动(在bot代码中)

See this sample for more info. 有关详细信息,请参阅此示例

var message = context.MakeMessage();

To Disable Input 禁用输入

message.ChannelData = new { chatBox = "disable" }

To Enable Input 启用输入

message.ChannelData = new { chatBox = "enable" }

Send the message 发送消息

await context.PostAsync(message);

Create the event for channelData.chatBox (in index.html script tags) 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);
    }
);

Listen for event and enable/disable the chat box (in index.html script tags) 侦听事件并启用/禁用聊天框(在index.html脚本标记中)

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;
    }
});

Enabled: 启用:

在此输入图像描述

Disabled: 禁用:

在此输入图像描述

Just be sure you ensure you re-enable it after disabling, as needed! 确保您确保在禁用后根据需要重新启用它!

With BotChat 用BotChat

It's pretty much the same thing. 这几乎是一回事。 You're reporting that BotChat isn't detecting all messages, so I used events instead. 您报告BotChat未检测到所有消息,因此我使用了事件。

Create/send events 创建/发送事件

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);

Listen for events in BotChat 在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;
    }
}

Note: If you're using the standard BotChat css, the input color doesn't change. 注意:如果您使用的是标准BotChat css,则输入颜色不会更改。 You'll need to add the css yourself. 你需要自己添加css。 Something along the lines of: 有点像:

input:disabled {
          background-color: black !important;
      }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用c#发送microsoft botframework sdk v4中本地文件夹中的图像 - how to send images which are in local folder in microsoft botframework sdk v4 using c# 将按钮从C#发送到客户端 - Send button from c# to client side 如何使用javascript从客户端获取c#timezoneinfo类的客户端时区ID - How to get the client timezone id for c# timezoneinfo class from client side using javascript BotFramework V4:如何从机器人发送事件并在 React WebChat 中捕获它? - BotFramework V4: how to send an event from the bot and catch it in react WebChat? 使用Botframework V3将数据从Web表单发送到Web聊天机器人 - Send data from web form to web chat bot with Botframework V3 如何在客户端(使用javascript)和服务器端验证mac地址和ipaddress(使用c#) - How to Validate mac address & ipaddress in client side(using javascript) & server side(using c#) 如何使用c#将对话消息记录到Microsoft botframework sdk v4中的Azure SQL数据库中 - how to log conversation messages into azure sql database in microsoft botframework sdk v4 using c# 使用 TestFlow 时如何将选项传递给对话框 - Azure botframework v4 - C# - 单元测试 - How to pass in options to a Dialog when using TestFlow - Azure botframework v4 - C# - Unit Tests 如何使用C#或ASP.net从客户端调用私有void按键事件处理程序,以仅允许数字进入文本框 - How to call a private void keypress event handler from client side using C# or ASP.net to allow only numbers into textbox C#使用文本框作为“发布” /“便笺”客户端事件 - C# using textbox as “post it” / “sticky memo” client side event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM