0
votes

We have built a microsoft teams app which uses a bot and after a user installs the app, we would like to show a button that says "share to channel" where the user can click and then choose which channel they would like to send a card too.

How can I do this? Is this capable through connectors or webhooks or do I need another approach?

I have connected a connector and added that connector ID to the manifest.json file in the app package but I do not know where to go from there. I have read the following documentation: https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook

When a user installs an app, we can get information for that user but we cannot get information for other teams or channels that user is connected to.

I also tried creating a message extension https://docs.microsoft.com/en-us/microsoftteams/platform/messaging-extensions/how-to/create-messaging-extension

I also looked at Graph API

3
Hey, This is msftsofbot, We are looking into your issue.Trinetra-MSFT

3 Answers

0
votes

Sending a Card to channel using Bot

 public static async Task<ConversationResourceResponse> SendCardToChannel(ITurnContext turnContext, Attachment cardToSend, CancellationToken cancellationToken, IConfiguration configuration)
    {
        var id = configuration["MicrosoftAppId"];
        var pass = configuration["MicrosoftAppPassword"];
        var channelid = configuration["ChannelId"];
        var credentials = new MicrosoftAppCredentials(id, pass);
        var conversationParameters = new ConversationParameters
        {
            Activity = (Activity)MessageFactory.Attachment(cardToSend),
            ChannelData = new TeamsChannelData { Channel = new ChannelInfo(channelid) },
        };

        var tcs = new TaskCompletionSource<ConversationResourceResponse>();
        try
        {
            await ((BotFrameworkAdapter)turnContext.Adapter).CreateConversationAsync(
                    null,       // If we set channel = "msteams", there is an error as preinstalled middleware expects ChannelData to be present
                    turnContext.Activity.ServiceUrl,
                    credentials,
                    conversationParameters,
                    (newTurnContext, newCancellationToken) =>
                    {
                        var activity = newTurnContext.Activity;
                        tcs.SetResult(new ConversationResourceResponse
                        {
                            Id = activity.Conversation.Id,
                            ActivityId = activity.Id,
                            ServiceUrl = activity.ServiceUrl,
                        });
                        return Task.CompletedTask;
                    },
                    cancellationToken);
        }
        catch (Exception e)
        {

            Console.WriteLine(e);
        }

        return await tcs.Task;
    }

Try this piece of code and Let me know if you are able to send the card in channel.

0
votes

Your code, for security reasons, can't send a message to anything in teams (1-1 chat, group chat, or channel), unless you have something that's been installed to that location (a bot, connector, webhook). In this case, you have a bot already, so that's probably the best option to use, and you'd need to send your card as a "proactive message", as per Trinetra's answer. To do that, you need certain values (e.g. conversationid, serviceurl, etc.), and to get those, you -also- need to have your app installed to the final destination.

So, in essence, you'd need to have your bot installed to the destination, but there are a few ways to do this when it comes to a Channel. The easiest is to get the user to install your bot, and to get the information (conversationid, etc.) you need from the conversationUpdate event right away. As an alternative, you can install your app programmatically using the Graph.

0
votes

Just use an incoming webhook. You can send an adaptive card directly to a channel. https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using#send-adaptive-cards-using-an-incoming-webhook

To get channels the user belongs to, use an OData query on Ms Graph API https://docs.microsoft.com/en-us/graph/api/channel-list?view=graph-rest-1.0&tabs=http

But to get a list of channels, you need to get the Teams of the user first. A channel belongs to a team. Then get all postable channels for each team.