gpt4 book ai didi

c# - 加载 Webchat 控件后立即从机器人发送问候/欢迎消息

转载 作者:行者123 更新时间:2023-11-30 20:16:05 28 4
gpt4 key购买 nike

我正在使用 Microsoft 的 C# Bot 框架开发一个机器人。在他/她发送任何内容之前,我试图向用户发送一条欢迎消息作为介绍。

经过研究,我使用 HandleSystemMessage 函数并在 ConversationUpdate 的情况下发送消息,如下所示:

if (activity.Type == ActivityTypes.ConversationUpdate) 
{
IConversationUpdateActivity update = activity;
if (update.MembersAdded.Any())
{
foreach (var newMember in update.MembersAdded)
{
if (newMember.Id != activity.Recipient.Id)
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

Activity reply = activity.CreateReply();
reply.Text("Hello, how can I help you?");
await connector.Conversations.ReplyToActivityAsync((Activity)bubble);
}
}
}
}

我用这个方法遇到的问题:

  • 在模拟器中,当您点击上面的刷新按钮时,或者当用户开始输入时,如果它已经闲置了一段时间,就会出现欢迎消息。这是我正在寻找的行为,它正在按预期工作。
  • 现在使用 Bot Framework Web Chat组件,当用户发起对话时发送此消息,即当用户键入内容并将其发送给机器人时。这不是我想要的,而是我希望在加载网络聊天控件后立即从机器人中显示消息,因为该消息将包含一些有关如何使用机器人的说明。

我认为我的问题可以使用另一个 ActivityType 或一些 Javascript 的“hacky”方式来解决,但直到现在我都找不到解决方案。

最佳答案

对于网络聊天组件,您可以使用反向 channel 功能向您的机器人发送隐藏消息,以启动问候语。

这里是网络聊天端的一个实现示例:

<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
</head>
<body>
<div id="bot" />
<script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
<script>
// Get parameters from query
const params = BotChat.queryParams(location.search);
// Language definition
var chatLocale = params['locale'] || window.navigator.language;

// Connection settings
const botConnectionSettings = new BotChat.DirectLine({
domain: params['domain'],
secret: 'YOUR_SECRET',
webSocket: params['webSocket'] && params['webSocket'] === 'true'
});

// Webchat init
BotChat.App({
botConnection: botConnectionSettings,
user: { id: 'userid' },
bot: { id: 'botid' },
locale: chatLocale,
resize: 'detect'
}, document.getElementById('bot'));

// Send hidden message to do what you want
botConnectionSettings.postActivity({
type: 'event',
from: { id: 'userid' },
locale: chatLocale,
name: 'myCustomEvent',
value: 'test'
}).subscribe(function (id) { console.log('event sent'); });
</script>
</body>
</html>

在您的机器人端,您将在您的 Message Controlelr 上获得此事件:

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
// DEMO PURPOSE: echo all incoming activities
Activity reply = activity.CreateReply(Newtonsoft.Json.JsonConvert.SerializeObject(activity, Newtonsoft.Json.Formatting.None));

var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
connector.Conversations.SendToConversation(reply);

// Process each activity
if (activity.Type == ActivityTypes.Message)
{
await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
}
// Webchat: getting an "event" activity for our js code
else if (activity.Type == ActivityTypes.Event && activity.ChannelId == "webchat")
{
var receivedEvent = activity.AsEventActivity();

if ("myCustomEvent".Equals(receivedEvent.Name, StringComparison.InvariantCultureIgnoreCase))
{
// DO YOUR GREETINGS FROM HERE
}
}
// Sample for Skype: in ContactRelationUpdate event
else if (activity.Type == ActivityTypes.ContactRelationUpdate && activity.ChannelId == "skype")
{
// DO YOUR GREETINGS FROM HERE
}
// Sample for emulator, to debug locales
else if (activity.Type == ActivityTypes.ConversationUpdate && activity.ChannelId == "emulator")
{
foreach (var userAdded in activity.MembersAdded)
{
if (userAdded.Id == activity.From.Id)
{
// DO YOUR GREETINGS FROM HERE
}
}
}

var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}

我使用此功能制作了一个工作演示来发送用户语言环境,它是 here在 Github 上

关于c# - 加载 Webchat 控件后立即从机器人发送问候/欢迎消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50982168/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com