Exemple #1
0
        internal static async Task <int> FillMissingLinksIntoDB_Async(TelegramBotAbstract sender, MessageEventArgs e)
        {
            const string q1 = "SELECT id FROM Groups WHERE link IS NULL OR link = ''";
            var          dt = SqLite.ExecuteSelect(q1);

            var n = 0;

            if (dt == null || dt.Rows.Count == 0)
            {
                return(n);
            }

            foreach (DataRow dr in dt.Rows)
            {
                var success = await CreateInviteLinkAsync((long)dr.ItemArray[0], sender, e);

                switch (success.isNuovo)
                {
                case SuccessoGenerazioneLink.NUOVO_LINK:
                case SuccessoGenerazioneLink.RICICLATO:
                    n++;
                    break;
                }
            }

            return(n);
        }
        private static int?GetMessageTypeByName(MessageType type, int times = 1)
        {
            while (true)
            {
                if (times < 0)
                {
                    return(null);
                }

                const string q1            = "SELECT id FROM MessageTypes WHERE name = @name";
                var          keyValuePairs = new Dictionary <string, object> {
                    { "@name", type.ToString() }
                };
                var r1 = SqLite.ExecuteSelect(q1, keyValuePairs);
                var r2 = SqLite.GetFirstValueFromDataTable(r1);
                if (r1 == null || r1.Rows.Count == 0 || r2 == null)
                {
                    AddMessageType(type);
                    times--;
                    continue;
                }

                try
                {
                    return(Convert.ToInt32(r2));
                }
                catch
                {
                    return(null);
                }
            }
        }
Exemple #3
0
        internal static async Task <bool> CreateInviteLinkAsync(long chatId, TelegramBotAbstract sender)
        {
            string r = null;

            try
            {
                r = await sender.ExportChatInviteLinkAsync(chatId);
            }
            catch
            {
                // ignored
            }

            if (string.IsNullOrEmpty(r))
            {
                return(false);
            }

            const string q1 = "UPDATE Groups SET link = @link, last_update_link = @lul WHERE id = @id";

            SqLite.Execute(q1, new Dictionary <string, object>
            {
                { "@link", r },
                { "@lul", DateTime.Now },
                { "@id", chatId }
            });

            return(true);
        }
        private static void AddMessageType(MessageType type)
        {
            const string q             = "INSERT INTO MessageTypes (name) VALUES (@name)";
            var          keyValuePairs = new Dictionary <string, object> {
                { "@name", type.ToString() }
            };

            SqLite.Execute(q, keyValuePairs);
            Tables.FixIdTable("MessageTypes", "id", "name");
        }
