Example #1
0
        public async Task <Unit> Handle(HandleMessageRequest request, CancellationToken cancellationToken)
        {
            var activity        = request.Activity;
            var connectorClient = request.connectorClient;

            Activity reply;
            string   botLastMessage;

            var senderAadId = activity.SenderAadId();

            var senderUserDetails = await _repository.UserDetailsGet(senderAadId);

            var lastMatch = await _bot.GetLastMatch(senderAadId);

            var lastBotMessageInfo = await _repository.BotLastMessageGet(senderAadId);

            var userOptStatusInfo = await _repository.UserInfoGet(senderAadId);

            if (senderUserDetails == null || lastBotMessageInfo == null)
            {
                reply = await _mediator.Send(new UnregisterdUserRequest { Activity = activity });

                botLastMessage = BotMessageTypes.UnregisteredUser;
            }
            else if (lastMatch == null && userOptStatusInfo != null && userOptStatusInfo.OptedIn == false)
            {
                reply = await _mediator.Send(new UserIsOptedDownRequest { Activity = activity });

                botLastMessage = BotMessageTypes.UnregisteredUser;
            }
            else if (lastMatch == null)
            {
                _telemetryClient.TrackTrace($"User {senderUserDetails.Email} has last time math");
                throw new Exception($"User {senderUserDetails.Email} has last time math");
            }
            else if (activity.Is(CardActions.Optout))
            {
                reply = await _mediator.Send(new OptOutRequest { Activity = activity });

                botLastMessage = BotMessageTypes.OptOut;
            }
            else if (activity.Is(CardActions.Optin))
            {
                reply = await _mediator.Send(new OptInRequest { Activity = activity });

                botLastMessage = BotMessageTypes.OptIn;
            }
            else if (activity.Is(CardActions.FeedbackYes))
            {
                reply = await _mediator.Send(new FeedbackYesRequest { Activity = activity, UserMatch = lastMatch, BotLastMessage = lastBotMessageInfo });

                botLastMessage = BotMessageTypes.FbRootYesDetails;
            }
            else if (activity.Is(CardActions.FeedbackNo))
            {
                reply = await _mediator.Send(new FeedbackNoRequest { Activity = activity, UserMatch = lastMatch, BotLastMessage = lastBotMessageInfo });

                botLastMessage = BotMessageTypes.FbRootNoDetails;
            }
            else if (activity.Is(CardActions.FeedbackYesSuper))
            {
                reply = await _mediator.Send(new FeedbackYesSuperReplyRequest { Activity = activity, UserMatch = lastMatch, BotLastMessage = lastBotMessageInfo });

                botLastMessage = BotMessageTypes.FbYesSuperResponse;
            }
            else if (activity.Is(CardActions.FeedbackYesGood))
            {
                reply = await _mediator.Send(new FeedBackYesGoodReplyRequest()
                {
                    Activity = activity, UserMatch = lastMatch, BotLastMessage = lastBotMessageInfo
                });

                botLastMessage = BotMessageTypes.FbYesGoodResponse;
            }
            else if (activity.Is(CardActions.FeedbackYesBad))
            {
                reply = await _mediator.Send(new FeedbackYesBadRequest()
                {
                    Activity = activity, UserMatch = lastMatch, BotLastMessage = lastBotMessageInfo
                });

                botLastMessage = BotMessageTypes.FbYesBadResponse;
            }
            else if (activity.Is(CardActions.FeedBackNoNoResponse))
            {
                reply = await _mediator.Send(new FeedbackNoNoResponseRequest()
                {
                    Activity = activity, UserMatch = lastMatch, BotLastMessage = lastBotMessageInfo
                });

                botLastMessage = BotMessageTypes.FbNoMeetupResponse;
            }
            else if (activity.Is(CardActions.FeedBackNoCanceled))
            {
                reply = await _mediator.Send(new FeedBackNoCanceledRequest()
                {
                    Activity = activity, UserMatch = lastMatch, BotLastMessage = lastBotMessageInfo
                });

                botLastMessage = BotMessageTypes.FbNoCanceledResponse;
            }
            else if (activity.Is(CardActions.FeedBackNoNoGoodTime))
            {
                reply = await _mediator.Send(new FeedBackNoNoGoodTimeRequest()
                {
                    Activity = activity, UserMatch = lastMatch, BotLastMessage = lastBotMessageInfo
                });

                botLastMessage = BotMessageTypes.FbNoGoodTimeResponse;
            }
            else if (activity.Is(CardActions.FeedBackNoAnother))
            {
                reply = await _mediator.Send(new FeedBackNoAnotherRequest()
                {
                    Activity = activity, UserMatch = lastMatch, BotLastMessage = lastBotMessageInfo
                });

                botLastMessage = BotMessageTypes.FbNoAnotherReasonResponse;
            }
            else
            {
                var commonRes = await _mediator.Send(new CommonTextRequest { Activity = activity, UserMatch = lastMatch, BotLastMessage = lastBotMessageInfo });

                reply          = commonRes.Reply;
                botLastMessage = commonRes.NewBotMessage;
            }

            if (reply != null)
            {
                await connectorClient.Conversations.ReplyToActivityAsync(reply, cancellationToken);

                await _repository.BotLastMessageUpdate(senderAadId, senderUserDetails?.Email, botLastMessage);
            }

            return(Unit.Value);
        }
