4

I have a Bot created with v4 of the Microsoft Bot Framework. I can successfully use this bot in the "Test in Web Chat" portion in the Azure Portal. I can also successfully use this bot in an app that I've created in Microsoft Teams. I now want to send a notification from the "Test in Web Chat" piece to a specific user in Teams. For example, in the "Test in Web Chat" piece, I'd like to enter

Hello someuser@mytenant.com

When this is sent via the "Test in Web Chat" piece, I'd like to show "Hello" in Microsoft Teams to only someuser@mytenant.com. I have successfully tokenized the string from the "Test in Web Chat". Thus, I know what I want to send, and who I want to send it to. However, I do not know how to actually send it.

Currently, I have the following in my bot:

public class EchoBot : ActivityHandler
{
  private ConcurrentDictionary<string, ConversationReference> _conversationReferences;

  public EchoBot(ConcurrentDictionary<string, ConversationReference> conversationReferences)
  {
    _conversationReferences = conversationReferencs;
  }

  private void AddConversationReference(Activity activity)
  {
    var reference = activity.GetConversationReference();
    _conversationReferences.AddOrUpdate(reference.User.Id, reference, (key, newValue) => reference);
  }

  protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> context, CancellationToken cancellationToken)
  {
    AddConversationReference(context.Activity as Activity);

    var parameters = GetParameters();  // Parses context.Activity.Text;

    // Send a message to the target (i.e. someuser@mytenant.com)
    var connection = new Microsoft.Bot.Connector.ConnectorClient(new Uri(context.Activity.ServiceUrl));
    var tenant = context.Activity.GetChannelData<TeamsChannelData>().Tenant;
    // how do I send the message to parameters.Target?

    // Confirm message was sent to the sender
    var confirmation = $"Message was sent to {parameters.Target}.";
    await context.SendActivityAsync(MessageFactory.Text(confirmation));
  }
} 

I've reviewed how to send proactive notifications to users. However, I've been unsuccessful in a) getting the user specified in parameters.Target and b) sending a notification to that user. What am I missing?

Some User
  • 5,257
  • 13
  • 51
  • 93

1 Answers1

1

First, you'll need to map user@email.com to their Teams userId (maybe with a static dictionary), which is in the format of:

29:1I9Is_Sx0O-Iy2rQ7Xz1lcaPKlO9eqmBRTBuW6XzXXXXXXXXMij8BVMdBcL9L_RwWNJyAHFQb0TXXXXXX

You can get the Teams UserId by either:

  1. Querying the roster, or
  2. Having the user message the bot, and setting a breakpoint on an incoming message, looking at the Activity.ChannelData for the Teams userId, or
  3. Dynamically build a static dictionary of all incoming messages that stores the user's email mapped to their Teams userId (I believe both are found in Activity.ChannelData).

Note: #1 and #2 both require a user to message the bot, first, which sort of defeats the purpose of proactive messages

After you have the appropriate Teams IDs, you just send a proactive message to a Teams user. The end of this link also mentions trustServiceUrl, which you may find handy if you run into permissions/auth issues when trying to send a proactive message.

mdrichardson
  • 7,141
  • 1
  • 7
  • 21
  • Thank you. But, I've been unable to query the roster. My bot echo's input in the "Conversation" tab in teams correctly. But, when I try to query the roster, the `Team` property is null. I used this line: `var members = (await turnContext.TurnState.Get().Conversations.GetConversationMembersAsync( turnContext.Activity.GetChannelData().Team.Id).ConfigureAwait(false)).ToList();` shown [here](https://stackoverflow.com/questions/57120193/how-can-my-teams-bot-start-a-new-11-chat-with-a-known-user/57150951#57150951) – Some User Sep 25 '19 at 18:36
  • Did you add the bot to a team? `Teams > App Studio > Manifest Editor > YourBot > Test and Distribute > Install > [Dropdown by Add] Add to a team`? – mdrichardson Sep 25 '19 at 18:46
  • I did not add the app to a team. I did however install the Teams app in Teams. I'm looking for a way to use just a Teams app without creating an actual team in Teams. – Some User Sep 26 '19 at 13:00
  • @ZachTempleton You can't do that because the bot won't have appropriate scope. This is to prevent spam. – mdrichardson Oct 02 '19 at 18:09