public static async Task MarkMessagesasRead(TLChannel channel, TLMessage firstmessage)
        {
            var ch = new TLInputChannel()
            {
                ChannelId = channel.Id, AccessHash = (long)channel.AccessHash
            };



            try
            {
                int    mID         = firstmessage.Id;
                string TMessage    = firstmessage.Message;
                string TvisitCount = firstmessage.Views.ToString();

                var markAsRead = new TLRequestReadHistory()
                {
                    Channel = ch,
                    // MaxId = -1,
                    MessageId = mID,
                    // Dirty = true,
                    //  MessageId = 356217,
                    // ConfirmReceived = true,
                    //Sequence = dia.unread_count
                };
                var affectedMessages = await _client.SendRequestAsync <bool>(markAsRead);

                Console.WriteLine(mID + ' ' + TMessage + ' ' + TvisitCount + ' ' + affectedMessages);
            }
            catch { }
        }
Exemplo n.º 2
0
        public async Task SetAsRead(int id, long accessHash, int messageId, int count, int numberOfTry = 0)
        {
            try
            {
                var ch = new TLInputChannel()
                {
                    ChannelId = id, AccessHash = accessHash
                };
                var markAsRead = new TLSchema.Channels.TLRequestReadHistory()
                {
                    Channel         = ch,
                    MaxId           = -1,
                    Dirty           = true,
                    MessageId       = messageId,
                    ConfirmReceived = true,
                    //Sequence = count
                };
                var readed = await telegramClient.SendRequestAsync <bool>(markAsRead);
            }
            catch (Exception ex)
            {
                ApplicationHelpers.LogException(ex);

                if (numberOfTry > 2)
                {
                    throw;
                }

                await Task.Delay(telegramSetting.DelayPerMessage);
                await SetAsRead(id, accessHash, messageId, ++numberOfTry);
            }
        }
Exemplo n.º 3
0
        public async Task UnRead2()
        {
            var rnd = new Random();

            while (true)
            {
                Task.Delay(rnd.Next(3000, 6000)).Wait();
                var dialogs = telegramClient.GetUserDialogsAsync().Result as TLDialogs;

                if (dialogs == null)
                {
                    Task.Delay(rnd.Next(3000, 6000)).Wait();
                    continue;
                }

                foreach (TLDialog dialog in dialogs.Dialogs.Where(lambdaDialog => lambdaDialog.Peer is TLPeerChannel && lambdaDialog.UnreadCount > 0))
                {
                    TLPeerChannel      peer    = (TLPeerChannel)dialog.Peer;
                    TLChannel          channel = dialogs.Chats.OfType <TLChannel>().First(lambdaChannel => lambdaChannel.Id == peer.ChannelId);
                    TLInputPeerChannel target  = new TLInputPeerChannel {
                        ChannelId = channel.Id, AccessHash = channel.AccessHash ?? 0
                    };

                    Task.Delay(rnd.Next(3000, 6000)).Wait();
                    TLChannelMessages hist = (TLChannelMessages)telegramClient.GetHistoryAsync(target, 0, -1, dialog.UnreadCount).Result;
                    if (hist == null)
                    {
                        continue;
                    }

                    var users    = hist.Users.OfType <TLUser>().ToList();
                    var messages = hist.Messages.OfType <TLMessage>().ToList();

                    foreach (TLMessage message in messages)
                    {
                        TLUser sentUser = users.Single(lambdaUser => lambdaUser.Id == message.FromId);
                        Console.WriteLine($"{channel.Title} {sentUser.FirstName} {sentUser.LastName} {sentUser.Username}: {message.Message}");
                    }

                    TLInputChannel channelToMarkRead = new TLInputChannel {
                        ChannelId = target.ChannelId, AccessHash = target.AccessHash
                    };
                    var firstAbsMessage = hist.Messages[0];
                    int firstUnreadMessageId;
                    if (firstAbsMessage is TLMessage)
                    {
                        firstUnreadMessageId = ((TLMessage)firstAbsMessage).Id;
                    }
                    else if (firstAbsMessage is TLMessageService)
                    {
                        firstUnreadMessageId = ((TLMessageService)firstAbsMessage).Id;
                    }
                    else
                    {
                        continue;
                    }

                    var markHistoryAsReadRequest = new TLSchema.Channels.TLRequestReadHistory
                    {
                        Channel         = channelToMarkRead,
                        MaxId           = -1,
                        ConfirmReceived = true,
                        Dirty           = true,
                        MessageId       = firstUnreadMessageId,
                        Sequence        = dialog.UnreadCount
                    };

                    Task.Delay(rnd.Next(3000, 6000)).Wait();
                    telegramClient.SendRequestAsync <bool>(markHistoryAsReadRequest).Wait();

                    Console.WriteLine("Mark messages as read");
                }
            }
        }