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 <Tuple <bool?, int, string> > GetHasBeenSentAsync(DataRow dr, TelegramBotAbstract sender)
        {
            try
            {
                var b1 = (bool)dr["has_been_sent"];

                var s1 = b1 ? "S" : "N";
                s1 += "\n";
                s1 += "GetHasBeenSentAsync";
                var e1 = new Exception(s1);
                await NotifyUtil.NotifyOwners(e1, sender);

                return(new Tuple <bool?, int, string>(b1, 1, s1)); //todo: change to "return b1"
            }
            catch
            {
                ;
            }

            try
            {
                var s  = dr["has_been_sent"].ToString();
                var b2 = s == "1" || s == "S";

                var s2 = b2 ? "S" : "N";
                s2 += "\n";
                s2 += "GetHasBeenSentAsync";
                var e2 = new Exception(s2);
                await NotifyUtil.NotifyOwners(e2, sender);

                return(new Tuple <bool?, int, string>(b2, 2, s2)); //todo: change to "return b2"
            }
            catch
            {
                ;
            }

            var s4 = "[WE DON'T KNOW]";

            try
            {
                s4 = dr["has_been_sent"].ToString();
            }
            catch
            {
                ;
            }

            var s3 = s4;

            s3 += "\n";
            s3 += "GetHasBeenSentAsync";
            var e3 = new Exception(s3);
            await NotifyUtil.NotifyOwners(e3, sender);

            return(new Tuple <bool?, int, string>(null, 3, s3));
        }
