/// <summary>
        /// Gets a user setting from the database
        /// </summary>
        /// <typeparam name="T">The type the setting should be (bool, int, string)</typeparam>
        /// <param name="user">What user the setting comes from</param>
        /// <param name="field">The name of the setting</param>
        /// <param name="db">The database TelegramBotDbContext</param>
        /// <param name="def">The default value for the field</param>
        /// <returns></returns>
        public static T GetSetting <T>(this TelegramBotUser user, string field, TelegramBotDbContext db, object def)
        {
            //if (db.Connection.State != ConnectionState.Open)
            //    db.Connection.Open();
            ////verify settings exist
            //var columns = new SQLiteCommand("PRAGMA table_info(users)", db.Connection).ExecuteReader();
            //var t = default(T);

            //while (columns.Read())
            //{
            //    if (String.Equals(columns[1].ToString(), field))
            //    {
            //        var result = new SqliteCommand($"select {field} from users where ID = {user.ID}", db.Connection).ExecuteScalar();
            //        if (t != null && t.GetType() == typeof(bool))
            //        {
            //            result = (result.ToString() == "1"); //make it a boolean value
            //        }
            //        return (T)result;
            //    }
            //}
            //var type = "BLOB";
            //if (t == null)
            //    type = "TEXT";
            //else if (t.GetType() == typeof(int))
            //    type = "INTEGER";
            //else if (t.GetType() == typeof(bool))
            //    type = "INTEGER";

            //new SqliteCommand($"ALTER TABLE users ADD COLUMN {field} {type} DEFAULT {(type == "INTEGER" ? def : $"'{def}'")};", db.Connection)
            //    .ExecuteNonQuery();
            //return (T)def;
            throw new NotImplementedException();
        }
        public static TelegramBotUser GetTelegramUser(TelegramBotDbContext db, int adminId, Update update = null, InlineQuery query = null, CallbackQuery cbQuery = null, bool logPoint = true)
        {
            var users = db.Users.AsNoTracking().ToList();

            var from = update?.Message.From ?? query?.From ?? cbQuery?.From;

            if (from == null)
            {
                return(null);
            }
            var u = db.Users.AsNoTracking().FirstOrDefault(x => x.UserId == from.Id) ?? new TelegramBotUser
            {
                FirstSeen  = DateTime.Now,
                Points     = 0,
                Debt       = 0,
                IsBotAdmin = false, //Program.LoadedSetting.TelegramDefaultAdminUserId == from.Id,
                UserId     = from.Id
            };

            u.UserName = from.Username;
            if (query?.Location != null)
            {
                u.Location = $"{query.Location.Latitude},{query.Location.Longitude}";
            }
            u.Name = (from.FirstName + " " + from.LastName).Trim();
            if (logPoint)
            {
                var where   = update != null ? update.Message.Chat.Title ?? "Private" : "Using inline query";
                u.LastHeard = DateTime.Now;
                u.LastState = "talking in " + where;
                u.Points   += update?.Message.Text.Length ?? 0 * 10;
            }
            u.Save(db);
            return(u);
        }
        public static void SetString(this TelegramBotSetting set, TelegramBotDbContext db, string field, string value)
        {
            throw new NotImplementedException();

            //new SQLiteCommand($"Update settings set {field} = '{value}' WHERE Alias = '{set.Alias}'", db.Connection)
            //    .ExecuteNonQuery();
        }
 public static void Save(this TelegramBotGroup u, TelegramBotDbContext db)
 {
     if (u.ID == null || !ExistsInDb(u, db))
     {
         //need to insert
         //db.ExecuteNonQuery(
         //    "insert into chatgroup (GroupId, Name, UserName, MemberCount) VALUES (@GroupId, @Name, @UserName, @MemberCount)",
         //    u);
         //u.ID =
         //    db.Connection.Query<int>(
         //        $"SELECT ID FROM chatgroup WHERE GroupId = @GroupId", u)
         //        .First();
         db.Groups.Add(u);
         db.SaveChanges();
         u.ID = db.Groups.FirstOrDefault(c => c.GroupId == u.GroupId).ID;
     }
     else
     {
         db.Groups.Update(u);
         db.SaveChanges();
         //db.ExecuteNonQuery(
         //    "UPDATE chatgroup SET GroupId = @GroupId, Name = @Name, UserName = @UserName, MemberCount = @MemberCount WHERE ID = @ID",
         //    u);
     }
 }
        public static string ExecuteQuery(this TelegramBotDbContext db, string commandText, object param = null)
        {
            // Ensure we have a connection
            //if (db.Connection == null)
            //{
            //    throw new NullReferenceException(
            //        "Please provide a connection");
            //}

            //// Ensure that the connection state is Open
            //if (db.Connection.State != ConnectionState.Open)
            //{
            //    db.Connection.Open();
            //}
            //var reader = db.Connection.ExecuteReader(commandText, param);
            //var response = "";
            //for (int i = 0; i < reader.FieldCount; i++)
            //{
            //    response += $"{reader.GetName(i)} - ";
            //}
            //response += "\n";
            //while (reader.Read())
            //{
            //    for (int i = 0; i < reader.FieldCount; i++)
            //    {
            //        response += $"{reader[i]} - ";
            //    }
            //    response += "\n";
            //}
            //// Use Dapper to execute the given query
            //return response;
            throw new NotImplementedException();
        }
        public virtual void SeedDb(TelegramBotDbContext db, params int[] admins)
        {
            if (!db.Users.AsNoTracking().ToList().Any(c => c.UserId == LoadedSetting.TelegramDefaultAdminUserId))
            {
                using (var tx = db.Database.BeginTransaction())
                {
                    try
                    {
                        foreach (var u in admins)
                        {
                            db.Users.Add(new TelegramBotUser()
                            {
                                IsBotAdmin = true, UserId = u, FirstSeen = DateTime.UtcNow
                            });
                        }
                        db.Users.Add(new TelegramBotUser()
                        {
                            IsBotAdmin = true, UserId = LoadedSetting.TelegramDefaultAdminUserId, FirstSeen = DateTime.UtcNow
                        });


                        db.SaveChanges();
                        tx.Commit();
                    }
                    catch (Exception ex)
                    {
                        Log.Write($"Saving admin error: {ex}", LogLevel.Error, null, "error.log");
                    }
                }
            }
        }
        /// <summary>
        /// Returns the requested field from settings.
        /// </summary>
        /// <param name="set"></param>
        /// <param name="db"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        public static string GetString(this TelegramBotSetting set, TelegramBotDbContext db, string field)
        {
            throw new NotImplementedException();

            //return
            //    db.Connection.Query<string>($"select {field} from settings where Alias = '{set.Alias}'")
            //        .FirstOrDefault();
        }
        /// <summary>
        /// Constructor. You may inject IServiceProvider to freely use you registered services in your modules
        /// </summary>
        /// <param name="key"></param>
        /// <param name="adminId"></param>
        /// <param name="serviceProvider"></param>
        /// <param name="alias"></param>
        public TelegramBotWrapper(string key, int adminId, IServiceProvider serviceProvider = null, string alias = "TelegramBotFramework", bool needNewUserApproove = false, string paymentToken = null, string dir = "", string webHookUrl = null, bool shouldUseInMemoryDb = false)
        {
            if (!String.IsNullOrEmpty(webHookUrl))
            {
                _webHookUrl = webHookUrl;
            }
            if (!String.IsNullOrEmpty(dir))
            {
                RootDirectory = dir;
            }

            UserMustBeApprooved = needNewUserApproove;
            _paymentToken       = paymentToken;
            ServiceProvider     = serviceProvider;
            if (!Directory.Exists(Path.Combine(RootDirectory)))
            {
                Directory.CreateDirectory(Path.Combine(RootDirectory));
            }
            Log = new TelegramBotLogger(Path.Combine(RootDirectory, "Logs-" + alias));
            var setting = new TelegramBotSetting()
            {
                Alias = alias, TelegramDefaultAdminUserId = adminId, TelegramBotAPIKey = key
            };

            LoadedSetting = setting;

            try
            {
                Db = shouldUseInMemoryDb ? new TelegramBotDbContext() : new TelegramBotDbContext(Path.Combine(RootDirectory, alias));
                Db.Database.EnsureCreated();
                SeedDb(Db);
            }
            catch (Exception ex)
            {
                Log.WriteLine($"Db creating data error: {ex.ToString()}", LogLevel.Error, null, "error.log");
            }

            Console.OutputEncoding = Encoding.UTF8;
            Messenger.MessageSent += MessengerOnMessageSent;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;

            //            var telegramBotModuleDir = Path.Combine(RootDirectory, "AddonModules-" + alias);

            //WatchForNewModules(telegramBotModuleDir);
        }
