MicrosoftDocs / msteams-docs

Source for the Microsoft Teams developer platform documentation.
https://aka.ms/teamsdev
Creative Commons Attribution 4.0 International
281 stars 502 forks source link

Section & code sample for `Respond to botMessagePreview send` incomplete/incorrect #9671

Closed andrewconnell closed 6 months ago

andrewconnell commented 11 months ago

The section Respond to botMessagePreview send states (emphasis added with my numbered callouts):

Your web service 1️⃣ must create and send a proactive message with the Adaptive Card to the conversation, and also 2️⃣ reply to the invoke.

WRT 1️⃣ unable to post a message

The docs say this should be a proactive message to the channel... agreed... but the code provided doesn't do that. If the code in the listing is used, you get an error that the bot isn't a member in the conversation. Proactive messages need to use an adapter to post a message to a channel.

WRT 2️⃣ included code snippet invalid

The code listing in the doc below this statement doesn't address 2️⃣. Furthermore, the code listing came from a sample linked to in at the end of the doc that also contradicts the doc (statement 2️⃣).

Why? 👉 The code listing (and referenced sample) don't respond. The method TeamsActivityHandler. handleTeamsMessagingExtensionBotMessagePreviewSend() doesn't respond... it acts like it has a void return. However the SDK clearly states it should return a Promise<MessagingExtensionActionRequest> ref.

Failure to respond triggers an error.

I've tried multiple response combinations, but every single one fails... per guidelines on the botbuilder-js repo, I posted a question to SO.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

sayali-MSFT commented 11 months ago

@andrewconnell - Thanks for reporting your issue. We will check this at our end and update the document accordingly.

sayali-MSFT commented 11 months ago

@andrewconnell -We are checking this with the sample team, we will let you know once we have any update.

ChetanSharma-msft commented 9 months ago

Hello @andrewconnell - Sorry for delay in response. We have tried to setup and debug this sample but debugger is not hitting for "handleTeamsMessagingExtensionBotMessagePreviewSend" method, so we are looking into it.

handleTeamsMessagingExtensionBotMessagePreviewSend

We will let you know the further updates once we have any.

We have also raised one issue here: https://github.com/microsoft/botbuilder-js/issues/4581

andrewconnell commented 7 months ago

@ChetanSharma-msft any update? I haven't seen any activity on that file in the repo...

I see the reply comment from the the botbuilder issue... any thoughts on their response?

andrewconnell commented 7 months ago

Following up... WRT the root of my question, what's the proper way to do this, where this = "respond to botMessagePreview send"?

ChetanSharma-msft commented 7 months ago

Hello @andrewconnell - Sorry for delay in response. We are looking into this issue and let you know the updates, if any.

andrewconnell commented 7 months ago

Regardless of the sample, it's a simple question: How are you supposed to respond to a botMessagePreview send event with TypeScript using the Teams JS SDK?

This sample is the only reference I've found which is why I referenced it in my issue.

ChetanSharma-msft commented 7 months ago

Hello @andrewconnell - We will investigate/debug the issue as per the inputs from engineering person from bot builder repo and let you know the updates.

ChetanSharma-msft commented 7 months ago

Hello @andrewconnell - Thanks for your patience. I have debugged the code and please find below analysis: (I have executed NodeJS sample)

Below sample is working fine for me: https://github.com/OfficeDev/Microsoft-Teams-Samples/blob/main/samples/msgext-action-preview/nodejs/bots/teamsMessagingExtensionsActionPreviewBot.js#L50-L74

I am able to prepare the card and able to send it in Teams channel: MicrosoftTeams-image (29)

I am also act on the posted card and able to submit the data: MicrosoftTeams-image (30)

I can see that the return type of method is Promise<void> only: (method) TeamsMessagingExtensionsActionPreviewBot.handleTeamsMessagingExtensionBotMessagePreviewSend(context: any, action: any): Promise MicrosoftTeams-image (31)

Used "botbuilder": "^4.18.0"

but as per bot builder repro return type is: botbuilder-js/libraries/botbuilder/src/teamsActivityHandler.ts at main · microsoft/botbuilder-js (github.com)

Promise<MessagingExtensionActionRequest>

MicrosoftTeams-image (32)

Action: we will check this with engineering team and let you know.

In C# sample also, we can see the return type as Task<MessagingExtensionActionResponse> OnTeamsMessagingExtensionBotMessagePreviewSendAsync

That's why we can return like below: return new MessagingExtensionActionResponse(); Or return null;

