/// <summary>
        /// Async function for send preview card.
        /// </summary>
        /// <param name="previewDataEntity">previewDataEntity param.</param>
        /// <param name="maxRetryCount">maxRetryCount param.</param>
        /// <param name="log">log param.</param>
        /// <returns>sending message response.</returns>
        public async Task <SendMessageResponse> SendPreviewUpdateMessageAsync(
            NotificationUpdatePreviewEntity previewDataEntity, int maxRetryCount, ILogger log)
        {
            // Set the service URL in the trusted list to ensure the SDK includes the token in the request.
            MicrosoftAppCredentials.TrustServiceUrl(previewDataEntity.ServiceUrl);

            var response = new SendMessageResponse
            {
                TotalNumberOfSendThrottles = 0,
                AllSendStatusCodes         = string.Empty,
            };

            await this.botAdapter.ContinueConversationAsync(
                botAppId : this.microsoftAppId,
                reference : previewDataEntity.ConversationReferance,
                callback : async(turnContext, cancellationToken) =>
            {
                previewDataEntity.MessageActivity.Conversation = turnContext.Activity.Conversation;
                var policy = this.GetRetryPolicy(maxRetryCount, log);
                try
                {
                    // Send message.
                    var messageResponse = await policy.ExecuteAsync(async() => await turnContext.SendActivityAsync(previewDataEntity.MessageActivity));

                    // Success.
                    response.ResultType          = SendMessageResult.Succeeded;
                    response.StatusCode          = (int)HttpStatusCode.Created;
                    response.AllSendStatusCodes += $"{(int)HttpStatusCode.Created},";
                    response.ActivityId          = messageResponse.Id;
                }
                catch (ErrorResponseException e)
                {
                    var errorMessage = $"{e.GetType()}: {e.Message}";
                    log.LogError(e, $"Failed to send message. Exception message: {errorMessage}");

                    response.StatusCode          = (int)e.Response.StatusCode;
                    response.AllSendStatusCodes += $"{(int)e.Response.StatusCode},";
                    response.ErrorMessage        = e.Response.Content;
                    switch (e.Response.StatusCode)
                    {
                    case HttpStatusCode.TooManyRequests:
                        response.ResultType = SendMessageResult.Throttled;
                        response.TotalNumberOfSendThrottles = maxRetryCount;
                        break;

                    case HttpStatusCode.NotFound:
                        response.ResultType = SendMessageResult.RecipientNotFound;
                        break;

                    default:
                        response.ResultType = SendMessageResult.Failed;
                        break;
                    }
                }
            },
                cancellationToken : CancellationToken.None);

            return(response);
        }
