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); } }
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 { } }
private static async Task <bool?> InsertOurBotAsyncChat(TLChat x5, TLAbsInputPeer u, long?accessHash, TelegramBotAbstract telegramBotAbstract) { ; ; const long userIdOfOurBot = 768169879; TLInputChannel channel = null; if (accessHash != null) { channel = new TLInputChannel { AccessHash = accessHash.Value, ChannelId = x5.Id } } ; else { channel = new TLInputChannel { ChannelId = x5.Id } }; var r4 = await F1Async(telegramBotAbstract, userIdOfOurBot, u, x5.Title, x5.Id, channel); if (r4.returnobject != null) { return(r4.returnobject.Value); } if (r4.r2.Item1 == null) { var m = "\n"; m += "We can't make our bot admin in this group:\n"; m += "[Title] " + x5.Title + "\n"; m += "[ID] " + x5.Id; m += "\n --- end --- "; m += "\n"; var e2 = new Exception(m, r4.r2.Item2); await NotifyUtil.NotifyOwners(e2, telegramBotAbstract); await DeleteMessageAddedAsync(r4.idMessageAdded, x5, accessHash, telegramBotAbstract); return(false); } Thread.Sleep(2000); ; await DeleteMessageAddedAsync(r4.idMessageAdded, x5, accessHash, telegramBotAbstract); id_of_chats_we_know_are_ok[x5.Id] = true; return(r4.r != null && r4.r2.Item1 != null); return(null); }
private static async Task DeleteMessageAddedAsync2(int?idMessageAdded, int id, long?accessHash, TelegramBotAbstract telegramBotAbstract) { if (idMessageAdded != null) { try { await telegramBotAbstract.DeleteMessageAsync(id, idMessageAdded.Value, ChatType.Supergroup, accessHash); } catch (Exception e3) { ; try { await telegramBotAbstract.DeleteMessageAsync(id, idMessageAdded.Value, ChatType.Group, accessHash); } catch { ; try { var messageToDelete = new TLVector <int> { idMessageAdded.Value }; TLAbsInputChannel x7 = new TLInputChannel { AccessHash = accessHash.Value, ChannelId = id }; await telegramBotAbstract._userbotClient.ChannelsDeleteMessageAsync(x7, messageToDelete); } catch { ; try { /* * TLVector<int> messageToDelete = new TLVector<int>(); * messageToDelete.Add(idMessageAdded.Value); * TLAbsInputChannel x7 = new TLInputChannel() { AccessHash = x5.AccessHash.Value, ChannelId = x5.Id }; * await this._userbotClient.DeleteMessageFromChat(x7, messageToDelete); */ } catch { ; } } } } ; Thread.Sleep(2000); } }
public async Task <TLAbsUpdates> addUserIntoChannel(string userID, TLChannel channel) { if (string.IsNullOrEmpty(userID)) { return(null); } switch (_isbot) { case BotTypeApi.REAL_BOT: break; case BotTypeApi.USER_BOT: { try { var users = new TLVector <TLAbsInputUser>(); if (userID.StartsWith("@")) { var u = await UserbotPeer.GetPeerUserWithAccessHash(userID.Substring(1), _userbotClient); TLAbsInputUser input2 = new TLInputUser { AccessHash = u.AccessHash, UserId = u.UserId }; users.Add(input2); } else { users.Add(UserbotPeer.GetPeerUserFromdId(Convert.ToInt32(userID))); } var tLInputChannel = new TLInputChannel { ChannelId = channel.Id }; if (channel.AccessHash != null) { tLInputChannel.AccessHash = channel.AccessHash.Value; } var r = await _userbotClient.ChannelsInviteToChannel(tLInputChannel, users); return(r); } catch (Exception e) { ; } } break; case BotTypeApi.DISGUISED_BOT: break; } return(null); }
private async Task MarkAsRead(TLInputPeerChannel target, TLSharp.Core.TelegramClient telegram) { var ch = new TLInputChannel() { ChannelId = target.ChannelId, AccessHash = target.AccessHash }; var request = new TLRequestReadHistory() { Channel = ch, }; await telegram.SendRequestAsync <bool>(request); }
private static async Task <Tuple <bool?, DateTime?> > FixTheFactThatSomeGroupsDoesNotHaveOurModerationBot5( TLChannel x5, TLAbsInputPeer u, TelegramBotAbstract telegramBotAbstract) { if (x5 == null) { return(null); } ; if (x5.Broadcast) { return(null); } ; if (x5.AccessHash == null) { return(null); } var x7 = new TLInputChannel { AccessHash = x5.AccessHash.Value, ChannelId = x5.Id }; var isOurBotPresent = await CheckIfOurBotIsPresent2Async(x7, telegramBotAbstract); if (isOurBotPresent.Item2 != null) { return(new Tuple <bool?, DateTime?>(null, isOurBotPresent.Item2)); } if (isOurBotPresent.Item1 != null && isOurBotPresent.Item1.Value) { return(new Tuple <bool?, DateTime?>(true, null)); } var r3 = await InsertOurBotAsyncChannel(x5, u, telegramBotAbstract); return(new Tuple <bool?, DateTime?>(r3, null)); }
private static async Task <Tuple <bool?, DateTime?> > CheckIfOurBotIsPresent2Async(TLInputChannel x5, TelegramBotAbstract telegramBotAbstract) { if (id_of_chats_we_know_are_ok.ContainsKey(x5.ChannelId)) { return(new Tuple <bool?, DateTime?>(id_of_chats_we_know_are_ok[x5.ChannelId], null)); } TLAbsInputChannel channel = new TLInputChannel { ChannelId = x5.ChannelId, AccessHash = x5.AccessHash }; TLChatFull x = null; try { x = await telegramBotAbstract._userbotClient.getFullChat(channel); } catch (Exception e) { ; if (e is FloodException eflood) { ; var untilWhen = GetUntilWhenWeCanMakeRequests(eflood); return(new Tuple <bool?, DateTime?>(null, untilWhen)); } } var isOurBotPresent = CheckIfOurBotIsPresent(x); if (isOurBotPresent) { id_of_chats_we_know_are_ok[x5.ChannelId] = true; } return(new Tuple <bool?, DateTime?>(isOurBotPresent, null)); }
private async void btStart_Click(object sender, EventArgs e) { var client = new TLSharp.Core.TelegramClient(teleApi.apiId, teleApi.apiHash); await client.ConnectAsync(); while (true) { var state = await client.SendRequestAsync <TLState>(new TLRequestGetState()); var req = new TLRequestGetDifference() { Date = state.Date, Pts = state.Pts, Qts = state.Qts }; var diff = await client.SendRequestAsync <TLAbsDifference>(req) as TLDifference; if (diff != null) { foreach (var upd in diff.OtherUpdates.ToList().OfType <TLUpdateNewChannelMessage>()) { tbReceipt.Text = (upd.Message as TLMessage).Message + tbReceipt.Text; //Console.WriteLine((upd.Message as TLMessage).Message); } foreach (var ch in diff.Chats.ToList().OfType <TLChannel>().Where(x => !x.Left)) { var ich = new TLInputChannel() { ChannelId = ch.Id, AccessHash = (long)ch.AccessHash }; var readed = new TeleSharp.TL.Channels.TLRequestReadHistory() { Channel = ich, MaxId = -1 }; await client.SendRequestAsync <bool>(readed); } } await Task.Delay(500); } }
private async void MessagePinExecute(TLMessageBase message) { if (PinnedMessage?.Id == message.Id) { var dialog = new TLMessageDialog(); dialog.Title = "Unpin message"; dialog.Message = "Would you like to unpin this message?"; dialog.PrimaryButtonText = "Yes"; dialog.SecondaryButtonText = "No"; var dialogResult = await dialog.ShowAsync(); if (dialogResult == ContentDialogResult.Primary) { var channel = Peer as TLInputPeerChannel; var inputChannel = new TLInputChannel { ChannelId = channel.ChannelId, AccessHash = channel.AccessHash }; var result = await ProtoService.UpdatePinnedMessageAsync(false, inputChannel, 0); if (result.IsSucceeded) { PinnedMessage = null; } } } else { var dialog = new TLMessageDialog(); dialog.Title = "Pin message"; dialog.Message = "Would you like to pin this message?"; dialog.CheckBoxLabel = "Notify all members"; dialog.IsChecked = true; dialog.PrimaryButtonText = "Yes"; dialog.SecondaryButtonText = "No"; var dialogResult = await dialog.ShowAsync(); if (dialogResult == ContentDialogResult.Primary) { var channel = Peer as TLInputPeerChannel; var inputChannel = new TLInputChannel { ChannelId = channel.ChannelId, AccessHash = channel.AccessHash }; var silent = dialog.IsChecked == false; var result = await ProtoService.UpdatePinnedMessageAsync(silent, inputChannel, message.Id); if (result.IsSucceeded) { var updates = result.Result as TLUpdates; if (updates != null) { var newChannelMessageUpdate = updates.Updates.OfType <TLUpdateNewChannelMessage>().FirstOrDefault(); if (newChannelMessageUpdate != null) { Handle(newChannelMessageUpdate.Message as TLMessageCommonBase); Aggregator.Publish(new TopMessageUpdatedEventArgs(_currentDialog, newChannelMessageUpdate.Message)); } } PinnedMessage = message; } } } }
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"); } } }
public override TLChatBase this[long index] { get { if (TryGetValue(index, out TLChatBase value)) { return(value); } Statement statement; Sqlite3.sqlite3_prepare_v2(_database, $"SELECT {_fields} FROM `Chats` WHERE `index` = {index}", out statement); TLChatBase result = null; if (Sqlite3.sqlite3_step(statement) == SQLiteResult.Row) { var id = Sqlite3.sqlite3_column_int64(statement, 0); var title = Sqlite3.sqlite3_column_text(statement, 4); var version = Sqlite3.sqlite3_column_int(statement, 6); var date = Sqlite3.sqlite3_column_int(statement, 8); TLChatPhotoBase photo = null; var photoType = Sqlite3.sqlite3_column_type(statement, 10); if (photoType == 1) // SQLITE_INTEGER { var photo_small_local_id = Sqlite3.sqlite3_column_int(statement, 10); var photo_small_secret = Sqlite3.sqlite3_column_int64(statement, 11); var photo_small_volume_id = Sqlite3.sqlite3_column_int64(statement, 12); var photo_small_dc_id = Sqlite3.sqlite3_column_int(statement, 13); var photo_big_local_id = Sqlite3.sqlite3_column_int(statement, 14); var photo_big_secret = Sqlite3.sqlite3_column_int64(statement, 15); var photo_big_volume_id = Sqlite3.sqlite3_column_int64(statement, 16); var photo_big_dc_id = Sqlite3.sqlite3_column_int(statement, 17); photo = new TLChatPhoto { PhotoSmall = new TLFileLocation { LocalId = photo_small_local_id, Secret = photo_small_secret, VolumeId = photo_small_volume_id, DCId = photo_small_dc_id }, PhotoBig = new TLFileLocation { LocalId = photo_big_local_id, Secret = photo_big_secret, VolumeId = photo_big_volume_id, DCId = photo_big_dc_id } }; } else { photo = new TLChatPhotoEmpty(); } if (((ulong)id & PeerIdTypeMask) == PeerIdChatShift) // CHAT { var flags = (TLChat.Flag)Sqlite3.sqlite3_column_int(statement, 3); var participants_count = Sqlite3.sqlite3_column_int(statement, 7); TLInputChannelBase migratedTo = null; if (flags.HasFlag(TLChat.Flag.MigratedTo)) { var channel_id = Sqlite3.sqlite3_column_int(statement, 18); var access_hash = Sqlite3.sqlite3_column_int64(statement, 19); migratedTo = new TLInputChannel { ChannelId = channel_id, AccessHash = access_hash }; } result = new TLChat { Id = (int)(uint)((ulong)id & PeerIdMask), Flags = flags, Title = title, Version = version, ParticipantsCount = participants_count, Date = date, Photo = photo, MigratedTo = migratedTo }; } else if (((ulong)id & PeerIdTypeMask) == PeerIdChannelShift) // CHANNEL { var flags = (TLChannel.Flag)Sqlite3.sqlite3_column_int(statement, 3); var access_hash = Sqlite3.sqlite3_column_int64(statement, 2); var username = Sqlite3.sqlite3_column_text(statement, 5); var restriction_reason = Sqlite3.sqlite3_column_text(statement, 9); TLChannelAdminRights adminRights = null; if (flags.HasFlag(TLChannel.Flag.AdminRights)) { adminRights = new TLChannelAdminRights { Flags = (TLChannelAdminRights.Flag)Sqlite3.sqlite3_column_int(statement, 20) }; } TLChannelBannedRights bannedRights = null; if (flags.HasFlag(TLChannel.Flag.AdminRights)) { bannedRights = new TLChannelBannedRights { Flags = (TLChannelBannedRights.Flag)Sqlite3.sqlite3_column_int(statement, 21) }; } result = new TLChannel { Id = (int)(uint)((ulong)id & PeerIdMask), AccessHash = access_hash, Flags = flags, Title = title, Username = username, Version = version, Date = date, RestrictionReason = restriction_reason, Photo = photo, AdminRights = adminRights, BannedRights = bannedRights }; } base[index] = result; } Sqlite3.sqlite3_finalize(statement); return(result); } set { base[index] = value; Statement statement; Sqlite3.sqlite3_prepare_v2(_database, $"INSERT OR REPLACE INTO `Chats` ({_fields}) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", out statement); if (value is TLChat chat) { Sqlite3.sqlite3_bind_int64(statement, 1, (long)(PeerIdChatShift | (ulong)(uint)chat.Id)); Sqlite3.sqlite3_bind_int(statement, 2, chat.Id); Sqlite3.sqlite3_bind_null(statement, 3); Sqlite3.sqlite3_bind_int(statement, 4, (int)chat.Flags); Sqlite3.sqlite3_bind_text(statement, 5, chat.Title, -1); Sqlite3.sqlite3_bind_null(statement, 6); Sqlite3.sqlite3_bind_int(statement, 7, chat.Version); Sqlite3.sqlite3_bind_int(statement, 8, chat.ParticipantsCount); Sqlite3.sqlite3_bind_int(statement, 9, chat.Date); Sqlite3.sqlite3_bind_null(statement, 10); if (chat.Photo is TLChatPhoto photo && photo.PhotoSmall is TLFileLocation small && photo.PhotoBig is TLFileLocation big) { Sqlite3.sqlite3_bind_int(statement, 11, small.LocalId); Sqlite3.sqlite3_bind_int64(statement, 12, small.Secret); Sqlite3.sqlite3_bind_int64(statement, 13, small.VolumeId); Sqlite3.sqlite3_bind_int(statement, 14, small.DCId); Sqlite3.sqlite3_bind_int(statement, 15, big.LocalId); Sqlite3.sqlite3_bind_int64(statement, 16, big.Secret); Sqlite3.sqlite3_bind_int64(statement, 17, big.VolumeId); Sqlite3.sqlite3_bind_int(statement, 18, big.DCId); } else { Sqlite3.sqlite3_bind_null(statement, 11); Sqlite3.sqlite3_bind_null(statement, 12); Sqlite3.sqlite3_bind_null(statement, 13); Sqlite3.sqlite3_bind_null(statement, 14); Sqlite3.sqlite3_bind_null(statement, 15); Sqlite3.sqlite3_bind_null(statement, 16); Sqlite3.sqlite3_bind_null(statement, 17); Sqlite3.sqlite3_bind_null(statement, 18); } if (chat.HasMigratedTo && chat.MigratedTo is TLInputChannel inputChannel) { Sqlite3.sqlite3_bind_int(statement, 19, inputChannel.ChannelId); Sqlite3.sqlite3_bind_int64(statement, 20, inputChannel.AccessHash); } else { Sqlite3.sqlite3_bind_null(statement, 19); Sqlite3.sqlite3_bind_null(statement, 20); } Sqlite3.sqlite3_bind_null(statement, 21); Sqlite3.sqlite3_bind_null(statement, 22); } else if (value is TLChannel channel)
private static async Task <Tuple <bool?, DateTime?> > FixTheFactThatSomeGroupsDoesNotHaveOurModerationBot4( TLChat x5, TLAbsInputPeer u, TelegramBotAbstract telegramBotAbstract) { if (x5 == null) { return(null); } ; ; if (x5.MigratedTo == null) { var r4 = await FixTheFactThatSomeGroupsDoesNotHaveOurModerationBot6Async(x5, u, telegramBotAbstract); return(r4); } var x6 = x5.MigratedTo; TLInputChannel x8 = null; if (x6 is TLInputChannel x7) { x8 = x7; } else { ; return(null); } if (x5.MigratedTo == null) { return(null); } TLAbsInputChannel channel = x8; TLChatFull x = null; var channel2 = GetChannel(channel); if (channel2 == null) { return(new Tuple <bool?, DateTime?>(false, null)); } var isOurBotPresent2 = await CheckIfOurBotIsPresent2Async(channel2, telegramBotAbstract); if (isOurBotPresent2 == null) { return(new Tuple <bool?, DateTime?>(false, null)); } if (isOurBotPresent2.Item2 != null) { return(new Tuple <bool?, DateTime?>(null, isOurBotPresent2.Item2)); } if (isOurBotPresent2.Item1 != null && isOurBotPresent2.Item1.Value) { return(new Tuple <bool?, DateTime?>(true, null)); } var r3 = await InsertOurBotAsyncChat(x5, u, x8.AccessHash, telegramBotAbstract); return(new Tuple <bool?, DateTime?>(r3, null)); }
private static async Task <ResultF1> F1Async(TelegramBotAbstract telegramBotAbstract, long userIdOfOurBot, TLAbsInputPeer u, string x5Title, int x5Id, TLInputChannel channel) { var users = new TLVector <TLAbsInputUser>(); if (u == null) { return(new ResultF1(false, null, null, null)); } long accessHashUser = 0; ; TLInputPeerUser u5 = null; if (u is TLInputPeerUser u4) { u5 = u4; } if (u5 == null) { return(new ResultF1(false, null, null, null)); } accessHashUser = u5.AccessHash; TLAbsInputUser u2 = new TLInputUser { UserId = (int)userIdOfOurBot, AccessHash = accessHashUser }; users.Add(u2); TLAbsUpdates r = null; try { r = await telegramBotAbstract._userbotClient.ChannelsInviteToChannel(channel, users); } catch (Exception e) { var m = "\n"; m += "We can't add our bot in this group:\n"; m += "[Title] " + x5Title + "\n"; m += "[ID] " + x5Id; m += "\n --- end --- "; m += "\n"; var e2 = new Exception(m, e); await NotifyUtil.NotifyOwners(e2, telegramBotAbstract); return(new ResultF1(false, null, r, null)); } Thread.Sleep(2000); var idMessageAdded = GetIdMessageAdded(r); Tuple <TLAbsUpdates, Exception> r2 = null; try { r2 = await PromoteToAdminAsync(u2, channel, telegramBotAbstract); } catch (Exception e2) { ; } ; return(new ResultF1(null, idMessageAdded, r, r2)); }
private static async Task <Tuple <TLAbsUpdates, Exception> > PromoteToAdminAsync(TLAbsInputUser u2, TLInputChannel channel, TelegramBotAbstract telegramBotAbstract) { TLAbsUpdates r2 = null; try { TLAbsChannelParticipantRole role = new TLChannelRoleEditor(); r2 = await telegramBotAbstract._userbotClient.ChannelsEditAdmin(channel, u2, role); } catch (Exception e2) { ; try { TLAbsChannelParticipantRole role2 = new TLChannelRoleModerator(); r2 = await telegramBotAbstract._userbotClient.ChannelsEditAdmin(channel, u2, role2); } catch (Exception e3) { try { var r3 = await telegramBotAbstract._userbotClient.Messages_EditChatAdmin(channel.ChannelId, u2, true); if (r3 == false) { return(new Tuple <TLAbsUpdates, Exception>(null, e3)); } } catch (Exception e4) { ; return(new Tuple <TLAbsUpdates, Exception>(null, e4)); } return(new Tuple <TLAbsUpdates, Exception>(null, e3)); } return(new Tuple <TLAbsUpdates, Exception>(null, e2)); } ; return(new Tuple <TLAbsUpdates, Exception>(r2, null)); }