コード例 #1
0
 public ImportChatMessagesIntoTeamsHandler(HttpClient httpClient,
                                           IUserRepository repository,
                                           IConfiguration config,
                                           INotifier notifier,
                                           IGraphAuthService graphAuthService)
 {
     this._repository       = repository;
     this._httpClient       = httpClient;
     this._config           = config;
     this._notifier         = notifier;
     this._graphAuthService = graphAuthService;
     this._apiCaller        = new ProtectedApiCallHelper(httpClient);
     _httpClient.DefaultRequestHeaders
     .Accept
     .Add(new MediaTypeWithQualityHeaderValue("application/json"));
 }
コード例 #2
0
        public async Task <bool> HandleMessageAsync(AbstractMessage message)
        {
            try
            {
                var apiCaller   = new ProtectedApiCallHelper(_httpClient);
                var userMessage = (UserMessage)message;
                var user        = await _repository.GetUser(userMessage.O365UserUPN);

                Console.WriteLine($"Fetching Graph User {userMessage.O365UserUPN}"
                                  + " count - " + message.RetryCount);
                var response = await apiCaller.CallWebApiAndProcessResultASync($"https://graph.microsoft.com/v1.0/me", user.GraphAccessToken);

                var aadUser = JsonConvert.DeserializeObject <AADUser>(response);
                user.AadUser = aadUser;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error when fetching AAD user from graph endpoint" + ex.Message);
                message.RetryCount++;
                _notifier.Notify(message);
            }
            return(true);
        }
