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












0














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.










share|improve this question



























    0














    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.










    share|improve this question

























      0












      0








      0







      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.










      share|improve this question













      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.







      c# botframework microsoft-teams






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 21:40









      Peter

      84




      84
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Teams does not yet officially support Bot Framework 4.0.






          share|improve this answer





















          • Thanks for the response. I found a statement here indicating that Teams does support at least private messaging and channel messaging a bot. Specifically "Currently, Microsoft Teams support bots in private chats and channels within a team". Is there anywhere that says specifically that a bot can't message a channel in V4 yet?
            – Peter
            Dec 3 at 15:24











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383028%2fproactively-send-message-to-a-team-chat-in-microsoft-teams-using-microsoft-bot-f%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Teams does not yet officially support Bot Framework 4.0.






          share|improve this answer





















          • Thanks for the response. I found a statement here indicating that Teams does support at least private messaging and channel messaging a bot. Specifically "Currently, Microsoft Teams support bots in private chats and channels within a team". Is there anywhere that says specifically that a bot can't message a channel in V4 yet?
            – Peter
            Dec 3 at 15:24
















          1














          Teams does not yet officially support Bot Framework 4.0.






          share|improve this answer





















          • Thanks for the response. I found a statement here indicating that Teams does support at least private messaging and channel messaging a bot. Specifically "Currently, Microsoft Teams support bots in private chats and channels within a team". Is there anywhere that says specifically that a bot can't message a channel in V4 yet?
            – Peter
            Dec 3 at 15:24














          1












          1








          1






          Teams does not yet officially support Bot Framework 4.0.






          share|improve this answer












          Teams does not yet officially support Bot Framework 4.0.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 15:25









          Bill Bliss - MSFT

          2,1261612




          2,1261612












          • Thanks for the response. I found a statement here indicating that Teams does support at least private messaging and channel messaging a bot. Specifically "Currently, Microsoft Teams support bots in private chats and channels within a team". Is there anywhere that says specifically that a bot can't message a channel in V4 yet?
            – Peter
            Dec 3 at 15:24


















          • Thanks for the response. I found a statement here indicating that Teams does support at least private messaging and channel messaging a bot. Specifically "Currently, Microsoft Teams support bots in private chats and channels within a team". Is there anywhere that says specifically that a bot can't message a channel in V4 yet?
            – Peter
            Dec 3 at 15:24
















          Thanks for the response. I found a statement here indicating that Teams does support at least private messaging and channel messaging a bot. Specifically "Currently, Microsoft Teams support bots in private chats and channels within a team". Is there anywhere that says specifically that a bot can't message a channel in V4 yet?
          – Peter
          Dec 3 at 15:24




          Thanks for the response. I found a statement here indicating that Teams does support at least private messaging and channel messaging a bot. Specifically "Currently, Microsoft Teams support bots in private chats and channels within a team". Is there anywhere that says specifically that a bot can't message a channel in V4 yet?
          – Peter
          Dec 3 at 15:24


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383028%2fproactively-send-message-to-a-team-chat-in-microsoft-teams-using-microsoft-bot-f%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?