示例#2
0
        /// <inheritdoc/>
        public async Task <SendMessageResponse> SendMessageAsync(
            IMessageActivity message,
            string conversationId,
            string serviceUrl,
            int maxAttempts,
            ILogger log)
        {
            if (message is null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (string.IsNullOrEmpty(conversationId))
            {
                throw new ArgumentException($"'{nameof(conversationId)}' cannot be null or empty", nameof(conversationId));
            }

            if (string.IsNullOrEmpty(serviceUrl))
            {
                throw new ArgumentException($"'{nameof(serviceUrl)}' cannot be null or empty", nameof(serviceUrl));
            }

            if (log is null)
            {
                throw new ArgumentNullException(nameof(log));
            }

            var conversationReference = new ConversationReference
            {
                ServiceUrl   = serviceUrl,
                Conversation = new ConversationAccount
                {
                    Id = conversationId,
                },
            };

            var response = new SendMessageResponse
            {
                TotalNumberOfSendThrottles = 0,
                AllSendStatusCodes         = string.Empty,
            };

            await this.botAdapter.ContinueConversationAsync(
                botId : this.microsoftAppId,
                reference : conversationReference,
                callback : async(turnContext, cancellationToken) =>
            {
                var policy = this.GetRetryPolicy(maxAttempts, log);
                try
                {
                    // Send message.
                    await policy.ExecuteAsync(async() => await turnContext.SendActivityAsync(message));

                    // Success.
                    response.ResultType          = SendMessageResult.Succeeded;
                    response.StatusCode          = (int)HttpStatusCode.Created;
                    response.AllSendStatusCodes += $"{(int)HttpStatusCode.Created},";
                }
                catch (ErrorResponseException exception)
                {
                    var errorMessage = $"{exception.GetType()}: {exception.Message}";
                    log.LogError(exception, $"Failed to send message. Exception message: {errorMessage}");

                    response.StatusCode          = (int)exception.Response.StatusCode;
                    response.AllSendStatusCodes += $"{(int)exception.Response.StatusCode},";
                    response.ErrorMessage        = exception.ToString();
                    switch (exception.Response.StatusCode)
                    {
                    case HttpStatusCode.TooManyRequests:
                        response.ResultType = SendMessageResult.Throttled;
                        response.TotalNumberOfSendThrottles = maxAttempts;
                        break;

                    case HttpStatusCode.NotFound:
                        response.ResultType = SendMessageResult.RecipientNotFound;
                        break;

                    default:
                        response.ResultType = SendMessageResult.Failed;
                        break;
                    }
                }
            },
                cancellationToken : CancellationToken.None);

            return(response);
        }
        /// <inheritdoc/>
        public async Task <SendMessageResponse> SendMessageAsync(
            IMessageActivity message,
            string conversationId,
            string serviceUrl,
            int maxAttempts,
            bool shouldMentionAndNotify,
            ChannelAccount account,
            ILogger log)
        {
            if (message is null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (string.IsNullOrEmpty(conversationId))
            {
                throw new ArgumentException($"'{nameof(conversationId)}' cannot be null or empty", nameof(conversationId));
            }

            if (string.IsNullOrEmpty(serviceUrl))
            {
                throw new ArgumentException($"'{nameof(serviceUrl)}' cannot be null or empty", nameof(serviceUrl));
            }

            if (log is null)
            {
                throw new ArgumentNullException(nameof(log));
            }

            // Set the service URL in the trusted list to ensure the SDK includes the token in the request.
            MicrosoftAppCredentials.TrustServiceUrl(serviceUrl);

            var conversationReference = new ConversationReference
            {
                ServiceUrl   = serviceUrl,
                Conversation = new ConversationAccount
                {
                    Id = conversationId,
                },
            };

            var response = new SendMessageResponse
            {
                TotalNumberOfSendThrottles = 0,
                AllSendStatusCodes         = string.Empty,
            };

            await this.botAdapter.ContinueConversationAsync(
                botAppId : this.microsoftAppId,
                reference : conversationReference,
                callback : async(turnContext, cancellationToken) =>
            {
                var policy = this.GetRetryPolicy(maxAttempts, log);
                try
                {
                    // Prepare message.
                    if (shouldMentionAndNotify)
                    {
                        //var mention = new Mention
                        //{
                        //    Mentioned = account,
                        //    Text = $"<at>{XmlConvert.EncodeName(account.Name)}</at>",
                        //};
                        //message.Text = mention.Text;
                        message.TeamsNotifyUser();
                    }

                    // Send message.
                    await policy.ExecuteAsync(async() => await turnContext.SendActivityAsync(message));

                    // Success.
                    response.ResultType          = SendMessageResult.Succeeded;
                    response.StatusCode          = (int)HttpStatusCode.Created;
                    response.AllSendStatusCodes += $"{(int)HttpStatusCode.Created},";
                }
                catch (ErrorResponseException e)
                {
                    var errorMessage = $"{e.GetType()}: {e.Message}";
                    log.LogError(e, $"Failed to send message. Exception message: {errorMessage}");

                    response.StatusCode          = (int)e.Response.StatusCode;
                    response.AllSendStatusCodes += $"{(int)e.Response.StatusCode},";
                    response.ErrorMessage        = e.Response.Content;
                    switch (e.Response.StatusCode)
                    {
                    case HttpStatusCode.TooManyRequests:
                        response.ResultType = SendMessageResult.Throttled;
                        response.TotalNumberOfSendThrottles = maxAttempts;
                        break;

                    case HttpStatusCode.NotFound:
                        response.ResultType = SendMessageResult.RecipientNotFound;
                        break;

                    default:
                        response.ResultType = SendMessageResult.Failed;
                        break;
                    }
                }
            },
                cancellationToken : CancellationToken.None);

            return(response);
        }