public async Task Handle(Activity activity) { var channelData = activity.GetChannelData <TeamsChannelData>(); var isGroupChat = channelData.Channel == null; var teamId = isGroupChat ? string.Empty : channelData.Team.Id; var channelId = isGroupChat ? activity.Conversation.Id : channelData.Channel.Id; var draftCompetition = await _competitionService.CreateDraftCompetition( activity.ServiceUrl, Guid.Parse(channelData.Tenant.Id), teamId, channelId, activity.Locale, activity.GetOffset().TotalHours, activity.From.Name, activity.From.AadObjectId); var activityForDraft = _activityBuilder.CreateMainActivity(draftCompetition); using (var botClient = _botClientFactory.CreateBotClient(activity.ServiceUrl)) { var mainMessage = await botClient.SendToConversationAsync(activityForDraft); await _competitionService.UpdateMainActivity(draftCompetition.Id, mainMessage.Id); } }
public async Task <TaskModuleResponse> Handle(Activity activity, IUrlHelper urlHelper) { var invokeActionData = activity.GetInvokeActionData(); var competition = await _competitionService.GetCompetition(invokeActionData.CompetitionId); var errorMessage = CanActivateCompetition(competition); if (!string.IsNullOrEmpty(errorMessage)) { return(_activityBuilder.CreateDraftCompetitionEditTaskInfoResponse(competition, errorMessage, activity.Locale)); } competition = await _competitionService.ActivateCompetition(competition.Id); var mainActivity = _activityBuilder.CreateMainActivity(competition); using (var botClient = _botClientFactory.CreateBotClient(activity.ServiceUrl)) { await botClient.UpdateActivityAsync(competition.ChannelId, competition.MainActivityId, mainActivity); } await _timerService.AddScheduledHttpRequest( competition.PlannedDrawTime, "POST", urlHelper.Action(nameof(CompetitionsController.DrawForCompetition), "Competitions", new { competitionId = competition.Id })); return(null); }
public async Task Handle(Activity activity) { var invokeActionData = activity.GetInvokeActionData(); var competition = await _competitionService.AddCompetitor(invokeActionData.CompetitionId, activity.From.AadObjectId, activity.From.Name); var updatedActivity = _activityBuilder.CreateMainActivity(competition); using (var botClient = _botClientFactory.CreateBotClient(activity.ServiceUrl)) { await botClient.UpdateActivityAsync(competition.ChannelId, competition.MainActivityId, updatedActivity); } }
private async Task <bool> HandleCompetitionInitialization(Activity activity) { var parameters = ParseCreateCompetitionParameters(activity); if (parameters == null) { return(false); } var channelData = activity.GetChannelData <TeamsChannelData>(); var competition = await _competitionService.Create( activity.ServiceUrl, Guid.Parse(channelData.Tenant.Id), channelData.Team.Id, channelData.Channel.Id, parameters.PlannedDrawTime, activity.Locale, parameters.OffsetHours, parameters.Gift, parameters.GiftImageUrl, parameters.WinnerCount, activity.From.Name, activity.From.AadObjectId); var mainActivity = _activityBuilder.CreateMainActivity(competition); using (var botClient = _botClientFactory.CreateBotClient(activity.ServiceUrl)) { var mainMessage = await botClient.SendToConversationAsync(mainActivity); await _competitionService.UpdateMainActivity(competition.Id, mainMessage.Id); } await _timerService.AddScheduledHttpRequest( competition.PlannedDrawTime, "POST", Url.Action(nameof(CompetitionsController.DrawForCompetition), "Competitions", new { competitionId = competition.Id })); return(true); }
public async Task Handle(Activity activity, IUrlHelper urlHelper) { var editForm = GetEditForm(activity); var offset = activity.GetOffset(); var date = DateTimeOffset.Parse(editForm.PlannedDrawTimeLocalDate); var time = DateTimeOffset.Parse(editForm.PlannedDrawTimeLocalTime); var plannedDrawTime = new DateTimeOffset(date.Year, date.Month, date.Day, time.Hour, time.Minute, time.Second, 0, offset).ToUniversalTime(); var channelData = activity.GetChannelData <TeamsChannelData>(); var isGroupChat = channelData.Channel == null; var teamId = isGroupChat ? string.Empty : channelData.Team.Id; var channelId = isGroupChat ? activity.Conversation.Id : channelData.Channel.Id; var competition = await _competitionService.CreateActiveCompetition( activity.ServiceUrl, Guid.Parse(channelData.Tenant.Id), teamId, channelId, plannedDrawTime, activity.Locale, offset.TotalHours, editForm.Gift, editForm.GiftImageUrl, int.Parse(editForm.WinnerCount), activity.From.Name, activity.From.AadObjectId); var mainActivity = _activityBuilder.CreateMainActivity(competition); using (var botClient = _botClientFactory.CreateBotClient(activity.ServiceUrl)) { var mainMessage = await botClient.SendToConversationAsync(mainActivity); await _competitionService.UpdateMainActivity(competition.Id, mainMessage.Id); } await _timerService.AddScheduledHttpRequest( competition.PlannedDrawTime, "POST", urlHelper.Action(nameof(CompetitionsController.DrawForCompetition), "Competitions", new { competitionId = competition.Id })); }
private async Task <bool> Draw(Guid competitionId) { var competition = await _competitionService.Draw(competitionId); if (string.IsNullOrEmpty(competition.MainActivityId)) { _logger.LogWarning("The competition has not generated main activity. Skip this competition. {competitionId}", competitionId); return(false); } using (var botClient = _botClientFactory.CreateBotClient(competition.ServiceUrl)) { var updatedMainActivity = _activityBuilder.CreateMainActivity(competition); await botClient.UpdateActivityAsync(competition.ChannelId, competition.MainActivityId, updatedMainActivity); var resultActivity = _activityBuilder.CreateResultActivity(competition); var resultMessage = await botClient.SendToConversationAsync(resultActivity); await _competitionService.UpdateResultActivity(competition.Id, resultMessage.Id); } return(true); }
private async Task <bool> Draw(Guid competitionId) { var competition = await _competitionService.Draw(competitionId); if (competition == null) { // Sometimes, the competitions was drawn but posting ResultActivity failed. // So, when the timer service invokes the "draw" endpoint again, the competition is in "Closed" list already. // Get the competition from "Closed" list, and then try to post ResultActivity to Teams again. _logger.LogWarning("The competition has already been drawn and closed. Try to get it from Closed competition list. {competitionId}", competitionId); competition = await _competitionService.GetCompetition(competitionId); } if (string.IsNullOrEmpty(competition.MainActivityId)) { _logger.LogWarning("The competition has not generated main activity. Skip this competition. {competitionId}", competitionId); return(false); } if (!string.IsNullOrEmpty(competition.ResultActivityId)) { _logger.LogWarning("The competition has been drawn and the ResultActivity has already been posted. Skip the duplicate posting. {competitionId}", competitionId); return(true); } using (var botClient = _botClientFactory.CreateBotClient(competition.ServiceUrl)) { var updatedMainActivity = _activityBuilder.CreateMainActivity(competition); await botClient.UpdateActivityAsync(competition.ChannelId, competition.MainActivityId, updatedMainActivity); var resultActivity = _activityBuilder.CreateResultActivity(competition); var resultMessage = await botClient.SendToConversationAsync(resultActivity); await _competitionService.UpdateResultActivity(competition.Id, resultMessage.Id); } return(true); }