Skip to content

Instantly share code, notes, and snippets.

@vman
Last active February 24, 2020 09:31
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/5f038a84732a56356351efb6b1f4be97 to your computer and use it in GitHub Desktop.
Save vman/5f038a84732a56356351efb6b1f4be97 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";
//Teams channel id in which to create the post.
string teamsChannelId = "19:86f8774fd88a4d4ba1bae4715dcca821@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 be mentioned.
string mentionUserPrincipalName = "user@tenant.onmicrosoft.com";
//From the Bot Channel Registration
string botClientID = "<client-id>";
string botClientSecret = "<client-secret>";
MicrosoftAppCredentials.TrustServiceUrl(serviceUrl);
var connectorClient = new ConnectorClient(new Uri(serviceUrl), new MicrosoftAppCredentials(botClientID, botClientSecret));
var teamMembers = await connectorClient.Conversations.GetConversationMembersAsync(teamInternalId, default);
var userToMention = teamMembers
.Select(channelAccount => JObject.FromObject(channelAccount).ToObject<TeamsChannelAccount>())
.First(user => user.UserPrincipalName == mentionUserPrincipalName);
var mention = new Mention
{
Mentioned = userToMention,
Text = $"<at>{userToMention.Name}</at>",
};
var topLevelMessageActivityWithMention = MessageFactory.Text($"Hi {mention.Text}");
topLevelMessageActivityWithMention.Entities = new List<Entity> { mention };
var conversationParameters = new ConversationParameters
{
IsGroup = true,
ChannelData = new TeamsChannelData
{
Channel = new ChannelInfo(teamsChannelId),
},
Activity = topLevelMessageActivityWithMention
};
await connectorClient.Conversations.CreateConversationAsync(conversationParameters);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment