public virtual async Task <HttpResponseMessage> Post([FromBody] Microsoft.Bot.Connector.Activity activity) { try { if (activity != null && activity.GetActivityType() == ActivityTypes.Message) { // Get and process IMessageActivity message = activity.AsMessageActivity(); ActivityRequest request = new ActivityRequest( recipient: message.Recipient.Name, text: message.Text, from: message.From.Name, fromId: message.From.Id, channelId: message.ChannelId, conversationId: message.Conversation.Id, isGroup: message.Conversation.IsGroup, attachments: message.Attachments?.Select( attachment => new AttachmentRequest(attachment.ContentUrl, attachment.ContentType) )); ActivityResponse response = await this.ActivityProcessor.ProcessActivityAsync(this.Store, request).ConfigureAwait(false); // Reply (on a new network connection) back. Microsoft.Bot.Connector.Activity reply = activity.CreateReply(); reply.Text = response.Text; foreach (AttachmentResponse attachment in response.Attachments) { reply.Attachments.Add(new Attachment(attachment.ContentType, attachment.ContentUrl, null, attachment.Name)); } // Send it either as a group message or individually, depending on how we received the message, using (ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl))) { if (message.Conversation.IsGroup.HasValue && message.Conversation.IsGroup.Value) { await connector.Conversations.SendToConversationAsync(reply); } else { await connector.Conversations.ReplyToActivityAsync(reply); } } } } catch (Exception ex) { using (ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl))) { Microsoft.Bot.Connector.Activity reply = activity.CreateReply(); reply.Text = ex.Message + " " + ex.StackTrace; await connector.Conversations.ReplyToActivityAsync(reply); } } // We always accept the message and send the response using the bot framework on another channel. return(new HttpResponseMessage(HttpStatusCode.Accepted)); }
public static Tuple <double, double> GetLocation(this Microsoft.Bot.Connector.Activity activity) { var userInfo = activity.AsMessageActivity().Entities.Where(e => e.Type.Equals("UserInfo")).SingleOrDefault(); if (userInfo == null) { return(null); } dynamic location = userInfo.Properties.Value <JObject>("Location"); return(new Tuple <double, double>( (double)location.Hub.Latitude, (double)location.Hub.Longitude)); }
public static string GetUPN(this Microsoft.Bot.Connector.Activity activity) { var tokenEntity = activity.AsMessageActivity().Entities.Where(e => e.Type.Equals("AuthorizationToken")).SingleOrDefault(); var token = tokenEntity?.Properties.Value <string>("token"); if (token == null) { return(null); } var jwt = new JwtSecurityToken(token); var upn = jwt.Payload.Claims.Where(c => c.Type.Equals("upn", StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault(); return(upn?.Value); }
public static IDictionary <string, string> GetClaims(this Microsoft.Bot.Connector.Activity activity) { var tokenEntity = activity.AsMessageActivity().Entities.Where(e => e.Type.Equals("AuthorizationToken")).SingleOrDefault(); if (tokenEntity == null) { return(null); } var token = tokenEntity.Properties.Value <string>("token"); var jwt = new JwtSecurityToken(token); var claims = jwt.Payload.Claims.Select(c => new { Key = c.Type, Value = c.Value }).ToArray(); Dictionary <string, string> ret = new Dictionary <string, string>(); foreach (var item in claims) { ret.Add(item.Key, item.Value); } return(ret); }
public async Task <object> Post([FromBody] Activity activity) { try { // Initialize the azure bot using (BotService.Initialize()) { // BotBuilder insists on getting from the default config - this overrides it Conversation.UpdateContainer(b => { b.RegisterInstance(new MicrosoftAppCredentials( Config.GetAppSetting("MicrosoftAppId"), Config.GetAppSetting("MicrosoftAppPassword") )); }); // use this to check what the registered value is // ((MicrosoftAppCredentials)(Conversation.Container.ComponentRegistry.TryGetRegistration(new Autofac.Core.TypedService(typeof(MicrosoftAppCredentials)), out var xxx) ? Conversation.Container.ResolveComponent(xxx, new Autofac.Core.Parameter[0]) : null)).MicrosoftAppId // Deserialize the incoming activity //string jsonContent = await req.Content.ReadAsStringAsync(); //var activity = JsonConvert.DeserializeObject<Activity>(jsonContent); // authenticate incoming request and add activity.ServiceUrl to MicrosoftAppCredentials.TrustedHostNames // if request is authenticated var authHeader = this.Request.Headers.GetCommaSeparatedValues(HeaderNames.Authorization); var authParts = authHeader[0].Split(new[] { ' ' }, 2); var identityToken = await BotService.Authenticator.TryAuthenticateAsync(authParts[0], authParts[1], CancellationToken.None); if (null == identityToken || !identityToken.Authenticated) { this.Response.Headers.Add("WWW-Authenticate", $"Bearer realm=\"{Request.Host}\""); return(Unauthorized()); } identityToken.ValidateServiceUrlClaim(new[] { activity }); MicrosoftAppCredentials.TrustServiceUrl(activity.ServiceUrl); if (activity != null) { // one of these will have an interface and process it switch (activity.GetActivityType()) { case ActivityTypes.Message: var text = activity.AsMessageActivity().Text ?? ""; Trace.WriteLine($"Recieved message: '{text}' from {activity.From.Id}/{activity.From.Name} on {activity.ChannelId}/{activity.Conversation.IsGroup.GetValueOrDefault()}"); Trace.WriteLine($" ChannelData: {(activity.ChannelData as JObject)}"); Trace.WriteLine($" Conversation: {activity.Conversation.ConversationType}, id: {activity.Conversation.Id}, name: {activity.Conversation.Name}, role: {activity.Conversation.Role}, properties: {activity.Conversation.Properties}"); Trace.WriteLine($" From: {activity.From.Id}/{activity.From.Name}, role: {activity.From.Role}, properties: {activity.From.Properties}"); Trace.WriteLine($" Recipient: {activity.Recipient.Id}/{activity.Recipient.Name}, role: {activity.Recipient.Role}, properties: {activity.Recipient.Properties}"); if (text.Contains("</at>") && activity.ChannelId == "msteams") { // ignore the mention of us in the reply text = new Regex("<at>.*</at>").Replace(text, "").Trim(); } if (activity.ChannelId == "slack") { var mentions = activity.Entities.Where(i => i.Type == "mention").Select(i => i.Properties.ToAnonymousObject(new { mentioned = new { id = "", name = "" }, text = "" })) .ToList(); // ignore any group messages that don't mention us if (activity.Conversation.IsGroup.GetValueOrDefault() && !mentions.Any(i => i.mentioned.name == activity.Recipient.Name)) { break; } // filter out any mentions - we don't really care about them... foreach (var mention in mentions) { if (!string.IsNullOrEmpty(mention.text)) { text = text.Replace(mention.text, ""); } } // set up the conversation so we'll be in the thread string thread_ts = ((dynamic)activity.ChannelData)?.SlackMessage?.@event?.thread_ts; string ts = ((dynamic)activity.ChannelData)?.SlackMessage?.@event?.ts; if (string.IsNullOrEmpty(thread_ts) && !string.IsNullOrEmpty(ts) && activity.Conversation.Id.Split(':').Length == 3) { // this is a main-channel conversation - pretend it came in on a thread activity.Conversation.Id += $":{ts}"; Trace.WriteLine($" Modified Conversation: {activity.Conversation.ConversationType}, id: {activity.Conversation.Id}, name: {activity.Conversation.Name}, role: {activity.Conversation.Role}, properties: {activity.Conversation.Properties}"); } } activity.AsMessageActivity().Text = text.Trim(); Trace.WriteLine($"Processing message: '{text}' from {activity.From.Id}/{activity.From.Name} on {activity.ChannelId}/{activity.Conversation.IsGroup.GetValueOrDefault()}"); await Conversation.SendAsync(activity, () => new ExceptionHandlerDialog <object>(new BotDialog(Request.GetRequestUri()), true)); break; case ActivityTypes.ConversationUpdate: var client = new ConnectorClient(new Uri(activity.ServiceUrl), new MicrosoftAppCredentials( Config.GetAppSetting("MicrosoftAppId"), Config.GetAppSetting("MicrosoftAppPassword") )); IConversationUpdateActivity update = activity; if (update.MembersAdded?.Any() ?? false) { var reply = activity.CreateReply(); var newMembers = update.MembersAdded?.Where(t => t.Id != activity.Recipient.Id); foreach (var newMember in newMembers) { reply.Text = "Welcome"; if (!string.IsNullOrEmpty(newMember.Name)) { reply.Text += $" {newMember.Name}"; } reply.Text += ", this is a bot from Rightpoint Labs Beta - say 'info' for more."; await client.Conversations.ReplyToActivityAsync(reply); } } break; case ActivityTypes.ContactRelationUpdate: case ActivityTypes.Typing: case ActivityTypes.DeleteUserData: case ActivityTypes.Ping: default: Trace.WriteLine($"Unknown activity type ignored: {activity.GetActivityType()}"); break; } } return(Accepted()); } } catch (Exception ex) { TelemetryClient.TrackException(ex); throw; } }