Exemplo n.º 1
0
        public static async Task <NotificationSendStatus> SendNotification(string serviceUrl, string tenantId, string userId, string messageText, Attachment attachment)
        {
            var connectorClient = new ConnectorClient(new Uri(serviceUrl));

            var parameters = new ConversationParameters
            {
                Members     = new ChannelAccount[] { new ChannelAccount(userId) },
                ChannelData = new TeamsChannelData
                {
                    Tenant       = new TenantInfo(tenantId),
                    Notification = new NotificationInfo()
                    {
                        Alert = true
                    }
                }
            };

            try
            {
                var conversationResource = await connectorClient.Conversations.CreateConversationAsync(parameters);

                var replyMessage = Activity.CreateMessageActivity();
                replyMessage.Conversation = new ConversationAccount(id: conversationResource.Id.ToString());
                replyMessage.ChannelData  = new TeamsChannelData()
                {
                    Notification = new NotificationInfo(true)
                };
                replyMessage.Text = messageText;
                if (attachment != null)
                {
                    replyMessage.Attachments.Add(attachment);
                }

                var resourceResponse = await connectorClient.Conversations.SendToConversationAsync(conversationResource.Id, (Activity)replyMessage);

                return(new NotificationSendStatus()
                {
                    MessageId = resourceResponse.Id, IsSuccessful = true
                });
            }
            catch (Exception ex)
            {
                // Handle the error.
                ErrorLogService.LogError(ex);
                return(new NotificationSendStatus()
                {
                    IsSuccessful = false, FailureMessage = ex.Message
                });
            }
        }
        private static async Task <NotificationSendStatus> GetConversationId(ConnectorClient connectorClient, string tenantId, string userId)
        {
            var parameters = new ConversationParameters
            {
                Members     = new ChannelAccount[] { new ChannelAccount(userId) },
                ChannelData = new TeamsChannelData
                {
                    Tenant       = new TenantInfo(tenantId),
                    Notification = new NotificationInfo()
                    {
                        Alert = true
                    },
                },
                IsGroup = false,
                Bot     = new ChannelAccount(ApplicationSettings.AppId)
            };

            try
            {
                var exponentialBackoffRetryStrategy = new ExponentialBackoff(5, TimeSpan.FromSeconds(2),
                                                                             TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(1));


                // Define the Retry Policy
                var retryPolicy = new RetryPolicy(new BotSdkTransientExceptionDetectionStrategy(), exponentialBackoffRetryStrategy);

                var conversationResource = await retryPolicy.ExecuteAsync(() =>
                                                                          connectorClient.Conversations.CreateConversationAsync(parameters)
                                                                          ).ConfigureAwait(false);

                //var conversationResource = await
                //                            connectorClient.Conversations.CreateConversationAsync(parameters)
                //                            ;

                return(new NotificationSendStatus()
                {
                    MessageId = conversationResource.Id, IsSuccessful = true
                });
            }
            catch (Exception ex)
            {
                // Handle the error.
                ErrorLogService.LogError(ex);
                return(new NotificationSendStatus()
                {
                    IsSuccessful = false, FailureMessage = ex.Message
                });
            }
        }
        //public static async Task<NotificationSendStatus> SendPersonalNotificationNew(ConnectorClient connectorClient, string tenantId, User userDetails, string messageText, Attachment attachment)
        //{
        //    //MicrosoftAppCredentials.TrustServiceUrl(serviceUrl, DateTime.MaxValue);
        //    //var connectorClient = new ConnectorClient(new Uri(serviceUrl));
        //    if (string.IsNullOrEmpty(userDetails.PersonalConversationId))
        //    {
        //        var createConversationResult = await GetConversationId(connectorClient, tenantId, userDetails.BotConversationId);
        //        if (createConversationResult.IsSuccessful)
        //        {
        //            userDetails.PersonalConversationId = createConversationResult.MessageId;
        //            //await Cache.Users.AddOrUpdateItemAsync(userDetails.Id, userDetails);
        //        }
        //        else
        //            return createConversationResult; // Failed
        //    }

        //    return await SendNotificationToConversationId(connectorClient, tenantId, userDetails.PersonalConversationId, messageText, attachment);

        //}

        private static async Task <NotificationSendStatus> SendNotificationToConversationId(ConnectorClient connectorClient, string tenantId, string conversationId, string messageText, Attachment attachment)
        {
            try
            {
                var replyMessage = Activity.CreateMessageActivity();

                replyMessage.Conversation = new ConversationAccount(id: conversationId);
                replyMessage.ChannelData  = new TeamsChannelData()
                {
                    Notification = new NotificationInfo(true)
                };
                replyMessage.Text = messageText;
                if (attachment != null)
                {
                    replyMessage.Attachments.Add(attachment);
                }

                var exponentialBackoffRetryStrategy = new ExponentialBackoff(5, TimeSpan.FromSeconds(2),
                                                                             TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(1));

                // Define the Retry Policy
                var retryPolicy = new RetryPolicy(new BotSdkTransientExceptionDetectionStrategy(), exponentialBackoffRetryStrategy);

                var resourceResponse = await retryPolicy.ExecuteAsync(() =>
                                                                      connectorClient.Conversations.SendToConversationAsync(conversationId, (Activity)replyMessage)
                                                                      ).ConfigureAwait(false);

                //var resourceResponse = await
                //                       connectorClient.Conversations.SendToConversationAsync(conversationId, (Activity)replyMessage)
                //                       ;

                return(new NotificationSendStatus()
                {
                    MessageId = resourceResponse.Id, IsSuccessful = true
                });
            }
            catch (Exception ex)
            {
                ErrorLogService.LogError(ex);
                return(new NotificationSendStatus()
                {
                    IsSuccessful = false, FailureMessage = ex.Message
                });
            }
        }