コード例 #3
0
        public async Task <bool> HandleMessageAsync(AbstractMessage message)
        {
            this._authenticateResult = await this._graphAuthService.GetGraphAuthResult();

            try
            {
                if (this._authenticateResult != null)
                {
                    var channelAddedMessage = (ChannelAddedMessage)message;
                    var messages            = await this._repository.GetChannelMessagesForChannelId(channelAddedMessage.ZoomChannelId);

                    var channel = await this._repository.GetChannel(channelAddedMessage.ZoomChannelId);

                    var apiCaller = new ProtectedApiCallHelper(_httpClient);
                    //await apiCaller.CallWebApiAndProcessResultASync($"https://teamsgraph.teams.microsoft.com/beta/teams('250dfa22-2334-4d15-a7c0-7d3bb9303e36')/channels", result.AccessToken, Display);
                    //await apiCaller.CallWebApiAndProcessResultASync($"https://graph.microsoft.com/beta/teams", result.AccessToken, Display);

                    string currentTime         = DateTime.Now.ToString("yyyy-mm-ddThh:mm:ss") + ".043Z";
                    string channelCreationTime = DateTime.Now.ToString("yyyy-mm-ddThh:mm:ss") + ".053Z";
                    //Create Team with migration mode set - Copy the teamId from Response Header
                    CreateTeam newTeam = new CreateTeam
                    {
                        teamCreationMode = "migration",
                        bind             = "https://graph.microsoft.com/beta/teamsTemplates('standard')",
                        displayName      = "FromZoom" + channel.Name,
                        description      = channel.Name,
                        createdDateTime  = "2021-03-12T11:22:17.043Z"
                    };

                    var data     = new StringContent(JsonConvert.SerializeObject(newTeam), Encoding.UTF8, "application/json");
                    var response = await apiCaller.CallWebApiPostAndProcessResultASync($"https://graph.microsoft.com/beta/teams",
                                                                                       this._authenticateResult.AccessToken, Display, data);

                    var location  = response.Headers.Location?.ToString();
                    var teamId    = ((location.Split('/')[1]).Remove(0, 7)).Remove(36, 2);
                    var channelId = "";

                    CreateChannelRequest newChannel = new CreateChannelRequest
                    {
                        channelCreationMode = "migration",
                        displayName         = channel.Name + "Test",
                        description         = channel.Name,
                        membershipType      = "standard",
                        createdDateTime     = "2021-03-12T11:22:17.053Z"
                    };
                    data     = new StringContent(JsonConvert.SerializeObject(newChannel), Encoding.UTF8, "application/json");
                    response = await apiCaller.CallWebApiPostAndProcessResultASync($"https://graph.microsoft.com/beta/teams/{teamId}/channels",
                                                                                   this._authenticateResult.AccessToken, Display, data);

                    if (response.IsSuccessStatusCode)
                    {
                        string json = await response.Content.ReadAsStringAsync();

                        channelId = JObject.Parse(json)["id"].ToString();
                        Console.WriteLine("ChannelId - " + channelId);
                    }
                    else
                    {
                        throw new Exception("Channel creation failed");
                    }
                    if (channelId == "")
                    {
                        throw new Exception("Channel creation failed");
                    }
                    if (messages != null && messages.Count > 0)
                    {
                        foreach (ChannelMessage textMessage in messages)
                        {
                            DateTimeOffset timeOffset = DateTimeOffset.FromUnixTimeMilliseconds(textMessage.EpochTime);
                            DateTime       msgTime    = timeOffset.DateTime;
                            //textMessage.Sender
                            var user = await this._repository.GetUserByZoomMailId(textMessage.Sender);

                            if (user == null)
                            {
                                Console.WriteLine($"Zoom user {textMessage.Sender} did not login yet, so skipping message");
                                continue;
                            }
                            ChatMessageRequest newMessage = new ChatMessageRequest
                            {
                                createdDateTime = msgTime.ToString("yyyy-MM-ddThh:mm:ss") + ".053Z",
                                from            = new From
                                {
                                    user = new User
                                    {
                                        id               = user.AadUser.Id,
                                        displayName      = user.AadUser.DisplayName,
                                        userIdentityType = "aadUser"
                                    }
                                },
                                body = new ItemBody
                                {
                                    content     = textMessage.Message,
                                    contentType = "html"
                                }
                            };
                            var str = JsonConvert.SerializeObject(newMessage);
                            data     = new StringContent(JsonConvert.SerializeObject(newMessage), Encoding.UTF8, "application/json");
                            response = await apiCaller.CallWebApiPostAndProcessResultASync($"https://graph.microsoft.com/beta/teams/{teamId}/channels/{channelId}/messages", _authenticateResult.AccessToken, Display, data);

                            if (response.IsSuccessStatusCode)
                            {
                                Console.WriteLine("Posted msg");
                            }
                            else
                            {
                                throw new Exception("Posting msg failed");
                            }
                            Thread.Sleep(100);
                        }
                    }

                    response = await apiCaller.CallWebApiPostAndProcessResultASync($"https://graph.microsoft.com/beta/teams/{teamId}/channels/{channelId}/completeMigration", _authenticateResult.AccessToken, Display, null);

                    if (response.IsSuccessStatusCode)
                    {
                        Console.WriteLine("Completed migration for newly created channel");
                    }
                    else
                    {
                        throw new Exception("Completing migration for channel failed");
                    }

                    string channelListResponse = await apiCaller.CallWebApiAndProcessResultASync($"https://graph.microsoft.com/v1.0/teams/{teamId}/channels", _authenticateResult.AccessToken);

                    var          teamsChannels  = JsonConvert.DeserializeObject <TeamsChannelList>(channelListResponse);
                    TeamsChannel generalChannel = teamsChannels.channels.Where(channel => channel.DisplayName.Equals("General")).First();

                    //Need to get the 'General' channel Id and complete migration  TODO

                    response = await apiCaller.CallWebApiPostAndProcessResultASync($"https://graph.microsoft.com/beta/teams/{teamId}/channels/{generalChannel.Id}/completeMigration", _authenticateResult.AccessToken, Display, null);

                    if (response.IsSuccessStatusCode)
                    {
                        Console.WriteLine("Completed migration for newly created channel");
                    }
                    else
                    {
                        throw new Exception("Completing migration for channel failed");
                    }

                    response = await apiCaller.CallWebApiPostAndProcessResultASync($"https://graph.microsoft.com/beta/teams/{teamId}/completeMigration", _authenticateResult.AccessToken, Display, null);

                    if (response.IsSuccessStatusCode)
                    {
                        Console.WriteLine("Completed migration for team");
                    }
                    else
                    {
                        throw new Exception("Completing migration for team failed");
                    }

                    //Add owner

                    List <TodoListAPI.BusinessModels.User> owners = await _repository.GetOwnerOfZoomChannel(channelAddedMessage.ZoomChannelId);

                    foreach (BusinessModels.User user in owners)
                    {
                        AddMemberToTeam member = new AddMemberToTeam
                        {
                            type  = "#microsoft.graph.aadUserConversationMember",
                            roles = new string[] { "owner" },
                            bind  = $"https://graph.microsoft.com/beta/users/{user.AadUser.Id}"
                        };
                        data = new StringContent(JsonConvert.SerializeObject(member), Encoding.UTF8, "application/json");
                        await apiCaller.CallWebApiPostAndProcessResultASync($"https://graph.microsoft.com/beta/teams/{teamId}/members", _authenticateResult.AccessToken, Display, data);
                    }

                    // Add Member
                    List <TodoListAPI.BusinessModels.User> members = await _repository.GetMembersOfZoomChannel(channelAddedMessage.ZoomChannelId);

                    foreach (BusinessModels.User user in members)
                    {
                        AddMemberToTeam member = new AddMemberToTeam
                        {
                            type  = "#microsoft.graph.aadUserConversationMember",
                            roles = new string[] {  },
                            bind  = $"https://graph.microsoft.com/beta/users/{user.AadUser.Id}"
                        };
                        data = new StringContent(JsonConvert.SerializeObject(member), Encoding.UTF8, "application/json");
                        await apiCaller.CallWebApiPostAndProcessResultASync($"https://graph.microsoft.com/beta/teams/{teamId}/members", _authenticateResult.AccessToken, Display, data);
                    }
                }
            }
            catch (Exception)
            {
                message.RetryCount++;
                this._notifier.Notify(message);
            }
            return(true);
        }