我结束了@Andrei Dragotoniu 的想法,但我想提供一些实现细节。
短篇小说
将最后(或更多,如果您愿意)发送的消息保存在某个存储中(您可以使用对话 ID 来识别它)。需要时重新发送。
说来话长
要保存最后一条消息,我发现最简单的方法是创建一个 IBotToUser 实现并将其添加到 IBotToUser 链中。
public class StoreLastActivity : IBotToUser
{
private readonly IBotToUser inner;
public StoreLastActivity(IBotToUser inner)
{
SetField.NotNull(out this.inner, nameof(inner), inner);
}
public IMessageActivity MakeMessage()
{
return this.inner.MakeMessage();
}
public async Task PostAsync(IMessageActivity message, CancellationToken cancellationToken = default(CancellationToken))
{
// Save the message here!!!
await this.inner.PostAsync(message, cancellationToken);
}
}
在Global.asax.cs 或某些模块类中注册该类和新链。更新对话容器。
Conversation.UpdateContainer(builder =>
{
// Registers the class the saves the last send message for each conversation.
builder
.RegisterKeyedType<StoreLastActivity, IBotToUser>()
.InstancePerLifetimeScope();
// Adds the class on the IBotToUser chain.
builder
.RegisterAdapterChain<IBotToUser>
(
typeof(AlwaysSendDirect_BotToUser),
typeof(AutoInputHint_BotToUser),
typeof(MapToChannelData_BotToUser),
typeof(StoreLastActivity),
typeof(LogBotToUser)
)
.InstancePerLifetimeScope();
}
这意味着发送给用户的每条消息都会经过StoreLastActivity.PostAsync,因此您可以将其保存在任何地方并使用message.Conversation.Id 作为id。我保存了整个IMessageActivity,因为它并不总是只有文本,它可能包含卡片、按钮等......
现在只需检索IMessageActivity 并在需要时发送。
如果有人有更简单的解决方案,我想听听。
谢谢。