コード例 #1
0
        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();
        }
コード例 #2
0
        /// <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();
        }
コード例 #3
0
        /// <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 TelegramBotWrapperWithDb(ITelegramBotOptions options, IDbContextFactory <TDbContext> contextFactory)
        {
            DbContextFactory = contextFactory;

            Options = options;
            if (Options.InMemoryDb)
            {
                using (var db = DbContextFactory.CreateDbContext())
                {
                    db.Database.EnsureCreated();
                    db.Database.Migrate();
                }
            }
            if (!String.IsNullOrEmpty(options.Directory))
            {
                RootDirectory = options.Directory;
            }

            if (!Directory.Exists(Path.Combine(RootDirectory)))
            {
                Directory.CreateDirectory(Path.Combine(RootDirectory));
            }
            Log = new TelegramBotLogger(Path.Combine(RootDirectory, "Logs-" + Options.Alias));
            var setting = new TelegramBotSetting()
            {
                Alias = options.Alias, TelegramDefaultAdminUserId = Options.AdminId, TelegramBotAPIKey = Options.Key
            };

            LoadedSetting = setting;

            try
            {
                using (Db)
                {
                    Db.Database.EnsureCreated();

                    Db.SaveChanges();
                }
            }
            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);
        }
コード例 #4
0
 public static void Save(this TelegramBotSetting set, ITelegramBotDbContext db)
 {
     if (set.Id == 0 || !ExistsInDb(set, db))
     {
         db.TelegramBotSettings.Add(set);
         db.SaveChanges();
         set.Id = db.TelegramBotSettings.FirstOrDefault(c => c.Alias == set.Alias).Id;
     }
     else
     {
         db.TelegramBotSettings.Update(set);
         db.SaveChanges();
     }
 }
コード例 #5
0
        /// <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);
        }
コード例 #6
0
 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();
     }
 }
コード例 #7
0
        /// <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();
        }
コード例 #8
0
 public static bool ExistsInDb(this TelegramBotSetting set, ITelegramBotDbContext db)
 {
     return(db.TelegramBotSettings.Any(c => c.Id == set.Id));
 }
コード例 #9
0
 public static void RemoveFromDb(this TelegramBotSetting set, ITelegramBotDbContext db)
 {
     db.TelegramBotSettings.Remove(set);
     db.SaveChanges();
 }