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

问题描述 投票:1回答:1

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

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

现在,我想使用该ConversationReference将主动通知消息发布到用户在团队之外指定的频道。因此,用户尚未在此频道中与机器人进行交互,但是我可以列出该频道并获得其ID。

我可以使用捕获的整个General将消息发布到ConversationReference通道,或者通过省略通道专用字段直接在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);
  }
);
node.js botframework microsoft-graph microsoft-teams
1个回答
1
投票

成功!事实证明,将消息定向到另一个通道需要操纵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');
  }
);
© www.soinside.com 2019 - 2024. All rights reserved.