Peter Andreoli
Peter Andreoli

Reputation: 53

Proactively send message to a Team chat in Microsoft Teams using Microsoft Bot Framework V4

I am trying to proactively send an adhoc message from my Azure hosted bot to a Microsoft Teams team chat. I have been successful in sending a message to a 1 on 1 chat a user has with a bot.

I have seen this documentation detailing how to do so, however I believe this is Bot Framework version 3. It uses the nugget package Microsoft.Bot.Connector.Teams.Models, which is no longer supported in version 4. I have not been able to find documentation for doing this.

To be a little more descriptive:

I have a bot that recieves POST requests from my web app with alarm data. When the bot receives one of these POST requests it forwards a message to a team chat. This this message will be sent outside any context. My code for forwarding to a 1 on 1 chat can be found here.

/// <summary>
    /// Forwards a message to a personal chat.
    /// The details of who to send the message to is included in the <paramref name="forwardContext"/>.
    /// </summary>
    /// <param name="forwardContext">JSON of the recipient details.</param>
    /// <param name="messageToSend">Text message to send to chat.</param>
    /// <returns>A <see cref="Task"/> representing the asynchronous messsage forward.</returns>
    private async Task ForwardMessageToPersonalChatAsync(JToken forwardContext, string messageToSend)
    {
        // Collect data from JSON input
        var restCmd = forwardContext;
        var toId = (string)restCmd["toId"];
        var toName = (string)restCmd["toName"];
        var fromId = (string)restCmd["fromId"];
        var fromName = (string)restCmd["fromName"];
        var channelId = (string)restCmd["channel"];
        var serviceURL = (string)restCmd["serviceURL"];
        var conversationId = (string)restCmd["conversation"];
        var tenant = (string)restCmd["tenant"];
        var cred_str = $@"toId: {toId}
        toName: {toName}
        fromId: {fromId}
        fromName: {fromName}
        channelId: {channelId}
        serviceURL: {serviceURL}
        conversationId: {conversationId}";
        this.logger.LogInformation(cred_str);
        this.logger.LogInformation($"Forwarding the following message to {toName}: {messageToSend}");

        var uri = new System.Uri(serviceURL);

        Dictionary<string, string> botCreds = this.GetBotCredentials();
        ConnectorClient connector = new ConnectorClient(uri, botCreds["App ID"], botCreds["App Password"]);
        var activity = new Activity
        {
            Type = ActivityTypes.Message,
            From = new ChannelAccount(fromId, fromName),
            Recipient = new ChannelAccount(toId, toName),
            Conversation = new ConversationAccount(false, "personal", conversationId),
            Text = messageToSend,
        };

        try
        {
            MicrosoftAppCredentials.TrustServiceUrl(serviceURL);
            await connector.Conversations.SendToConversationAsync(conversationId, activity);
        }
        catch (System.Exception ex)
        {
            this.logger.LogError(ex.ToString());
        }
    }

How can I send a proactive message to a Team chat?

Thanks for any help.

Upvotes: 1

Views: 2332

Answers (1)

Bill Bliss - MSFT
Bill Bliss - MSFT

Reputation: 3581

Teams does not yet officially support Bot Framework 4.0.

Upvotes: 1

Related Questions