Example #1
0
        public virtual async Task SendMessageToUser(string toUserName, string message)
        {
            var client = new TLSharp.Core.TelegramClient(apiId, apiHash);
            await client.ConnectAsync();

            var normalizedUser = (toUserName.StartsWith("@") ?
                                  toUserName.Substring(1, toUserName.Length - 1) :
                                  toUserName).ToLower();
            var dialogs = (TLDialogs)await client.GetUserDialogsAsync();

            var user = dialogs.Users
                       .Where(x => x.GetType() == typeof(TLUser))
                       .OfType <TLUser>()
                       .FirstOrDefault(x => x.Username != null && x.Username.ToLower() == normalizedUser);

            if (user != null)
            {
                await client.SendTypingAsync(new TLInputPeerUser()
                {
                    UserId = user.Id, AccessHash = user.AccessHash.Value
                });

                await client.SendMessageAsync(new TLInputPeerUser()
                {
                    UserId = user.Id, AccessHash = user.AccessHash.Value
                }, message);
            }

            //await client.SendMessageAsync(new TLInputPeerUser() {  UserId = user.Id, AccessHash = user.AccessHash.Value }, message);


            //send message
            //await client.SendMessageAsync(new TLInputPeerChannel() { ChannelId = chat.Id, AccessHash = chat.AccessHash.Value }, "OUR_MESSAGE");
            //var result = await client.SearchUserAsync(normalizedUser, 10);
            //if (result.Users.Count > 0)
            //{
            //    TLUser user = (TLUser)result.Users[0];
            //    await client.SendMessageAsync(new TLInputPeerUser() { UserId = user.Id }, message);
            //}
            //var normalizedNumber1 = toUserName.StartsWith("@") ?
            //    toUserName.Substring(1, toUserName.Length - 1) :
            //    toUserName;

            //var user = result.Users
            //   .OfType<TLUser>()
            //   .FirstOrDefault(x => x.Username == toUserName);
            //await client.SendMessageAsync(new TLInputPeerUser() { UserId = user.Id }, message);
        }