Exemplo n.º 4
0
        public static async Task <NotificationSendStatus> SendChannelNotification(ChannelAccount botAccount, string serviceUrl, string channelId, string messageText, Attachment attachment)
        {
            try
            {
                var replyMessage = Activity.CreateMessageActivity();
                replyMessage.Text = messageText;

                if (attachment != null)
                {
                    replyMessage.Attachments.Add(attachment);
                }

                var connectorClient = new ConnectorClient(new Uri(serviceUrl));

                var parameters = new ConversationParameters
                {
                    Bot         = botAccount,
                    ChannelData = new TeamsChannelData
                    {
                        Channel      = new ChannelInfo(channelId),
                        Notification = new NotificationInfo()
                        {
                            Alert = true
                        }
                    },
                    IsGroup  = true,
                    Activity = (Activity)replyMessage
                };

                var conversationResource = await connectorClient.Conversations.CreateConversationAsync(parameters);

                return(new NotificationSendStatus()
                {
                    MessageId = conversationResource.Id, IsSuccessful = true
                });
            }
            catch (Exception ex)
            {
                ErrorLogService.LogError(ex);
                return(new NotificationSendStatus()
                {
                    IsSuccessful = false, FailureMessage = ex.Message
                });
            }
        }
        public async Task <string> GetTeamPhoto(string tenantId, string teamId)
        {
            var graphClient     = GetAuthenticatedClient();
            var profilePhotoUrl = string.Empty;

            try
            {
                var    baseDirectory = $"/ProfilePhotos/{tenantId}/";
                var    fileName      = teamId + ".png";
                string imagePath     = System.Web.Hosting.HostingEnvironment.MapPath("~" + baseDirectory);
                if (!System.IO.Directory.Exists(imagePath))
                {
                    System.IO.Directory.CreateDirectory(imagePath);
                }
                imagePath += fileName;

                if (System.IO.File.Exists(imagePath))
                {
                    return(ApplicationSettings.BaseUrl + baseDirectory + fileName);
                }

                var photo = await graphClient.Groups[teamId].Photo.Content.Request().GetAsync();
                using (var fileStream = System.IO.File.Create(imagePath))
                {
                    photo.Seek(0, SeekOrigin.Begin);
                    photo.CopyTo(fileStream);
                }
                profilePhotoUrl = ApplicationSettings.BaseUrl + baseDirectory + fileName;
            }
            catch (Exception ex)
            {
                ErrorLogService.LogError(ex);
                profilePhotoUrl = ApplicationSettings.BaseUrl + "/Resources/Team.png";
            }
            return(profilePhotoUrl);
        }
 public override void OnException(HttpActionExecutedContext context)
 {
     ErrorLogService.LogError(context.Exception);
 }