Microsoft Teams botbuilder 如何在另一个频道中创建对话

Microsoft Teams botbuilder How to create conversation in another channel

(作为参考,我有 admin_consent 用于我用于与 Teams 实例交互的令牌的身份验证范围为 offline_access User.ReadWrite.All Group.ReadWrite.All AppCatalog.ReadWrite.All 的组织。)

通过 POST /teams/{id}/installedApps 安装应用程序后,它会发送一个 conversationUpdate 事件,我响应并保存整个 ConversationReference 对象。它有很多我不需要的东西,但我不确定什么是必要的。即时响应转到指定团队的 General 频道。

现在我想使用 ConversationReference 到 post 主动通知消息到用户在 Teams 之外指定的频道。所以用户没有与该频道中的机器人互动,但我可以列出该频道并获得其 ID。

我可以利用我捕获的整个 ConversationReference 将消息 post 发送到 General 频道,或者通过省略频道特定字段直接在 chat 中向用户发送消息,但如果我将其指定为 channelId

,我似乎无法将消息发送到特定频道
const msBotAdapter = new BotFrameworkAdapter({
  appId: TEAMS_CLIENT_ID,
  appPassword: TEAMS_CLIENT_SECRET,
});

//Paired down the saved reference to look like this
const conversationReference = {
        "user" : {
            "id" : "9:1rafmaopfopakdfafMzCYlCtg",
            "aadObjectId" : "fffffff-ffff-ffff-ffff-ffffffff"
        },
        "bot" : {
            "id" : "8:sdfsfsdf-dddd-ddddd-aaaaa-vvvvvvv",
            "name" : "Bot Name"
        },
        "conversation" : {
            "isGroup" : true,
            "conversationType" : "channel",
            "tenantId" : "ffffffff-ssssss-ssssss-ss-ssssss"
        },
        "channelId" : "msteams",
        "serviceUrl" : "https://smba.trafficmanager.net/amer/"
    }

const heroCard = CardFactory.heroCard(label, text, undefined, undefined, {
  subtitle: fromUser?.name ? `From: ${fromUser.name}` : undefined,
});

const channelId = {...retrieve channel Id}

const activity = {
  recipient: {
    id: channelId,
    name: 'Test channel 2',
  },
  type: ActivityTypes.Message,
  timestamp: new Date(),
  localTimezone: 'America/New_York',
  callerId: TEAMS_CLIENT_ID,
  serviceUrl: conversationReference.serviceUrl!,
  channelId,
  from: conversationReference.bot as { id: string; name: string },
  valueType: 'text',
  attachments: [heroCard],
};

await msBotAdapter.createConversation(
  conversationReference,
  async turnContext => {
    await turnContext.sendActivity(activity);
  }
);

成功!事实证明,将消息定向到另一个频道需要操纵 ConversationReference 而不是(如我所想)在发送的 Activity 中指定它。我通过删除我在原始问题中创建的 Activity 并通过 await turnContext.sendActivity('Test Message');

发送纯文本来展示这一点
const channelId = //retrieve desitnation channelId I use the graph api `/teams/${teamId}/channels`

const msBotAdapter = new BotFrameworkAdapter({
  appId: TEAMS_CLIENT_ID,
  appPassword: TEAMS_CLIENT_SECRET,
});

//Paired down the  initial conversation reference to bare necessities, the important part is setting the `conversationReference.conversation.id` to the `channelId` that you wish the message to go to.
const conversationReference = {
        "bot" : {
            "id" : "8:sdfsfsdf-dddd-ddddd-aaaaa-vvvvvvv",
        },
        "conversation" : {
             //This is where you dictate where the message goes
             id: channelId
        },
        "serviceUrl" : "https://smba.trafficmanager.net/amer/"
    }

await msBotAdapter.createConversation(
  conversationReference,
  async turnContext => {
    await turnContext.sendActivity('Test Message');
  }
);