【问题标题】:Microsoft Teams botbuilder How to create conversation in another channelMicrosoft Teams botbuilder 如何在另一个频道中创建对话
【发布时间】:2020-04-11 00:50:01
【问题描述】:

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

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

现在我想使用 ConversationReference 将主动通知消息发布到用户在 Teams 之外指定的频道。所以用户没有在这个频道中与机器人交互,但我可以列出频道并有它的 ID。

我可以使用我捕获的整个ConversationReference 将消息发布到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);
  }
);

【问题讨论】:

  • 我自己也遇到过类似的问题。这有帮助吗?我还没有尝试过解决方案...stackoverflow.com/questions/55007896/…
  • @billoverton 我终于想通了,谢谢你的链接,虽然我找不到我的解决方案。
  • (解决方案贴在下面)

标签: node.js botframework microsoft-graph-api microsoft-teams


【解决方案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');
  }
);

【讨论】:

    猜你喜欢
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    相关资源
    最近更新 更多