Also, regarding the below documentation: https://learn.microsoft.com/en-us/microsoftteams/platform/messaging-extensions/how-to/action-commands/respond-to-task-module-submit?tabs=dotnet%2Cdotnet-1#respond-to-botmessagepreview-send image

We will check with engineering team and let you know.

Regarding your below query: How are you supposed to respond to a botMessagePreview send event with TypeScript using the Teams JS SDK? => I will try to create a new TypeScript sample and let you know.

ChetanSharma-msft commented 7 months ago

Regardless of the sample, it's a simple question: How are you supposed to respond to a botMessagePreview send event with TypeScript using the Teams JS SDK?

This sample is the only reference I've found which is why I referenced it in my issue.

[ ![68747470733a2f2f6d656469612e67697068792e636f6d2f6d656469612f74584c34464850536e564a30412f67697068792e676966]

Hello @andrewconnell - I have created a new TypeScript sample and able to return the response from handleTeamsMessagingExtensionBotMessagePreviewSend method.

Simply used below code:

public async handleTeamsMessagingExtensionBotMessagePreviewSend(context, action): Promise<any> {
  // The data has been returned to the bot in the action structure.
  const submitData = AdaptiveCardHelper.toSubmitExampleData(action);

  // This is a send so we are done and we will create the adaptive card editor.
  const adaptiveCard = AdaptiveCardHelper.createAdaptiveCardAttachment(submitData);
  var responseActivity = { type: 'message', attachments: [adaptiveCard] };
  if (submitData.UserAttributionSelect === 'true') {
      responseActivity = {
          type: 'message',
          attachments: [adaptiveCard],
      };
  }

  await context.sendActivity(responseActivity);
}

Note: We don't need to send proactive messages via using Adapter. We can use this: await context.sendActivity(responseActivity);

We will update the documentation accordingly for proactive messages and other Web service related contents.

Please let me know if you still have any query.

ChetanSharma-msft commented 7 months ago

Raised the doc bug as well: https://github.com/MicrosoftDocs/msteams-docs/issues/10360

andrewconnell commented 7 months ago

@ChetanSharma-msft said:

Please let me know if you still have any query.

I'll try to repeat my process to see I still see the issue., but unless the SDK changed, I'm not sure I see a difference in what would get around the issue I was running into. When I tried it, the TypeScript compiler was failing due to no response by the method. In the snippet you shared, it doesn't return anything, but (as my OP mentioned) the signature in the docs explicitly state otherwise (see this).

Shouldn't the docs AND underlying source say that the return type could be Promise<MessagingExtensionActionResponse> (as it says now) and undefined?

The way it's (docs & source) are written, it implies you must return a Promise of type MessagingExtensionActionResponse, yet the snippet in your comment doesn't... it changes it to an any.

ChetanSharma-msft commented 7 months ago

Hello @andrewconnell - Thanks for the patience. I have investigated this issue further and after changing the response type to Promise<MessagingExtensionActionResponse> And returning the response like below is also working: return { composeExtension: { type: "result", attachmentLayout: "list", attachments: [adaptiveCard], }, };

 public async handleTeamsMessagingExtensionBotMessagePreviewSend(context, action): Promise<MessagingExtensionActionResponse> {
  // The data has been returned to the bot in the action structure.
  const submitData = AdaptiveCardHelper.toSubmitExampleData(action);

  // This is a send so we are done and we will create the adaptive card editor.
  const adaptiveCard = AdaptiveCardHelper.createAdaptiveCardAttachment(submitData);

  var responseActivity = { type: 'message', attachments: [adaptiveCard], channelData: context.channelData };
  if (submitData.UserAttributionSelect === ' true') {   

      responseActivity = {
          type: 'message',
          attachments: [adaptiveCard],
          channelData: {
              onBehalfOf: [
                  {
                      itemid: 0,
                      mentionType: 'person',
                      mri: context.activity.from.id,
                      displayName: context.activity.from.name
                  }
              ]
          }
      };
  }

  return {
    composeExtension: {
      type: "result",
      attachmentLayout: "list",
      attachments: [adaptiveCard],
    },
  };

  // await context.sendActivity(responseActivity);
}

Could you please try and let us know if you still face the issue.

andrewconnell commented 7 months ago

Let me get this straight... I post this message on October 13, 2023, over 120 days ago, it took 58 days to get a confirmation... clearly I've had to come up with another solution in the meantime.

You finally prove a meaningful response & while I'd like to test it, I have but just a few days before you close the issue? 😡

Seems pretty crappy. I'll test it when I can but I'm swamped with other commitments... will either reopen this issue or create a new one.

microsoft-github-policy-service[bot] commented 6 months ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 3 days. It will be closed if no further activity occurs within 3 days of this comment.