Adrian Ber
Adrian Ber

Reputation: 21380

Invokeresponse in botframework v4

How can I return an InvokeResponse in botframework v4 for C#? I need this to respond to compose extension activity messages. In the old framework this was done by returning in the response a composeExtension object from the controller.

How can this be done when implementing the IBot interface.

In the old framework there were MS Teams extensions, not available for the new framework version.

Upvotes: 0

Views: 1344

Answers (3)

Adrian Ber
Adrian Ber

Reputation: 21380

To respond to an invoke activity you have to set the "BotFrameworkAdapter.InvokeResponse" in turnContext.TurnState like in the below example

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);
    }
}

Upvotes: 1

Adrian Solis
Adrian Solis

Reputation: 941

From the BF SDK v4 code here: 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

what you do is use ITurnContext to "reply" with a fake activity of type ActivityTypesEx.InvokeResponse, setting Activity.Value to an InvokeResponse object with your desired status code and payload.

Upvotes: 0

D4RKCIDE
D4RKCIDE

Reputation: 3426

For what I think you are asking:
There is an example of handling an invoke response in this sample. In your OnTurnAsync you need to catch the Invoke activity and do whatever it is you need to do with the activity like in the sample.

I'm unsure which SDK you are using as you did not include it in your question but, a simple example in C# (Node would be similar) might look like this:

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
    }
}

Upvotes: 0

Related Questions