Exemple #3
0
        internal static async Task <bool> SendExceptionAsync(Exception e, TelegramBotAbstract telegramBotAbstract)
        {
            if (telegramBotAbstract == null)
            {
                return(false);
            }

            await NotifyUtil.NotifyOwners(e, telegramBotAbstract);

            return(true);
        }
        public static async Task <bool> CheckMessagesToSend(bool force_send_everything_in_queue,
                                                            TelegramBotAbstract telegramBotAbstract)
        {
            DataTable dt = null;
            var       q  = "SELECT * " +
                           "FROM Messages ";

            dt = SqLite.ExecuteSelect(q);
            if (dt == null || dt.Rows.Count == 0)
            {
                return(false);
            }

            foreach (DataRow dr in dt.Rows)
            {
                try
                {
                    var botToReportException = FindBotIfNeeded(null, telegramBotAbstract);
                    var r1 = await SendMessageToSend(dr, null, !force_send_everything_in_queue, botToReportException);

                    telegramBotAbstract = FindBotIfNeeded(r1, telegramBotAbstract);
                    if (telegramBotAbstract != null &&
                        r1 != null) // && r1.scheduleMessageSentResult != Enums.ScheduleMessageSentResult.ALREADY_SENT)
                    {
                        switch (r1.scheduleMessageSentResult)
                        {
                        case ScheduleMessageSentResult.NOT_THE_RIGHT_TIME:
                        case ScheduleMessageSentResult.FAILED_SEND:
                        case ScheduleMessageSentResult.SUCCESS:
                        case ScheduleMessageSentResult.WE_DONT_KNOW_IF_IT_HAS_BEEN_SENT:
                        {
                            await NotifyOwnersOfResultAsync(r1, telegramBotAbstract);

                            break;
                        }

                        case ScheduleMessageSentResult.THE_MESSAGE_IS_NOT_SCHEDULED:
                        case ScheduleMessageSentResult.ALREADY_SENT:
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    await NotifyUtil.NotifyOwners(e, BotUtil.GetFirstModerationRealBot(telegramBotAbstract));
                }
            }

            return(true);
        }
        private static async Task NotifyOwnersOfResultAsync(MessageSendScheduled r1,
                                                            TelegramBotAbstract telegramBotAbstract)
        {
            var s3 = r1.ToString();
            var s4 = r1?.r1?.Item2.ToString();

            if (string.IsNullOrEmpty(s4))
            {
                s4 = "[NULL(1)]";
            }
            s3 += "\n[Id1]: " + s4 + "\n";
            var s5 = r1?.r1?.Item3;

            if (string.IsNullOrEmpty(s5))
            {
                s5 = "[NULL(2)]";
            }
            s3 += "[Id2]: " + s5 + "\n";
            s3 += "[Id3]: " + r1?.scheduleMessageSentResult + "\n";
            s3 += "CheckMessagesToSend\n\n";
            var e3 = new Exception(s3);
            await NotifyUtil.NotifyOwners(e3, telegramBotAbstract);
        }
        internal static async Task <MessageSentResult> SendMessageInAGroup(TelegramBotAbstract telegramBotClient,
                                                                           string lang, Language text,
                                                                           long chatId, ChatType chatType, ParseMode parseMode, long?replyToMessageId,
                                                                           bool disablePreviewLink, int i = 0)
        {
            MessageSentResult r1 = null;

            if (telegramBotClient == null)
            {
                return(null);
            }

            if (i > 5)
            {
                return(null);
            }

            try
            {
                r1 = await telegramBotClient.SendTextMessageAsync(chatId,
                                                                  text,
                                                                  chatType,
                                                                  lang,
                                                                  parseMode,
                                                                  username : null,
                                                                  replyMarkupObject : null,
                                                                  replyToMessageId : replyToMessageId,
                                                                  disablePreviewLink : disablePreviewLink);
            }
            catch (Exception e1)
            {
                await NotifyUtil.NotifyOwners(e1, telegramBotClient, i + 1);
            }

            return(r1);
        }
        private static async Task <MessageSendScheduled> SendMessageToSend(DataRow dr,
                                                                           TelegramBotAbstract telegramBotAbstract,
                                                                           bool schedule, TelegramBotAbstract botToReportException)
        {
            bool?has_been_sent            = null;
            Tuple <bool?, int, string> r1 = null;

            try
            {
                r1 = await GetHasBeenSentAsync(dr, telegramBotAbstract);
            }
            catch (Exception e3)
            {
                await NotifyUtil.NotifyOwners(e3, botToReportException);
            }

            if (r1 != null)
            {
                has_been_sent = r1.Item1;
            }

            if (has_been_sent == null)
            {
                return(new MessageSendScheduled(ScheduleMessageSentResult.WE_DONT_KNOW_IF_IT_HAS_BEEN_SENT, null, null,
                                                r1));
            }

            if (has_been_sent.Value)
            {
                return(new MessageSendScheduled(ScheduleMessageSentResult.ALREADY_SENT, null, null, r1));
            }

            DateTime?dt = null;

            try
            {
                dt = (DateTime)dr["sent_date"];
            }
            catch
            {
                ;
            }

            if (schedule && dt == null)
            {
                return(new MessageSendScheduled(ScheduleMessageSentResult.THE_MESSAGE_IS_NOT_SCHEDULED, null, null, r1));
            }

            if (schedule && dt > DateTime.Now)
            {
                return(new MessageSendScheduled(ScheduleMessageSentResult.NOT_THE_RIGHT_TIME, null, null, r1));
            }

            var done = await SendMessageFromDataRow(dr, null, null, false, telegramBotAbstract, 0);

            if (done.IsSuccess() == false)
            {
                return(new MessageSendScheduled(ScheduleMessageSentResult.FAILED_SEND, null, null, r1));
            }

            var q2 = "UPDATE Messages SET has_been_sent = TRUE WHERE id = " + dr["id"];

            SqLite.Execute(q2);

            return(new MessageSendScheduled(ScheduleMessageSentResult.SUCCESS, null, null, r1));
        }
        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));
        }
        public static async Task <bool> FixTheFactThatSomeGroupsDoesNotHaveOurModerationBot2(
            TelegramBotAbstract telegramBotAbstract)
        {
            const int      LIMIT = 20;
            var            i     = 0;
            TLAbsInputPeer u     =
                await UserbotPeer.GetPeerUserWithAccessHash("polinetwork3bot", telegramBotAbstract._userbotClient);

            if (u == null)
            {
                return(false);
            }

            id_of_chats_we_know_are_ok = new Dictionary <long, bool>();

            while (true)
            {
                TLAbsDialogs   x = null;
                FloodException floodException1 = null;
                try
                {
                    x = await telegramBotAbstract._userbotClient.GetUserDialogsAsync(limit : LIMIT, offsetId : i);
                }
                catch (FloodException floodException)
                {
                    floodException1 = floodException;
                }

                if (x == null && floodException1 != null)
                {
                    var untilWhen = GetUntilWhenWeCanMakeRequests(floodException1);
                    WaitUntil(untilWhen);

                    try
                    {
                        x = await telegramBotAbstract._userbotClient.GetUserDialogsAsync(limit : LIMIT, offsetId : i);
                    }
                    catch (Exception e7)
                    {
                        ;
                    }
                }

                if (x == null)
                {
                    return(i > 0);
                }

                if (x is TLDialogs x2)
                {
                    if (x2.Chats != null)
                    {
                        foreach (var x4 in x2.Chats)
                        {
                            var r1 = await FixTheFactThatSomeGroupsDoesNotHaveOurModerationBot3(x4, u,
                                                                                                telegramBotAbstract);

                            await NotifyUtil.NotifyIfFalseAsync(r1, 1.ToString(), telegramBotAbstract);
                        }
                    }
                }
                else if (x is TLDialogsSlice x3)
                {
                    if (x3.Chats != null)
                    {
                        foreach (var x4 in x3.Chats)
                        {
                            var r1 = await FixTheFactThatSomeGroupsDoesNotHaveOurModerationBot3(x4, u,
                                                                                                telegramBotAbstract);

                            await NotifyUtil.NotifyIfFalseAsync(r1, 2.ToString(), telegramBotAbstract);
                        }
                    }
                }
                else
                {
                    ;
                }

                i += LIMIT;
            }

            throw new NotImplementedException();
        }