从嵌入式网络聊天发送事件

2023-12-10

我正在尝试从嵌入式网络聊天发送和接收事件,该网络聊天遵循本示例中的网站代码https://github.com/ryanvolum/backChannelBot并且机器人实现了来自的代码Bot框架获取内嵌聊天控制页面的ServiceUrl埃泽奎尔 回答了

这是我的设置中的样子 索引.html

<!DOCTYPE html>
<!-- 
NOTE: This sample requires a bot which can send and receive specific event messages. Follow the instructions on 
https://github.com/ryanvolum/backChannelBot to deploy such a bot. 
This is a sample HTML file which shows how to embed an instance of WebChat which listens for event activities. For the sake
of demonstration it specifically listens for events of name "changeBackground". Using the backChannelBot sample 
our page can listen for events of name "changeBackground" and send events of name "buttonClicked". This 
highlights the ability for a bot to communicate with a page that embeds the bot through WebChat. 

1. Build the project: "npm run build"
2. Start a web server: "npm run start"
3. Aim your browser at "http://localhost:8000/samples/backchannel?[parameters as listed below]"
For ease of testing, several parameters can be set in the query string:
    * s = Direct Line secret, or
    * t = Direct Line token (obtained by calling Direct Line's Generate Token)
    * domain = optionally, the URL of an alternate Direct Line endpoint
    * webSocket = set to 'true' to use WebSocket to receive messages (currently defaults to false)
    * userid, username = id (and optionally name) of bot user
    * botid, botname = id (and optionally name) of bot
-->
<html>

<head>
    <meta charset="UTF-8" />
    <title>Bot Chat</title>
    <link href="../../botchat.css" rel="stylesheet" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <style>
        .wc-chatview-panel {
            width: 320px;
            height: 500px;
            position: relative;
        }
        .h2{
            font-family: Segoe UI;
        }
    </style>
</head>

<body>
    <h2 style="font-family:Segoe UI;">Type a color into the WebChat!</h2>
    <div id="BotChatGoesHere" class="wc-narrow"></div>
    <button onclick="postButtonMessage()" style="width:120px;height:60px;padding:20px;margin-left:80px;margin-top:20px;">Click Me!</button>

    <script src="../../botchat.js"></script>
    <script>
            var params = BotChat.queryParams(location.search);
            var user = {
                id: params['me'] || 'userid',
                name: params["tester"] || 'username'
                };

            var bot = {
                id: params['somebot'] || 'botid',
                name: params["somebot"] || 'botname'
            };
            window['botchatDebug'] = params['debug'] && params['debug'] === "true";
            var botConnection = new BotChat.DirectLine({
                secret: params['mysecret'],
                token: params['t'],
                domain: params['ngroktunneledurl.com/api/messages'],
                webSocket: params['webSocket'] && params['webSocket'] === "true" // defaults to true
            });
            BotChat.App({
                botConnection: botConnection,
                user: user,
                bot: bot
            }, document.getElementById("BotChatGoesHere"));
            botConnection.activity$
                .filter(activity => activity.type === "event" && activity.name === "changeBackground")
                .subscribe(activity => changeBackgroundColor(activity.value))
            const changeBackgroundColor = (newColor) => {
                document.body.style.backgroundColor = newColor;
            }
            const postButtonMessage = () => {
                botConnection
                    .postActivity({type: "event", value: "", from: {id: "me" }, name: "buttonClicked"})
                    .subscribe(id => console.log("success"));
            }
        </script>
</body>

</html>

和机器人文件 消息控制器.cs

using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using Kaseya_AI_Kbot.LuisDialog;

[BotAuthentication]
public class MessagesController : ApiController
{
   public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        if (activity.Type == ActivityTypes.Event &&
            string.Equals(activity.Name, "buttonClicked", StringComparison.InvariantCultureIgnoreCase))
        {
            ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

            // return our reply to the user
            Activity reply = activity.CreateReply("I see that you just pushed that button");
            await connector.Conversations.ReplyToActivityAsync(reply);
        }

        if (activity.Type == ActivityTypes.Message)
        {
            ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

            // return our reply to the user
            var reply = activity.CreateReply();
            reply.Type = ActivityTypes.Event;
            reply.Name = "changeBackground";
            reply.Value = activity.Text;
            await connector.Conversations.ReplyToActivityAsync(reply);
        }
        else
        {
            HandleSystemMessage(activity);
        }
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
    }

    private async Task HandleSystemMessage(Activity message)
    {
        if (message.Type == ActivityTypes.DeleteUserData)
        {
            // Implement user deletion here
            // If we handle user deletion, return a real message
        }
        else if (message.Type == ActivityTypes.ConversationUpdate)
        {
            if (message.MembersAdded.Any(o => o.Id == message.Recipient.Id))
            {
                ConnectorClient client = new ConnectorClient(new Uri(message.ServiceUrl));

                var reply = message.CreateReply();

                reply.Text = "Welcome to the bot!";

                await client.Conversations.ReplyToActivityAsync(reply);
            }
        }
        else if (message.Type == ActivityTypes.ContactRelationUpdate)
        {
            // Handle add/remove from contact lists
            // Activity.From + Activity.Action represent what happened
        }
        else if (message.Type == ActivityTypes.Typing)
        {
            // Handle knowing tha the user is typing
        }
        else if (message.Type == ActivityTypes.Ping)
        {
        }
    }

    public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        if (activity.Type == ActivityTypes.Event &&
            string.Equals(activity.Name, "buttonClicked", StringComparison.InvariantCultureIgnoreCase))
        {
            ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

            // return our reply to the user
            Activity reply = activity.CreateReply("I see that you just pushed that button");
            await connector.Conversations.ReplyToActivityAsync(reply);
        }

        if (activity.Type == ActivityTypes.Message)
        {
            ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

            // return our reply to the user
            var reply = activity.CreateReply();
            reply.Type = ActivityTypes.Event;
            reply.Name = "changeBackground";
            reply.Value = activity.Text;
            await connector.Conversations.ReplyToActivityAsync(reply);
        }
        else
        {
            HandleSystemMessage(activity);
        }
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
    }
}

}

