botframework vch 中的调用者响应

Invokeresponse in botframework v4

如何在 C# 的 botframework v4 中 return 一个 InvokeResponse?我需要它来回复撰写扩展 activity 消息。在旧框架中,这是通过在响应中 returning 来自控制器的 composeExtension 对象来完成的。

实现IBot接口时如何做到这一点。

旧框架中有 MS Teams 扩展,新框架版本不可用。

认为你问的是:
this sample. In your OnTurnAsync you need to catch the Invoke activity 中有一个处理调用响应的示例,并像示例中那样对 activity 执行您需要执行的任何操作。

我不确定您使用的是哪个 SDK,因为您没有将其包含在您的问题中,但是 C# 中的一个简单示例(Node 类似)可能如下所示:

public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
    if (turnContext.Activity.Type == ActivityTypes.Message)
    {
        //do stuff
    }
    if (turnContext.Activity.Type == ActivityTypes.Invoke)
    {
        //do stuff
    }
}

来自此处的 BF SDK v4 代码: https://github.com/Microsoft/botbuilder-dotnet/blob/4bb6b8d5faa4b252379ac331d6f5140ea27c177b/libraries/Microsoft.Bot.Builder/BotFrameworkAdapter.cs#L216 https://github.com/Microsoft/botbuilder-dotnet/blob/4bb6b8d5faa4b252379ac331d6f5140ea27c177b/libraries/Microsoft.Bot.Builder/BotFrameworkAdapter.cs#L285

你所做的是使用 ITurnContext 到 "reply" 和 ActivityTypesEx.InvokeResponse 类型的假 activity,将 Activity.Value 设置为 InvokeResponse带有您想要的状态代码和有效负载的对象。

要响应调用 activity,您必须像下面的示例一样在 turnContext.TurnState 中设置 "BotFrameworkAdapter.InvokeResponse"

public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
    if (turnContext.Activity.Type == ActivityTypes.Message)
    {
        // do stuff
    }
    if (turnContext.Activity.Type == ActivityTypes.Invoke)
    {
        // do stuff
        var invokeResponse = new InvokeResponse()
        {
            Body = response,
            Status = (int)HttpStatusCode.OK
        };
        var activity = new Activity();
        activity.Value = invokeResponse;
        // set the response
        turnCoontext.TurnState.Add<InvokeResponse>(InvokeReponseKey, activity);
    }
}