BotFramework-使用C#重复最后一条消息 [英] BotFramework - Repeat last message using c#

查看:98
本文介绍了BotFramework-使用C#重复最后一条消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft Bot Framework和C#构建机器人.我正在使用对话框"控制流程,并使用快速答复作为按钮,当发送新消息时,它们会消失.

I'm building a bot using Microsoft Bot Framework using C#. I'm controlling my flow using Dialogs, and using quick replies as buttons, they dissapear when a new message is sent.

我还创建了一个全局命令来帮助用户.当用户键入帮助"时,机器人将提供一些有关其功能的信息.

I also created a global command to help the user. When the user types "help" the bot will give some information about what it can do.

问题在于,如果用户寻求帮助,快速回复将消失,并且用户可能不知道如何继续该对话框.我想知道在发送帮助后是否有任何方法可以重复上次发送的消息(该帮助不是对话框,可计分发送消息).

The problem is that if the user asks for help the quick replies will dissapear and the user might not know how to continue the dialog. I was wondering if there is any way to repeat the last sent message after the help is sent (the help is not a Dialog, the Scorable sends the message).

谢谢

推荐答案

我结束了@Andrei Dragotoniu的想法,但是我想提供一些实现细节.

I ended following @Andrei Dragotoniu idea, but I'd like to provide some implementation details.

将最近(或更多)发送的消息保存在某个存储区中(您可以使用对话ID进行标识).在需要时重新发送.

Save the last (or more if you want) sent message in some storage (you can use the conversation id to identify it). Resend it when needed.

要保存最后一条消息,我发现的最简单的方法是创建一个IBotToUser实现并将其添加到IBotToUser链中.

To save the last message the simplest way I found was to create a IBotToUser implementation and add it to the IBotToUser chain.

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或某些Module类中注册该类和新链.更新对话"容器.

In Global.asax.cs or in some Module class register the class and the new chain. Update the Conversation container.

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,因为它并不总是文本,它可能包含卡片,按钮等...

This means that every message sent to the user will pass by StoreLastActivity.PostAsync, so you can save it anywhere and use message.Conversation.Id as an id. I saved the entire IMessageActivity as it not always just text, it may contains cards, buttons etc...

现在只需检索IMessageActivity并在需要时将其发送即可.

Now just retrieve the IMessageActivity and send it whenever you need.

如果有人有更简单的解决方案,我想听听.

If anyone got a simpler solution I'd like to hear it.

谢谢.

这篇关于BotFramework-使用C#重复最后一条消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