Exemplo n.º 1
0
        public async void Execute()
        {
            while (true)
            {
                IEnumerable <StillInterestedEntity> messages = null;
                do
                {
                    var day = DateTimeOffset.UtcNow.AddDays(-configurationProvider.GetConfiguration <int>("stillInterestedTimeoutDays"));

                    TableQuery <StillInterestedEntity> tableQuery = new TableQuery <StillInterestedEntity>().Where(TableQuery.CombineFilters(
                                                                                                                       TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.LessThanOrEqual, day),
                                                                                                                       TableOperators.And, TableQuery.GenerateFilterConditionForBool("ReceivedAnswer", QueryComparisons.Equal, false)));

                    messages = await tableStorageProvider.RetrieveFromTableAsync("stillInterested", tableQuery);

                    if (messages.Any(x => !x.ReceivedAnswer))
                    {
                        foreach (var message in messages.Where(x => !x.ReceivedAnswer))
                        {
                            var conversationReference = JsonConvert.DeserializeObject <ConversationReference>(message.ConversationReference).GetPostToBotMessage();

                            var client = new ConnectorClient(new Uri(conversationReference.ServiceUrl));

                            using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, conversationReference))
                            {
                                var botData = scope.Resolve <IBotData>();
                                await botData.LoadAsync(CancellationToken.None);

                                var task = scope.Resolve <IDialogTask>();

                                //interrupt the stack
                                var dialog = new StillInterestedDialog(message);
                                task.Call(dialog.Void <object, IMessageActivity>(), null);

                                await task.PollAsync(CancellationToken.None);

                                //flush dialog stack
                                await botData.FlushAsync(CancellationToken.None);
                            }
                        }
                        await tableStorageProvider.DeleteFromTableAsync("stillInterested", messages);
                    }
                } while (messages.Any());
                Thread.Sleep(TimeSpan.FromHours(configurationProvider.GetConfiguration <int>("stillInterestedPollIntervalHours")));
            }
        }
Exemplo n.º 2
0
        public async void Execute()
        {
            while (true)
            {
                MessageEntity message = null;
                do
                {
                    message = await queueProvider.DequeueAsync <MessageEntity>("experttouser");


                    if (message != null)
                    {
                        var questionFromStillInterested = await tableStorageProvider.RetrievePartitionFromTableAsync <StillInterestedEntity>("stillinterested", message.Question);

                        var users = await tableStorageProvider.RetrieveTableAsync <UserEntity>("users");

                        if (questionFromStillInterested.Any())
                        {
                            foreach (var question in questionFromStillInterested)
                            {
                                var user = users.FirstOrDefault(x => x.UserId == question.UserId);
                                if (user != null)
                                {
                                    var conversationReference = JsonConvert.DeserializeObject <ConversationReference>(user.ConversationReference);

                                    var connector = new ConnectorClient(new Uri(conversationReference.ServiceUrl));
                                    MicrosoftAppCredentials.TrustServiceUrl(conversationReference.ServiceUrl, DateTime.MaxValue);
                                    var messageActivity = Activity.CreateMessageActivity();

                                    if (!question.ReceivedAnswer)
                                    {
                                        question.ReceivedAnswer = true;
                                        if (string.Compare(message.OriginalAnswer, message.ExpertAnswer, StringComparison.InvariantCultureIgnoreCase) == 0)
                                        {
                                            messageActivity.Text = string.Format(messageProvider.GetMessage("ReceivedOriginalAnswer"), message.Question, message.ExpertAnswer);
                                        }
                                        else
                                        {
                                            messageActivity.Text = string.Format(messageProvider.GetMessage("ReceivedAnswer"), message.Question, message.ExpertAnswer);
                                        }
                                    }
                                    else if (user.Notifications)

                                    {
                                        messageActivity.Text = string.Format(messageProvider.GetMessage("ReceivedAnswerUpdate"), message.Question, message.ExpertAnswer);
                                    }
                                    else
                                    {
                                        //Notifications are off. We clean the entry
                                        await tableStorageProvider.DeleteFromTableAsync("stillinterested", new StillInterestedEntity[] { question });

                                        return;
                                    }

                                    messageActivity.Conversation = new ConversationAccount(id: conversationReference.Conversation.Id);
                                    messageActivity.Recipient    = new ChannelAccount(id: conversationReference.User.Id, name: conversationReference.User.Name);
                                    messageActivity.From         = new ChannelAccount(id: conversationReference.Bot.Id, name: conversationReference.Bot.Name);
                                    messageActivity.ChannelId    = conversationReference.ChannelId;

                                    try
                                    {
                                        await connector.Conversations.SendToConversationAsync((Activity)messageActivity);

                                        await tableStorageProvider.SendToTableAsync("stillinterested", question);
                                    }
                                    catch (Exception e)
                                    {
                                        await eventProvider.SendEventAsync(new TechnicalErrorEvent()
                                        {
                                            Exception      = JsonConvert.SerializeObject(e),
                                            ChannelId      = conversationReference.ChannelId,
                                            ConversationId = conversationReference.Conversation.Id,
                                        });

                                        continue;
                                    }
                                }
                            }
                        }
                    }
                } while (message != null);
                Thread.Sleep(10000);
            }
        }