【问题标题】:How to process an activity recieved from a skill in bot framework v4?如何处理从 bot 框架 v4 中的技能收到的活动?
【发布时间】:2020-12-16 22:52:47
【问题描述】:

我正在使用 Microsoft Bot Framework v4 for C# 开发技能消费者机器人,我的技能是在 Bot Framework Composer 中开发的对话机器人,我的问题是:是否可以在显示之前处理从技能机器人收到的活动给用户?这样做的原因是因为我想发送一个 ID 代码(识别数据库中的真实消息)作为技能的响应,并使用此 ID 获取在连接到我的技能使用者的数据库中注册的真实答案机器人。然后向用户展示真实的答案。我在官方 Bot Framework 文档中看到,技能处理程序用于接收技能 (https://docs.microsoft.com/en-us/azure/bot-service/skill-implement-consumer?view=azure-bot-service-4.0&tabs=cs#skill-client-and-skill-handler) 的活动。

官方文档中的技能处理描述:

我检查了技能处理程序类,我看到了以下方法:

protected override Task OnDeleteActivityAsync(ClaimsIdentity claimsIdentity, string conversationId, string activityId, CancellationToken cancellationToken = default);
protected override Task<ResourceResponse> OnReplyToActivityAsync(ClaimsIdentity claimsIdentity, string conversationId, string activityId, Activity activity, CancellationToken cancellationToken = default);
protected override Task<ResourceResponse> OnSendToConversationAsync(ClaimsIdentity claimsIdentity, string conversationId, Activity activity, CancellationToken cancellationToken = default);
protected override Task<ResourceResponse> OnUpdateActivityAsync(ClaimsIdentity claimsIdentity, string conversationId, string activityId, Activity activity, CancellationToken cancellationToken = default);

这种方法是否可以帮助获得我正在寻找的东西?如果是,我应该如何使用它,或者我该怎么办?

我希望你能帮助我。

PD:对不起我的英语。我来自秘鲁:D

【问题讨论】:

  • 技能消费者是在 Composer 中构建的,还是只是技能?
  • @Kyle Delaney 只是技能。技能消费者是用 C# 构建的
  • 这应该不会太难。您是否尝试过实现自己的技能处理程序并覆盖 OnSendToConversationAsyncOnReplyToActivityAsync
  • 你还在做这个吗?

标签: c# azure-bot-service botframework


【解决方案1】:

我刚刚找到了使用中间件的解决方案。我创建了一个类 GetSkillResponseMiddleware.cs 来编写所有逻辑。

public class GetSkillResponseMiddleware : IMiddleware
{
    public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default)
    {
        // Here I can add logic to process messages sent by user. 
        // Example: Register user's message data in a telemetry app.

        // Register outgoing handler.
        turnContext.OnSendActivities(OutgoingHandler);

        // Continue processing messages.
        await next(cancellationToken);
    }

    private async Task<ResourceResponse[]> OutgoingHandler(ITurnContext turnContext, List<Activity> activities, Func<Task<ResourceResponse[]>> next)
    {
        foreach (var activity in activities)
        {
            if (activity.Type == "message")
            {
               // For example I want to concat the phrase "Bot said: " to all messages sent by the bot. In my real case I edit the answer sent by the skill bot.
               activity.Text = $"Bot said: {activity.Text}";
            }
        }

        return await next();
    }
}

在上面的代码中,方法 OutgoingHandler 处理机器人发送的所有活动,我使用条件只处理消息活动。在我的架构中,技能机器人只向用户发送消息,而不是消费者机器人,所以对我来说,没有必要添加另一个条件来仅处理技能机器人发送的消息(但添加它是个好主意)。最后我必须在机器人适配器中注册中间件。所以在 AdapterWithErrorHandler.cs 类中,我必须在适配器末尾添加以下行:Use(new GetSkillResponseMiddleware());

public AdapterWithErrorHandler(IConfiguration configuration, ILogger<BotFrameworkHttpAdapter> logger)
        : base(configuration, logger)
    {
        OnTurnError = async (turnContext, exception) =>
        {
            // Log any leaked exception from the application.
            // NOTE: In production environment, you should consider logging this to
            // Azure Application Insights. Visit https://aka.ms/bottelemetry to see how
            // to add telemetry capture to your bot.
            logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}");

            // Send a message to the user
            await turnContext.SendActivityAsync("The bot encountered an error or bug.");
            await turnContext.SendActivityAsync("To continue to run this bot, please fix the bot source code.");

            // Send a trace activity, which will be displayed in the Bot Framework Emulator
            await turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError");
        };

        Use(new GetSkillResponseMiddleware());
    }

希望我的回答对您有所帮助,我很高兴看到一些改进此代码的建议。问候。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 2020-03-21
    • 1970-01-01
    • 2021-03-08
    相关资源
    最近更新 更多