2
votes

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 [email protected]

When this is sent via the "Test in Web Chat" piece, I'd like to show "Hello" in Microsoft Teams to only [email protected]. 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. [email protected])
    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?

1

1 Answers

1
votes

First, you'll need to map [email protected] 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.