node.js - 微软机器人- Node SDK : How to mention a user in Microsoft Teams

标签 node.js botframework microsoft-teams

尝试使用 NodeJs SDK 在 Microsoft Teams 中提及用户:

我正在保存一个引用。到对话,然后恢复它。恢复后,填充entities与-据我所知-这是一个Mention目的。

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

这段代码实际上是向模拟器发送一个带有预期数据的事件:

{
  "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": "..."
}

但显示的事件只是一条常规消息,根本没有提及。我错过了什么?

===编辑===

另一个尝试是使用 TextEncoder<at>this sample :

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

最佳答案

发布最终答案,以便任何人都可以快速复制粘贴。

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

关于node.js - 微软机器人- Node SDK : How to mention a user in Microsoft Teams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61893493/

相关文章:

node.js - 使用交叉应用程序识别前台应用程序

node.js - Loopback 4 如何验证 requestBody 属性

javascript - 在 db.open() 回调中使用导出 - mongoose/express

c# - 在 FormFlow、BotFramework 中使用 List<T> 时缺少第一个选项

office365 - 为什么我看不到 "Create an outgoing webhook"链接?

microsoft-teams - 从 MS Teams 聊天调用外部 API

javascript - 如何在polka js中导入类似于express.Route()的路线

node.js - 将消息从 Facebook webview 发送回机器人

javascript - Microsoft Bot Framework 请求中的 MissingProperty 错误

node.js - Node.js 中的 TEAMS 机器人 : 'Authorization has been denied for this request' in CreateConversation method