Search code examples
botframeworkmicrosoft-teams

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.


Solution

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