Skip to content

Instantly share code, notes, and snippets.

@vman
Last active April 8, 2021 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vman/804b7a41de269380e354eafa8c8b8dc9 to your computer and use it in GitHub Desktop.
Save vman/804b7a41de269380e354eafa8c8b8dc9 to your computer and use it in GitHub Desktop.
using Microsoft.Bot.Builder;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Bot.Schema;
using Microsoft.Bot.Schema.Teams;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Teams.Bot.Conversations
{
class Program
{
public static async Task Main(string[] args)
{
//Teams internal id
string teamInternalId = "19:96391bb270744e218c04dc8f571d3d8b@thread.skype";
//The Bot Service Url needs to be dynamically stored and fetched from the Team. Recommendation is to store the serviceUrl from the bot Payload and later re-use it to send proactive messages.
string serviceUrl = "https://smba.trafficmanager.net/emea/";
//the upn of the user who should recieve the personal message
string mentionUserPrincipalName = "user@tenant.onmicrosoft.com";
//Office 365/Azure AD tenant id for the
string tenantId = "<tenant-GUID>";
//From the Bot Channel Registration
string botClientID = "<client-id>";
string botClientSecret = "<client-secret>";
var connectorClient = new ConnectorClient(new Uri(serviceUrl), new MicrosoftAppCredentials(botClientID, botClientSecret));
var user = await ((Microsoft.Bot.Connector.Conversations)connectorClient.Conversations).GetConversationMemberAsync(mentionUserPrincipalName, teamInternalId, default);
var personalMessageActivity = MessageFactory.Text($"Personal message from the Bot!");
var conversationParameters = new ConversationParameters()
{
ChannelData = new TeamsChannelData
{
Tenant = new TenantInfo
{
Id = tenantId,
}
},
Members = new List<ChannelAccount>() { user }
};
var response = await connectorClient.Conversations.CreateConversationAsync(conversationParameters);
await connectorClient.Conversations.SendToConversationAsync(response.Id, personalMessageActivity);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment