Microsoft Teams botbuilder How to create conversation in another channel

被刻印的时光 ゝ 提交于 2020-04-11 12:19:07

问题


(For reference I have admin_consent for the organization with a auth scope of offline_access User.ReadWrite.All Group.ReadWrite.All AppCatalog.ReadWrite.All for my token that I use to interact with the Teams instance.)

After installing the app via POST /teams/{id}/installedApps it sends an conversationUpdate event that I respond to and save the entire ConversationReference object. It has a lot of stuff I don't need but I'm not sure what is necessary. The immediate response goes to the General channel of the specified Team.

Now I want to use that ConversationReference to post proactive notification messages to a channel that the user has designated outside of Teams. So the user has not interacted with the bot in this channel, but I can list the channel and have its ID.

I can post the message into the General channel utilizing the entire ConversationReference I captured, or message the user directly in chat via ommiting the channel speicifc fields, but I can't seem to get the message sent to a specific channel if I specify it as the 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);
  }
);

回答1:


SUCCESS! Turns out directing the message to another channel requires manipulating the ConversationReference not (as I thought) specifying it in the Activity being sent. I'm showing this by removing the Activity I created in the original question and just sending plain text via 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');
  }
);


来源:https://stackoverflow.com/questions/61132206/microsoft-teams-botbuilder-how-to-create-conversation-in-another-channel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!