Esempio n. 9
0
        public static TelegramBotGroup GetGroup(TelegramBotDbContext db, Update update = null)
        {
            var from = update?.Message?.Chat;

            if (from == null)
            {
                return(null);
            }
            var u = db.Groups.FirstOrDefault(c => c.ID == from.Id) ?? new TelegramBotGroup
            {
                GroupId = from.Id
            };

            u.Name     = from.Title;
            u.UserName = from.Username;
            u.Save(db);
            return(u);
        }
        public static int ExecuteNonQuery(this TelegramBotDbContext db, string commandText, object param = null)
        {
            // Ensure we have a connection
            //if (db.Connection == null)
            //{
            //    throw new NullReferenceException(
            //        "Please provide a connection");
            //}

            //// Ensure that the connection state is Open
            //if (db.Connection.State != ConnectionState.Open)
            //{
            //    db.Connection.Open();
            //}

            //// Use Dapper to execute the given query
            //return db.Connection.Execute(commandText, param);
            throw new NotImplementedException();
        }
        public static void Save(this TelegramBotUser u, TelegramBotDbContext db)
        {
            if (u.ID == null || !ExistsInDb(u, db))
            {
                using (var tx = db.Database.BeginTransaction())
                {
                    try
                    {
                        db.Users.Add(u);
                        db.SaveChanges();
                        u.ID = db.Users.FirstOrDefault(c => c.UserId == u.UserId).ID;
                    }
                    catch { }
                }

                //db.Database.ExecuteSqlCommand(
                //    "insert into users (Name, UserId, UserName, FirstSeen, LastHeard, Points, Location, Debt, LastState, Greeting, Grounded, GroundedBy, IsBotAdmin, LinkingKey, Description) VALUES (@Name, @UserId, @UserName, @FirstSeen, @LastHeard, @Points, @Location, @Debt, @LastState, @Greeting, @Grounded, @GroundedBy, @IsBotAdmin, @LinkingKey, @Description)",
                //    u);
                //u.ID =
                //    db.Database.Query<int>(
                //        $"SELECT ID FROM Users WHERE UserId = @UserId", u)
                //        .First();
            }
            else
            {
                //using(var tx = db.Database.BeginTransaction())
                //{
                //    try
                //    {
                //        db.Users.Update(u);
                //        db.SaveChanges();
                //        tx.Commit();
                //    }
                //    catch { }
                //}

                //db.ExecuteNonQuery(
                //    "UPDATE users SET Name = @Name, UserId = @UserId, UserName = @UserName, FirstSeen = @FirstSeen, LastHeard = @LastHeard, Points = @Points, Location = @Location, Debt = @Debt, LastState = @LastState, Greeting = @Greeting, Grounded = @Grounded, GroundedBy = @GroundedBy, IsBotAdmin = @IsBotAdmin, LinkingKey = @LinkingKey, Description = @Description WHERE ID = @ID",
                //    u);
            }
        }
        /// <summary>
        /// Gets a group setting from the database
        /// </summary>
        /// <typeparam name="T">The type the setting should be (bool, int, string)</typeparam>
        /// <param name="group">What group the setting comes from</param>
        /// <param name="field">The name of the setting</param>
        /// <param name="db">The database TelegramBotDbContext</param>
        /// <param name="def">The default value for the field</param>
        /// <returns></returns>
        public static bool SetSetting <T>(this TelegramBotGroup group, string field, TelegramBotDbContext db, object def, object value)
        {
            //try
            //{
            //    if (db.Connection.State != ConnectionState.Open)
            //        db.Connection.Open();
            //    //verify settings exist
            //    var columns = new SQLiteCommand("PRAGMA table_info(chatgroup)", db.Connection).ExecuteReader();
            //    var t = default(T);
            //    var type = "BLOB";
            //    if (t == null)
            //        type = "TEXT";
            //    else if (t.GetType() == typeof(int))
            //        type = "INTEGER";
            //    else if (t.GetType() == typeof(bool))
            //        type = "INTEGER";
            //    bool settingExists = false;
            //    while (columns.Read())
            //    {
            //        if (String.Equals(columns[1].ToString(), field))
            //        {
            //            settingExists = true;
            //        }
            //    }
            //    if (!settingExists)
            //    {
            //        new SQLiteCommand($"ALTER TABLE chatgroup ADD COLUMN {field} {type} DEFAULT {(type == "INTEGER" ? def : $"'{def}'")};", db.Connection)
            //            .ExecuteNonQuery();
            //    }

            //    new SQLiteCommand($"UPDATE chatgroup set {field} = {(type == "INTEGER" ? (t != null && t.GetType() == typeof(bool)) ? (bool)value ? "1" : "0" : value : $"'{value}'")} where ID = {group.ID}", db.Connection).ExecuteNonQuery();

            //    return true;
            //}
            //catch
            //{
            //    return false;
            //}
            throw new NotImplementedException();
        }
 public static void Save(this TelegramBotSetting set, TelegramBotDbContext db)
 {
     if (set.ID == null || !ExistsInDb(set, db))
     {
         //need to insert
         //db.ExecuteNonQuery(
         //    "insert into settings (Alias, TelegramBotAPIKey, TelegramDefaultAdminUserId) VALUES (@Alias, @TelegramBotAPIKey, @TelegramDefaultAdminUserId)",
         //    set);
         //set.ID = db.Connection.Query<int>("SELECT ID FROM Settings WHERE Alias = @Alias", set).First();
         db.Settings.Add(set);
         db.SaveChanges();
         set.ID = db.Settings.FirstOrDefault(c => c.Alias == set.Alias).ID;
     }
     else
     {
         //db.ExecuteNonQuery(
         //    "UPDATE settings SET Alias = @Alias, TelegramBotAPIKey = @TelegramBotAPIKey, TelegramDefaultAdminUserId = @TelegramDefaultAdminUserId WHERE ID = @ID",
         //    set);
         db.Settings.Update(set);
         db.SaveChanges();
     }
 }
        /// <summary>
        /// Adds a field to the settings table, if needed
        /// </summary>
        /// <param name="set">the current settings loaded</param>
        /// <param name="db">TelegramBotDbContext of the database</param>
        /// <param name="field">Name of the field you need</param>
        /// <returns>Whether or not the field was missing / was added</returns>
        public static bool AddField(this TelegramBotSetting set, TelegramBotDbContext db, string field)
        {
            //if (db.Connection.State != ConnectionState.Open)
            //    db.Connection.Open();
            ////verify settings exist
            //var columns = new SQLiteCommand("PRAGMA table_info(settings)", db.Connection).ExecuteReader();
            //var settingExists = false;
            //while (columns.Read())
            //{
            //    if (String.Equals(columns[1].ToString(), field))
            //        settingExists = true;
            //}

            //if (!settingExists)
            //{
            //    new SQLiteCommand($"ALTER TABLE settings ADD COLUMN {field} TEXT DEFAULT '';", db.Connection)
            //        .ExecuteNonQuery();
            //    return true;
            //}

            //return false;
            throw new NotImplementedException();
        }
 public static bool ExistsInDb(this TelegramBotUser user, TelegramBotDbContext db)
 {
     return(db.Users.AsNoTracking().Any(i => i.ID == user.ID));
 }
        public static TelegramBotUser GetTarget(this Message message, string args, TelegramBotUser sourceUser, TelegramBotDbContext db)
        {
            if (message == null)
            {
                return(sourceUser);
            }
            if (message?.ReplyToMessage != null)
            {
                var m      = message.ReplyToMessage;
                var userid = m.ForwardFrom?.Id ?? m.From.Id;
                return(db.Users.AsNoTracking().FirstOrDefault(x => x.UserId == userid) ?? sourceUser);
            }
            if (String.IsNullOrWhiteSpace(args))
            {
                return(sourceUser);
            }
            //check for a user mention
            var mention     = message?.Entities.FirstOrDefault(x => x.Type == MessageEntityType.Mention);
            var textmention = message?.Entities.FirstOrDefault(x => x.Type == MessageEntityType.TextMention);
            var id          = 0;
            var username    = "";

            if (mention != null)
            {
                username = message.Text.Substring(mention.Offset + 1, mention.Length - 1);
            }
            else if (textmention != null)
            {
                id = textmention.User.Id;
            }
            TelegramBotUser result = null;

            if (!String.IsNullOrEmpty(username))
            {
                result = db.Users.AsNoTracking().FirstOrDefault(
                    x => x.UserName.ToUpper() == username.ToUpper());
            }
            else if (id != 0)
            {
                result = db.Users.AsNoTracking().FirstOrDefault(x => x.UserId == id);
            }
            else
            {
                result = db.Users.AsNoTracking().ToList().FirstOrDefault(
                    x =>
                    String.Equals(x.UserId.ToString(), args, StringComparison.InvariantCultureIgnoreCase) ||
                    String.Equals(x.UserName, args.Replace("@", ""), StringComparison.InvariantCultureIgnoreCase));
            }
            return(result ?? sourceUser);
        }
 public static bool ExistsInDb(this TelegramBotGroup group, TelegramBotDbContext db)
 {
     return(db.Groups.Any(c => c.ID == group.ID));
 }
 public static void RemoveFromDb(this TelegramBotGroup group, TelegramBotDbContext db)
 {
     db.Groups.Remove(group);
     db.SaveChanges();
 }
 public static void RemoveFromDb(this TelegramBotUser user, TelegramBotDbContext db)
 {
     db.Users.Remove(user);
     db.SaveChanges();
 }
 public static bool ExistsInDb(this TelegramBotSetting set, TelegramBotDbContext db)
 {
     return(db.Settings.Any(c => c.ID == set.ID));
 }
 public static void RemoveFromDb(this TelegramBotSetting set, TelegramBotDbContext db)
 {
     db.Settings.Remove(set);
     db.SaveChanges();
 }