Example #2
0
        /// <summary>
        /// Generate first feedBackmessage to user1 about meetup with user2
        /// </summary>
        /// <returns>The number of pairups that were made</returns>
        public async Task <int> CreateFeedbackMsgAndNotify()
        {
            this.telemetryClient.TrackTrace("generating feedback msgs");

            var installedTeamsCount = 0;
            var pairsNotifiedCount  = 0;
            var usersNotifiedCount  = 0;

            try
            {
                var teams = await this.dataProvider.InstalledTeamsGet();

                installedTeamsCount = teams.Count;
                this.telemetryClient.TrackTrace($"Generating pairs for {installedTeamsCount} teams");

                foreach (var team in teams)
                {
                    this.telemetryClient.TrackTrace($"Pairing members of team {team.Id}");

                    try
                    {
                        MicrosoftAppCredentials.TrustServiceUrl(team.ServiceUrl);
                        var connectorClient = new ConnectorClient(new Uri(team.ServiceUrl));

                        var allUsers = await connectorClient.Conversations.GetConversationMembersAsync(team.TeamId);

                        var allUsersList = allUsers.Select(x => new { User = x, ChannelAccount = x.AsTeamsChannelAccount() })
                                           .ToList();

                        var startDate  = DateTime.UtcNow.AddDays(-1 * Constants.FeedBackDelayDays);
                        var dbMatchers = await dataProvider.UserMatchInfoSearchByDate(startDate);

                        foreach (var userInfo in allUsersList)
                        {
                            var dbMatch = dbMatchers
                                          .Where(p => p.SenderAadId == userInfo.ChannelAccount.ObjectId)
                                          .OrderByDescending(p => p.Created)
                                          .FirstOrDefault();
                            if (dbMatch != null)
                            {
                                var senderDetails = await dataProvider.UserDetailsGet(dbMatch.SenderAadId);

                                var recipientDetails = await dataProvider.UserDetailsGet(dbMatch.RecipientAadId);

                                var card = FeedbackCard.GetCard(senderDetails?.GivenName, recipientDetails?.GivenName);
                                await NotifyUser(connectorClient, card, userInfo.User, team.TenantId);

                                await dataProvider.BotLastMessageUpdate(
                                    userInfo.ChannelAccount.ObjectId,
                                    userInfo.ChannelAccount.Email,
                                    BotMessageTypes.Fb);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        this.telemetryClient.TrackTrace($"Error pairing up team members: {ex.Message}", SeverityLevel.Warning);
                        this.telemetryClient.TrackException(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                this.telemetryClient.TrackTrace($"Error making pairups: {ex.Message}", SeverityLevel.Warning);
                this.telemetryClient.TrackException(ex);
            }

            // Log telemetry about the pairups
            var properties = new Dictionary <string, string>
            {
                { "InstalledTeamsCount", installedTeamsCount.ToString() },
                { "PairsNotifiedCount", pairsNotifiedCount.ToString() },
                { "UsersNotifiedCount", usersNotifiedCount.ToString() },
            };

            this.telemetryClient.TrackEvent("ProcessedPairups", properties);

            this.telemetryClient.TrackTrace($"Made {pairsNotifiedCount} pairups, {usersNotifiedCount} notifications sent");
            return(pairsNotifiedCount);


            return(0);
        }