public virtual async Task SendMessageByUserNameTest() { var settings = GetConfig(); UserNameToSendMessage = settings[nameof(UserNameToSendMessage)]; if (string.IsNullOrWhiteSpace(UserNameToSendMessage)) { throw new Exception($"Please fill the '{nameof(UserNameToSendMessage)}' setting in app.config file first"); } var client = NewClient(); await client.ConnectAsync(); var result = await client.SearchUserAsync(UserNameToSendMessage); var user = result.Users .Where(x => x.GetType() == typeof(TLUser)) .OfType <TLUser>() .FirstOrDefault(x => x.Username == UserNameToSendMessage.TrimStart('@')); if (user == null) { var contacts = await client.GetContactsAsync(); user = contacts.Users .Where(x => x.GetType() == typeof(TLUser)) .OfType <TLUser>() .FirstOrDefault(x => x.Username == UserNameToSendMessage.TrimStart('@')); } if (user == null) { throw new System.Exception("Username was not found: " + UserNameToSendMessage); } await client.SendTypingAsync(new TLInputPeerUser() { UserId = user.Id }); Thread.Sleep(3000); await client.SendMessageAsync(new TLInputPeerUser() { UserId = user.Id }, "TEST"); }
public async Task SendMessageByUserNameTest() { UserNameToSendMessage = Environment.GetEnvironmentVariable(nameof(UserNameToSendMessage)); if (string.IsNullOrWhiteSpace(UserNameToSendMessage)) { throw new Exception( $"Please fill the '{nameof(UserNameToSendMessage)}' setting in appsettings.json file first"); } using (var client = await NewClient().ConfigureAwait(false)) { await client.ConnectService.ConnectAsync(); var result = await client.ContactsService.SearchUserAsync(UserNameToSendMessage); var user = result.Users.Items .Where(x => x.GetType() == typeof(TUser)) .OfType <TUser>() .FirstOrDefault(x => x.Username == UserNameToSendMessage.TrimStart('@')); if (user == null) { var contacts = await client.ContactsService.GetContactsAsync(); user = contacts.Cast <TContacts>().Users.Items .Where(x => x.GetType() == typeof(TUser)) .OfType <TUser>() .FirstOrDefault(x => x.Username == UserNameToSendMessage.TrimStart('@')); } if (user == null) { throw new Exception("Username was not found: " + UserNameToSendMessage); } await client.MessagesService.SendTypingAsync(new TInputPeerUser { UserId = user.Id }); await Task.Delay(3000); await client.MessagesService.SendMessageAsync(new TInputPeerUser { UserId = user.Id }, "TEST"); } }