Exemple #5
0
        private static void SalvaNuovoLink(string nuovoLink, long chatId)
        {
            const string q1 = "UPDATE Groups SET link = @link, last_update_link = @lul WHERE id = @id";

            SqLite.Execute(q1, new Dictionary <string, object>
            {
                { "@link", nuovoLink },
                { "@lul", DateTime.Now },
                { "@id", chatId }
            });
        }
        private static async Task SendMessageToSend(DataRow dr)
        {
            var done = await SendMessageFromDataRow(dr);

            if (!done)
            {
                return;
            }

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

            SqLite.Execute(q2);
        }
        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);
        }
        internal static bool AddMessage(MessageType type, string messageText,
                                        int messageFromIdPerson, int?messageFromIdEntity,
                                        long idChatSentInto, DateTime?sentDate,
                                        bool hasBeenSent, int messageFromIdBot,
                                        int messageIdTgFrom, ChatType type_chat_sent_into,
                                        int?photo_id, int?video_id)
        {
            const string q = "INSERT INTO Messages " +
                             "(id, from_id_person, from_id_entity, type, " +
                             "id_photo, id_video, message_text, id_chat_sent_into, sent_date," +
                             " has_been_sent, from_id_bot, message_id_tg_from, type_chat_sent_into) " +
                             "VALUES " +
                             "(@id, @fip, @fie, @t, @idp, @idv, @mt, @icsi, @sent_date, @hbs, @fib, @mitf, @tcsi);";

            var typeI = GetMessageTypeByName(type);

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

            var id = Tables.GetMaxId("Messages", "id");

            id++;

            SqLite.Execute(q, new Dictionary <string, object>
            {
                { "@id", id },
                { "@fip", messageFromIdPerson },
                { "@fie", messageFromIdEntity },
                { "@t", typeI },
                { "@idp", photo_id },
                { "@idv", video_id },
                { "@mt", messageText },
                { "@icsi", idChatSentInto },
                { "@sent_date", sentDate },
                { "@hbs", hasBeenSent },
                { "@fib", messageFromIdBot },
                { "@mitf", messageIdTgFrom },
                { "@tcsi", type_chat_sent_into.ToString() }
            });

            return(true);
        }
        private static async Task <int?> GetIdEntityFromPersonAsync(int id, Language question,
                                                                    TelegramBotAbstract sender, string lang, string username)
        {
            const string q =
                "SELECT Entities.id, Entities.name FROM (SELECT * FROM PeopleInEntities WHERE id_person = @idp) AS T1, Entities WHERE T1.id_entity = Entities.id";
            var r = SqLite.ExecuteSelect(q, new Dictionary <string, object> {
                { "@idp", id }
            });

            if (r == null || r.Rows.Count == 0)
            {
                return(null);
            }

            if (r.Rows.Count == 1)
            {
                return(Convert.ToInt32(r.Rows[0].ItemArray[0]));
            }

            var l = new Dictionary <string, int>();

            foreach (DataRow dr in r.Rows)
            {
                var s = dr.ItemArray[1].ToString();
                if (!string.IsNullOrEmpty(s))
                {
                    l[s] = Convert.ToInt32(dr.ItemArray[0]);
                }
            }

            var l3 = l.Keys.Select(
                l2 => new Language(
                    new Dictionary <string, string>
            {
                { "en", l2 }
            })
                ).ToList();

            var options = KeyboardMarkup.ArrayToMatrixString(l3);
            var r2      = await AskUser.AskBetweenRangeAsync(id, question, sender, lang,
                                                             options, username);

            return(l[r2]);
        }
Exemple #10
0
        internal static int GetMaxId(string tableName, string columnIdName)
        {
            var q  = "SELECT MAX(" + columnIdName + ") FROM " + tableName;
            var r  = SqLite.ExecuteSelect(q);
            var r2 = SqLite.GetFirstValueFromDataTable(r);

            if (r2 == null)
            {
                return(0);
            }

            try
            {
                return(Convert.ToInt32(r2));
            }
            catch
            {
                return(0);
            }
        }
Exemple #11
0
        public static void FixIdTable(string tableName, string columnIdName, string uniqueColumn)
        {
            var r4 = GetMaxId(tableName, columnIdName);
            var q2 = "SELECT * FROM " + tableName + " WHERE " + columnIdName + " IS NULL";
            var r5 = SqLite.ExecuteSelect(q2);

            if (r5 == null)
            {
                return;
            }

            foreach (DataRow dr in r5.Rows)
            {
                r4++;

                var valueUnique = dr[uniqueColumn].ToString();
                var q3          = "UPDATE " + tableName + " SET " + columnIdName + "=" + r4 + " WHERE " + uniqueColumn +
                                  "='" + valueUnique + "'";
                SqLite.Execute(q3);
            }
        }
        public static async Task CheckMessagesToSend()
        {
            const string q =
                "SELECT * FROM Messages WHERE Messages.has_been_sent IS FALSE AND Messages.sent_date IS NOT NULL AND julianday('now') - julianday(Messages.sent_date) <= 0";
            var dt = SqLite.ExecuteSelect(q);

            if (dt == null || dt.Rows.Count == 0)
            {
                return;
            }

            foreach (DataRow dr in dt.Rows)
            {
                try
                {
                    await SendMessageToSend(dr);
                }
                catch
                {
                    //ignored
                }
            }
        }
