public async void Execute()
        {
            while (true)
            {
                NotificationQueueEntity message = null;
                do
                {
                    message = await queueProvider.DequeueAsync <NotificationQueueEntity>(configurationProvider.GetString("notificationsQueue"));

                    if (message != null)
                    {
                        var users = await tableStorageProvider.RetrieveTableAsync <UserEntity>("users");

                        Parallel.ForEach(users, (user) =>
                        {
                            var conversationReference = JsonConvert.DeserializeObject <ConversationReference>(user.ConversationReference);
                            var client = new ConnectorClient(new Uri(conversationReference.ServiceUrl));

                            var messageActivity = Activity.CreateMessageActivity();

                            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;
                            messageActivity.Text         = string.Format(messageProvider.GetMessage("NewNotification"), message.Text);

                            client.Conversations.SendToConversation((Activity)messageActivity);
                        });
                    }
                } while (message != null);
                Thread.Sleep(TimeSpan.FromMinutes(configurationProvider.GetConfiguration <int>("NotificationPollInterval")));
            }
        }
Beispiel #2
0
        public async void Execute()
        {
            while (true)
            {
                IssueQueueEntity message = null;
                do
                {
                    var channels = await GetChannels();

                    if (channels.Any())
                    {
                        message = await queueProvider.DequeueAsync <IssueQueueEntity>(configurationProvider.GetString("issueQueue"));

                        if (message != null)
                        {
                            await tableStorageProvider.SendToTableAsync("openIssues", message);

                            foreach (var channel in channels)
                            {
                                var messageActivity = Activity.CreateMessageActivity();
                                messageActivity.Type = ActivityTypes.Message;
                                messageActivity.Text = string.Format(messageProvider.GetMessage("NewIssue"), message.Text);
                                await channel.SendToChannel(messageActivity);
                            }
                        }
                    }
                } while (message != null);
                Thread.Sleep(TimeSpan.FromMinutes(configurationProvider.GetConfiguration <int>("IssuePollInterval")));
            }
        }
Beispiel #3
0
        public async void Execute()
        {
            while (true)
            {
                MessageEntity message  = null;
                var           channels = await GetChannels();

                var runTime = DateTime.UtcNow;
                do
                {
                    if (channels.Any())
                    {
                        message = await queueProvider.DequeueAsync <MessageEntity>("usertoexpert");

                        if (message != null)
                        {
                            NormalizeQuestion(message);


                            await tableStorageProvider.SendToTableAsync("unansweredquestions", message);

                            var eventToSend = new NewQuestionEvent()
                            {
                                MessageType           = message.MessageType,
                                Question              = message.Question,
                                OriginalAnswer        = message.OriginalAnswer,
                                QuestionCorrelationId = message.QuestionCorrelationId
                            };
                            await eventProvider.SendEventAsync(eventToSend);

                            try
                            {
                                foreach (var channel in channels)
                                {
                                    var messageActivity = Activity.CreateMessageActivity();
                                    messageActivity.Type = ActivityTypes.Message;
                                    if (message.MessageType == MessageType.NOANSWER)
                                    {
                                        messageActivity.Text = string.Format(messageProvider.GetMessage("PostMessageToExpertsNoAnswer"), message.Question);
                                        var conversationId = await channel.SendToChannel(messageActivity);

                                        eventToSend.ConversationId = conversationId;
                                        await channel.SendToConverstionData(conversationId, messageActivity, new Dictionary <string, string>() { { "question", message.Question } });
                                    }
                                    else
                                    {
                                        var actions = new List <CardAction>();
                                        actions.Add(new CardAction
                                        {
                                            Title = messageProvider.GetMessage("KeepOriginalWrong"),
                                            Type  = "imBack",
                                            Value = messageProvider.GetMessage("KeepOriginalWrong"),
                                        });
                                        actions.Add(new CardAction
                                        {
                                            Title = messageProvider.GetMessage("KeepNewWrong"),
                                            Value = messageProvider.GetMessage("KeepNewWrong"),
                                            Type  = "imBack"
                                        });

                                        var normalizedQuestion         = MessageHelper.NormalizeString(message.Question);
                                        var normalizedMatchingQuestion = MessageHelper.NormalizeString(message.OriginalQuestion);

                                        if (string.Compare(normalizedQuestion, normalizedMatchingQuestion, StringComparison.InvariantCultureIgnoreCase) != 0)
                                        {
                                            actions.Add(new CardAction
                                            {
                                                Title = messageProvider.GetMessage("KeepBothWrong"),
                                                Value = messageProvider.GetMessage("KeepBothWrong"),
                                                Type  = "imBack"
                                            });
                                        }

                                        var card = new HeroCard()
                                        {
                                            Text    = string.Format(messageProvider.GetMessage("PostMessageToExpertsWrongAnswerCard"), normalizedQuestion, message.OriginalAnswer, normalizedMatchingQuestion),
                                            Buttons = actions
                                        };
                                        messageActivity.Attachments.Add(card.ToAttachment());

                                        var conversationId = await channel.SendToChannel(messageActivity);

                                        eventToSend.ConversationId = conversationId;
                                        await channel.SendToConverstionData(conversationId, messageActivity, new Dictionary <string, string>() {
                                            { "question", message.Question },
                                            { "originalQuestion", message.OriginalQuestion },
                                            { "originalAnswer", message.OriginalAnswer }
                                        });
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                await eventProvider.SendEventAsync(new TechnicalErrorEvent()
                                {
                                    Exception      = JsonConvert.SerializeObject(e),
                                    ChannelId      = string.Empty,
                                    ConversationId = string.Empty
                                });

                                continue;
                            }
                        }
                    }
                } while (message != null && (DateTime.UtcNow - runTime).TotalMinutes < configurationProvider.GetConfiguration <int>("expertBotChannelsPoll"));
                Thread.Sleep((int)TimeSpan.FromMinutes(configurationProvider.GetConfiguration <int>("ExpertBotQueuePoll")).TotalMilliseconds);
            }
        }
Beispiel #4
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);
            }
        }