如何正确保存用户与机器人的聊天对话?是的,我已经看过关于此的例子,但它没有奏效。我也没有使用builder.Update()
,因为它已经过时了。但是,似乎我已经从机器人到用户(相同的实现)使用它。问题是,在我将聊天从用户登录到僵尸程序之后,僵尸程序停止响应(完全没有响应),它会让我烦恼,因为如果我从僵尸程序登录到用户,代码仍在持续工作。
这里是代码: 用户到机器人
public class UserToBotLoggerService : IPostToBot
{
private readonly IConnectorClient _client;
private readonly IMessageActivity _FromUser;
public UserToBotLoggerService(IMessageActivity fromUser, IConnectorClient client)
{
SetField.NotNull(out _FromUser, nameof(fromUser), fromUser);
SetField.NotNull(out _client, nameof(client), client);
}
public async Task PostAsync(IActivity activity, CancellationToken token)
{
var conversation = "";
var convActivity = "";
conversation = $"From: {activity.From.Name} \n\n To: {activity.Recipient.Name} \n\n Message: {activity.AsMessageActivity()?.Text}";
convActivity = convActivity + conversation;
await _client.Conversations.ReplyToActivityAsync((Activity)activity, token);
}
}
MessageController
public MessagesController()
{
//var builder = new ContainerBuilder();
Conversation.UpdateContainer(
builder =>
{
builder.RegisterType<UserToBotLoggerService>()
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
});
}
这是我使用IActivityLogger
public async Task LogAsync(IActivity activity)
{
var conversation = "";
var convActivity = "";
conversation = $"From: {activity.From.Name} \n\n To: {activity.Recipient.Name} \n\n Message: {activity.AsMessageActivity()?.Text}";
convActivity = convActivity + conversation;
}
Conversation.UpdateContainer(
builder =>
{
builder.RegisterType<BotToUserLoggerService>()
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
}
谢谢! PS:由于需要至少50的声誉,我仍然无法在用户线程中插入评论。所以请原谅我,如果我的问题有重复,我可能已经看过并试过他们的解决方案。
编辑1:我发现了我的错误,似乎我的isTyping活动指标阻挡了IActivityLogger
,因为ReplyToActivityAsync
按Reference使用YourApplicationRootFolder -> app -> build.gradle
!所以我的问题现在是isTyping活动指标。
Edit2:还修复了我的isTyping活动指标。
我会更新我的回答。
答案 0 :(得分:0)
我继承了IActivityLogger
并实现了它的功能,所以我最终得到了这个
public async Task LogAsync(IActivity activity)
{
var conversation = "";
var convActivity = "";
conversation = $"From: {activity.From.Name} \n\n To: {activity.Recipient.Name} \n\n Message: {activity.AsMessageActivity()?.Text}";
convActivity = convActivity + conversation;
}
是的,它与关于如何记录聊天记录的其他问题完全相同,但是,我发现我的问题出在我的connector.Conversations.ReplyToActivityAsync(message);
上,这并不允许我点击IActivityLogger
,ReplyToActivityAsync
所以我所做的,这可能在不久的将来帮助任何人。
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
Activity isTypingReply = activity.CreateReply();
isTypingReply.Type = ActivityTypes.Typing;
HandleSystemMessage(isTypingReply); //I passed the activity type to the HandleSystemMessage and let it handle the typingActivity;
await Conversation.SendAsync(activity, () => new YourDialog());
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
<强> HandleSystemMessage 强>
else if (message.Type == ActivityTypes.Typing)
{
ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));
connector.Conversations.ReplyToActivityAsync(message);
}
HandleSystemMessage
是处理ReplyToActivityAsync
的人IActivityLogger
,我奇迹般地发现它绕过了+---------+-----------+-----------+
| Product | Product-A | Product-B |
+---------+-----------+-----------+
| A | 0 | 1 |
+---------+-----------+-----------+
| B | 2 | 3 |
+---------+-----------+-----------+
限制,其中它就像拥有它自己的世界一样