我已经测试过发送消息活动,效果很好,但是尝试在收到消息后将事件从机器人发送到网页或从网页发送到机器人不会执行任何操作。

该网页说 BotChat 未在两者中定义,但我不确定为什么

var params = BotChat.queryParams(location.search);

and

var botConnection = new BotChat.DirectLine({

在index.html中

我的所有应用程序机密/id 和直接机密均已添加。我觉得问题可能是我如何在index.html中添加我的秘密和网址,但我不确定如何设置它


我重新创建了您的项目并将其发布到 Azure:http://QuickBackChannelEventTest.AzureWebsites.net/index.html

我将代码发布在GitHub。您需要更改 web.config 中的 MicrosoftAppId 和 MicrosoftAppPassword,并添加您的机器人Direct Lineindex.html 页面的 BotChat.DirectLine 创建的秘密:

var botConnection = new BotChat.DirectLine({
            secret: '**DIRECT_LINE_SECRET**',//params['mysecret'],
            //token: params['t'],
            //domain: params['ngroktunneledurl.com/api/messages'],
            webSocket: params['webSocket'] && params['webSocket'] === "true" // defaults to true
    });

我还添加了一个名为 TestStandAlone 的文件夹,其中仅包含 Index.html、botchat.css 和 botchat.js:https://github.com/EricDahlvang/BackChannelEventTest/tree/master/TestStandalone一旦设置了机器人的 Direct Line 密钥,这就会按预期工作。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从嵌入式网络聊天发送事件 的相关文章

  • 使用 itextSharp 5.3.3 对 Pdf 文档进行数字签名和验证

    我正在尝试使用 iTextSharp 5 3 3 在服务器 c 上进行数字签名和验证 pdf 文档 我使用 DigiSign 在线工具 生成了 Pfx 文件 然后使用 Windows 生成证书 cer 文件
  • 如何确定特定时间是否在javascript中给定时间范围之间

    我想检查两个时间 var open time 和 var close time 之间的 var check val var open time 23 30 var close time 06 30 var check val 02 30 i
  • 将 jstring 转换为 QString

    我正在调用一个返回字符串的 Java 函数 QAndroidJniObject obj QAndroidJniObject callStaticObjectMethod
  • ASP Net Core 属性路由和双正斜杠

    正如所指出的here https stackoverflow com a 20524044 3129340 URL 中包含双斜杠是有效的 我有一个使用属性路由的 ASP Net Core 项目 一个名为GroupController用于处理
  • CSS:DIV 中的文本被截断

    我正在致力于集成一个 jQuery 插件 该插件会隐藏页面的某些部分 直到用户单击 喜欢按钮 Facebook 小部件的部分文本被切断 我已经尝试过 CSS 但无法让它完全显示 如果容器的宽度设置正确 还有什么可能导致文本被截断 例子 ht
  • 如何获取 EF 中的实体更改增量?

    我只需要获取已更改字段的列表 数据存储区是 ssce 因此没有可用的触发器 EF 是否支持获取列表或构建通用组件 根据上下文的类型和生成的实体 您可以通过多种不同的方式来完成此操作 如果对象继承自 Entity 或 POCO 您可以使用Ob
  • 使用 JS 从列表中删除最近的 元素的 URL

    所以我有一个网址列表 并且有删除按钮 图像按钮 当点击删除按钮时 按钮旁边的 url 必须从列表中删除 let list const remove document getElementById remove const view docu
  • 根据传单中的属性更改标记颜色

    我的目标是让我的标记根据它们的不同而采用三种不同的颜色rating财产 我看过类似的帖子 其中使用对象来定义颜色 每个标记都有一个rating属性在 1 到 5 之间 我正在考虑使用 else if 语句 例如 if rating lt 3
  • 如何使用 C# 以编程方式识别对方法的引用数量

    我最近继承了需要一些修剪和清理的 C 控制台应用程序 长话短说 该应用程序由一个包含超过 110 000 行代码的类组成 是的 单个类中有超过 110 000 行 当然 该应用程序是我们业务的核心 全天候运行更新动态网站上使用的数据 尽管我
  • 使用 System.Windows.Forms.Timer.Start()/Stop() 与 Enabled = true/false

    假设我们在 Net 应用程序中使用 System Windows Forms Timer 在计时器上使用 Start 和 Stop 方法与使用 Enabled 属性之间有什么有意义的区别吗 例如 如果我们希望在进行某些处理时暂停计时器 我们
  • 自动打开默认电子邮件客户端并预填充内容

    当用户在页面上保存某些内容时 我需要自动打开用户的默认电子邮件客户端 我需要填充电子邮件主题 地址并在电子邮件正文中添加一些内容 实现这一目标的最佳选择是什么 我知道mailto 属性 但用户必须单击此属性 我不确定它是否允许您指定主题和内
  • 通过 C++ 标头在 C++ 和 C# 中使用枚举

    我有一个用 C 编写的服务器 位于命名管道的末端 嗯 提供服务 可以发送到服务器的命令在位于头文件中的枚举中定义 enum e doThing1 e doThing2 e doLastThing 所需枚举的值被放入发送到服务器的消息的第一个
  • 我的 C 程序无法运行,并显示“无法执行二进制文件:Exec 格式错误”

    我刚刚从 C 开始 我试图编译下面的代码并执行它 但出现错误 也在运行sizeBS 或数据堆栈中没有显示任何内容 include
  • CSS3 Marquee / Ticker 动画最后没有空格

    我正在用 2 个项目集合构建字幕 旋转木马效果 循环两者item collection跨越translateX并不难 这里是小提琴 http jsfiddle net k1k3h2p0 但我不喜欢每个循环末尾的空白区域 知道两个集合的宽度可
  • Angular 4 - “等待操作”的正确方法是什么?

    我遇到了一个简单的问题 有一个很奇怪的解决方案setTimeout 0 看看这个简单的代码 Component selector my app template div div
  • 如何使用 javascript 获取 html5 视频的缩略图?

    我找到了根据 URL 获取视频缩略图的 JavaScript 代码 不过 我只在 YouTube 和 Vimeo 上找到了这个 似乎没有人列出如何处理旨在嵌入 html5 视频标签的视频的示例 能做到吗 谢谢 是的 您可以使用视频作为画布的
  • React.js 的状态基于其他状态

    我遇到了 React js 的一些问题 并且在调用 setState 时状态没有立即设置 我不确定是否有更好的方法来解决这个问题 或者这是否真的只是 React 的一个缺点 我有两个状态变量 其中一个基于另一个 原始问题的小提琴 http
  • 即使没有任何转换,也违反了 C 中的严格别名?

    How can i and u i在此代码中打印不同的数字 即使i定义为int i u i 我只能假设我在这里触发了 UB 但我不知道具体是如何触发的 ideone演示 http ideone com Gcv5Xm如果我选择 C 作为语言
  • WPF - 将窗口置于前面

    我有一个 WPF 窗口 我没有关闭它 相反 我执行 Hide 和 Show 它 现在 当我双击记录上网格中的主窗口时 这将触发 Show 窗口 该窗口将始终显示在主窗口后面 我尝试过以下方法 但没有成功 view Show view Act
  • 布尔实现的atomicCAS

    我想弄清楚是否存在错误答案 https stackoverflow com a 57444538 11248508 现已删除 关于Cuda like的实现atomicCAS for bool是 答案中的代码 重新格式化 static inl

随机推荐