我正在使用C#中的Microsoft 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);
}
}
}
}
此方法面临的问题:
我认为可以使用另一种ActivityType
或某种Java语言“ hacky”方式解决我的问题,但直到现在我都找不到解决方案。
答案 0 :(得分:4)
对于网络聊天组件,您可以使用反向通道功能向机器人发送隐藏消息,以启动问候语。
Heere是网聊方面的实现示例:
<!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;
}
我使用此功能进行了有效的演示,以发送用户语言环境,它是Github上的here
答案 1 :(得分:2)
这是网络聊天中的一个已知问题。
使用WebChat或Directline,在创建对话时发送bot的ConversationUpdate,并在用户首次发送消息时发送该用户的ConversationUpdate。 [1]
一种解决方法是从网上聊天向机器人发送消息。
<!DOCTYPE html>
<script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
<script>
var user = {
id: 'user-id',
name: 'user name'
};
var botConnection = new BotChat.DirectLine({
token: '[DirectLineSecretHere]',
user: user
});
BotChat.App({
user: user,
botConnection: botConnection,
bot: { id: 'bot-id', name: 'bot name' },
resize: 'detect'
}, document.getElementById("bot"));
botConnection
.postActivity({
from: user,
name: 'requestWelcomeDialog',
type: 'event',
value: ''
})
.subscribe(function (id) {
console.log('"trigger requestWelcomeDialog" sent');
});
</script>
[2]
我没有更改机器人代码中的任何内容来实现这一目标。