Exemple #13
0
        internal static async Task <int> FillMissingLinksIntoDB_Async(TelegramBotAbstract sender)
        {
            const string q1 = "SELECT id FROM Groups WHERE link IS NULL OR link = ''";
            var          dt = SqLite.ExecuteSelect(q1);

            var n = 0;

            if (dt == null || dt.Rows.Count == 0)
            {
                return(n);
            }

            foreach (DataRow dr in dt.Rows)
            {
                var success = await CreateInviteLinkAsync((long)dr.ItemArray[0], sender);

                if (success)
                {
                    n++;
                }
            }

            return(n);
        }
        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));
        }
Exemple #15
0
        private static async Task UpdateLinksFromJson2Async(GruppoTG gruppoTG, TelegramBotAbstract sender,
                                                            MessageEventArgs e, ListaGruppiTG_Update L)
        {
            var result = new GruppoTG_Update(null, SuccessoGenerazioneLink.ERRORE);

            var group_id = gruppoTG.permanentId;
            var sql1     = "empty";

            if (!string.IsNullOrEmpty(gruppoTG.idLink))
            {
                sql1 = "SELECT id FROM Groups " +
                       "WHERE Groups.link LIKE '%" + gruppoTG.idLink + "%'";

                if (gruppoTG.idLink.Length < 3)
                {
                    gruppoTG.idLink = "";
                }
            }

            if (group_id == null)
            {
                try
                {
                    if (!string.IsNullOrEmpty(gruppoTG.idLink))
                    {
                        var r1 = SqLite.ExecuteSelect(sql1);
                        if (r1 != null && r1.Rows != null && r1.Rows.Count > 0 && r1.Rows[0] != null &&
                            r1.Rows[0].ItemArray != null && r1.Rows[0].ItemArray.Length > 0)
                        {
                            var r2 = r1.Rows[0];
                            var r3 = r2.ItemArray[0];
                            group_id = Convert.ToInt64(r3);
                        }
                        else
                        {
                            result.query1Fallita = true;
                        }
                    }
                }
                catch (Exception ex1)
                {
                    Console.WriteLine(ex1);
                    var ex1m = "1" + "\n\n" + ex1.Message + "\n\n" + sql1 + "\n\n" + gruppoTG.idLink + "\n\n" +
                               gruppoTG.nome + "\n\n" + gruppoTG.newLink + "\n\n" + gruppoTG.permanentId;
                    await sender.SendTextMessageAsync(e.Message.From.Id,
                                                      new Language(
                                                          new Dictionary <string, string>
                    {
                        {
                            "it",
                            ex1m
                        }
                    }),
                                                      ChatType.Private, "it", ParseMode.Default, null, e.Message.From.Username);

                    result.gruppoTG = gruppoTG;
                    result.successoGenerazioneLink = SuccessoGenerazioneLink.ERRORE;
                    result.ExceptionMessage        = ex1m;
                    result.ExceptionObject         = ex1;
                    L.Add(result);

                    return;
                }
            }

            var sql2 = "SELECT id FROM Groups WHERE Groups.title LIKE '%' || @nome || '%'";

            if (group_id == null)
            {
                try
                {
                    if (group_id == null && !string.IsNullOrEmpty(gruppoTG.nome))
                    {
                        var r1 = SqLite.ExecuteSelect(sql2, new Dictionary <string, object> {
                            { "@nome", gruppoTG.nome }
                        });
                        if (r1 != null && r1.Rows != null && r1.Rows.Count > 0 && r1.Rows[0] != null &&
                            r1.Rows[0].ItemArray != null && r1.Rows[0].ItemArray.Length > 0)
                        {
                            var r2 = r1.Rows[0];
                            var r3 = r2.ItemArray[0];
                            group_id = Convert.ToInt64(r3);
                        }
                        else
                        {
                            result.query2Fallita = true;
                        }
                    }
                }
                catch (Exception ex2)
                {
                    Console.WriteLine(ex2);
                    var ex2m = "2" + "\n\n" + ex2.Message + "\n\n" + sql2 + "\n\n" + gruppoTG.nome;
                    await sender.SendTextMessageAsync(e.Message.From.Id,
                                                      new Language(
                                                          new Dictionary <string, string>
                    {
                        {
                            "it",
                            ex2m
                        }
                    }),
                                                      ChatType.Private, "it", ParseMode.Default, null, e.Message.From.Username);

                    result.gruppoTG = gruppoTG;
                    result.successoGenerazioneLink = SuccessoGenerazioneLink.ERRORE;
                    result.ExceptionMessage        = ex2m;
                    result.ExceptionObject         = ex2;
                    L.Add(result);

                    return;
                }
            }

            if (group_id == null)
            {
                result.gruppoTG = gruppoTG;
                result.successoGenerazioneLink = SuccessoGenerazioneLink.ERRORE;
                L.Add(result);
            }
            else
            {
                NuovoLink s3 = null;
                try
                {
                    if (group_id != null)
                    {
                        gruppoTG.UpdateID(group_id.Value);

                        s3 = await CreateInviteLinkAsync(group_id.Value, sender, e);

                        if (s3 != null)
                        {
                            gruppoTG.UpdateNewLink(s3.link);
                        }
                    }
                }
                catch (Exception ex3)
                {
                    Console.WriteLine(ex3);
                    var ex3m = "3" + "\n\n" + ex3.Message;
                    await sender.SendTextMessageAsync(e.Message.From.Id,
                                                      new Language(
                                                          new Dictionary <string, string>
                    {
                        {
                            "it",
                            ex3m
                        }
                    }),
                                                      ChatType.Private, "it", ParseMode.Default, null, e.Message.From.Username);

                    result.gruppoTG = gruppoTG;
                    result.successoGenerazioneLink = SuccessoGenerazioneLink.ERRORE;
                    result.ExceptionMessage        = ex3m;
                    result.ExceptionObject         = ex3;
                    result.createInviteLinkFallita = true;
                    L.Add(result);

                    return;
                }

                var successoGenerazione = GetSuccessoGenerazione(s3);

                result.gruppoTG = gruppoTG;
                result.successoGenerazioneLink = successoGenerazione;
                L.Add(result);
            }
        }
        internal static async Task <List <DataRow> > BanAllAsync(TelegramBotAbstract sender, MessageEventArgs e,
                                                                 string target, bool banTarget)
        {
            var targetId = await Info.GetTargetUserIdAsync(target, sender);

            if (targetId == null)
            {
                var text2 = new Language(new Dictionary <string, string>
                {
                    {
                        "en", "We were not able to BanAll the target '" + target + "', error code " +
                        ErrorCodes.TargetInvalidWhenBanAll
                    },
                    {
                        "it", "Non siamo riusciti a bannareAll il target '" + target + "', error code " +
                        ErrorCodes.TargetInvalidWhenBanAll
                    }
                });
                await SendMessage.SendMessageInPrivate(sender, e,
                                                       text2);

                return(null);
            }

            const string q1 = "SELECT id FROM Groups";
            var          dt = SqLite.ExecuteSelect(q1);

            if (dt == null || dt.Rows.Count == 0)
            {
                var text3 = new Language(new Dictionary <string, string>
                {
                    {
                        "en", "We were not able to BanAll the target '" + target + "', error code " +
                        ErrorCodes.DatatableEmptyWhenBanAll
                    },
                    {
                        "it", "Non siamo riusciti a bannareAll il target '" + target + "', error code " +
                        ErrorCodes.DatatableEmptyWhenBanAll
                    }
                });
                await SendMessage.SendMessageInPrivate(sender, e,
                                                       text3);

                return(null);
            }

            var done = new List <DataRow>();

            if (banTarget)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    try
                    {
                        var groupChatId = (long)dr["id"];
                        var success     = await BanUserFromGroup(sender, e, targetId.Value, groupChatId, null);

                        if (success)
                        {
                            done.Add(dr);
                        }
                    }
                    catch
                    {
                        ;
                    }
                }
            }
            else
            {
                foreach (DataRow dr in dt.Rows)
                {
                    try
                    {
                        var groupChatId = (long)dr["id"];
                        var success     = await UnBanUserFromGroup(sender, e, targetId.Value, groupChatId);

                        if (success)
                        {
                            done.Add(dr);
                        }
                    }
                    catch
                    {
                        ;
                    }
                }
            }

            return(done);
        }
        internal static DataTable GetAllGroups()
        {
            const string q1 = "SELECT * FROM Groups";

            return(SqLite.ExecuteSelect(q1));
        }