简体   繁体   English

Microsoft Bot - 节点 SDK:如何在 Microsoft Teams 中提及用户

[英]Microsoft Bot - Node SDK: How to mention a user in Microsoft Teams

Trying to mention a user in Microsoft Teams using the NodeJs SDK:尝试使用 NodeJs SDK 在 Microsoft Teams 中提及用户:

I'm saving a ref.我正在保存一个参考。 to the conversation and then restoring it.到对话,然后恢复它。 When restored, filling entities with - what I understand - it's a Mention object.恢复后,填充entities - 据我所知 - 这是Mention object。

const message = "Konnichi wa";
const conversation = await .... // restored from db
await adapter.continueConversation(conversation, async (context) => {
  const topLevelMessage = MessageFactory.text(message);
  topLevelMessage.entities = [
    {
      type: "Mention", // have tried with "mention" as well
      mentioned: context.activity.from,
      text: `@${context.activity.from.name}`,
    },
  ];
  await context.sendActivity(topLevelMessage);
});

this code is actually sending to the emulator an activity with the expected data:此代码实际上是向模拟器发送带有预期数据的活动:

{
  "type": "message",
  "serviceUrl": "https://b9372952.ngrok.io",
  "channelId": "emulator",
  "from": {
    "id": "0c00d490-99db-11ea-a447-1de8c692dbf4",
    "name": "Bot",
    "role": "bot"
  },
  "conversation": {
    "id": "0e086460-99db-11ea-a447-1de8c692dbf4|livechat"
  },
  "recipient": {
    "id": "4c2e3fee-fb06-43a1-b9bb-279cc67ed6e6",
    "role": "user"
  },
  "text": "Konnichi wa",
  "inputHint": "...",
  "entities": [
    {
      "type": "Mention",
      "text": "@User",
      "mentioned": {
        "id": "4c2e3fee-fb06-43a1-b9bb-279cc67ed6e6",
        "name": "User",
        "role": "user"
      }
    }
  ],
  "replyToId": "...",
  "id": "...",
  "localTimestamp": "...",
  "timestamp": "...",
  "locale": "..."
}

But the shown activity is just a regular message with no mention at all.但是显示的活动只是一条常规消息,根本没有提及。 What am I missing?我错过了什么?

=== EDIT === === 编辑 ===

Another try has been using TextEncoder and <at> as this sample :另一种尝试是使用TextEncoder<at>作为这个示例

const encodedUserName = new TextEncoder().encode(context.activity.from.name);
const mention = {
    type: "mention",
    mentioned: context.activity.from,
    text: `<at>${encodedUserName}</at>`,
};

const topLevelMessage = MessageFactory.text(`${mention.text}: ${message}`);
topLevelMessage.entities = [mention];
await context.sendActivity(topLevelMessage);

Posting the final answer so anyone can copy-paste it quickly.发布最终答案,以便任何人都可以快速复制粘贴。

await adapter.continueConversation(conversation, async (context) => {
    const encodedUserName = new TextEncoder().encode(context.activity.from.name);
    const mention = {
        type: "mention",
        mentioned: context.activity.from,
        text: `<at>${encodedUserName}</at>`,
    };
    const topLevelMessage = MessageFactory.text(`${message} ${mention.text}`);
    topLevelMessage.entities = [mention];
    await context.sendActivity(topLevelMessage);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM