static void Main(string[] args) { try { botConfig = new TBotConfigParser().ParseAPIConf("TBotConf.json"); db = new TBotDataBase(); db.InitDBAsync().Wait(); if (db.UsersCount() == 0) { Console.WriteLine(TBotStrings.TBotNoUsersOnDB); db.InsertUser(botConfig.BotMaster, true); } else { TBotUser admin = db.GetUser(1); if (!admin.Username.Equals(botConfig.BotMaster)) { Console.WriteLine(string.Format(TBotStrings.TBotNotThisBotAdmin, db.GetDBName())); } } listUsers = db.GetUserListAsync().Result; chatBot = factory.Create(ChatterBotType.PANDORABOTS, "f5d922d97e345aa1"); chatBotSession = chatBot.CreateSession(); tBot = new TelegramBotClient(botConfig.TelegramBotAPIKEY); var me = tBot.GetMeAsync().Result; botNick = me.FirstName; Console.Title = botNick; tBot.OnMessage += TBot_OnMessage; tBot.StartReceiving(Array.Empty <UpdateType>()); if (botConfig.ChatGroupID != 0) { Task <bool> callTask = Task.Run(() => SendMsgToTelegram(botConfig.ChatGroupID, string.Format(TBotStrings.TBotGreetingMSG, me.FirstName))); callTask.Wait(); bool greetingSent = callTask.Result; if (greetingSent) { Console.WriteLine("Greetings sent"); } } Console.WriteLine($"I am user {me.Id} and my Username is {me.Username}.\nStart listening for @{me.FirstName}"); Console.ReadLine(); tBot.StopReceiving(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public bool RemoveUser(string userName) { try { using (SQLiteConnection dbConn = new SQLiteConnection(string.Format("Data Source={0};Version=3;", myDB))) { TBotUser user = GetUser(userName); if (user == null) { throw new ExceptionTBotDBUserNotfound(); } if (user.ID == 1) { throw new ExceptionTBotDBRemovingAdmin(); } string sqlCMD = "DELETE from tblTBotUser WHERE USERNAME = @Username"; SQLiteCommand command = new SQLiteCommand(sqlCMD, dbConn); command = new SQLiteCommand(sqlCMD, dbConn); command.ExecuteNonQueryAsync(); dbConn.Open(); command.Parameters.AddWithValue("Username", userName); Int32 rowsAffected = command.ExecuteNonQuery(); if (rowsAffected == 0) { dbConn.Close(); throw new ExceptionTBotDBRemovingUser(); } sqlCMD = "SELECT COUNT(ID) FROM tblTBotUser;"; command = new SQLiteCommand(sqlCMD, dbConn); amountUsers = Convert.ToInt32(command.ExecuteScalar()); dbConn.Clone(); return(true); } } catch (SQLiteException ex) { throw ex; } catch (Exception ex) { throw ex; } }
public TBotUser GetUser(int id) { try { using (SQLiteConnection dbConn = new SQLiteConnection(string.Format("Data Source={0};Version=3;", myDB))) { TBotUser user = null; var userList = new List <TBotUser>(); string sqlCMD = @"select * from tblTBotUser WHERE ID = @ID;"; SQLiteCommand command = new SQLiteCommand(sqlCMD, dbConn); dbConn.Open(); command.Parameters.Add(new SQLiteParameter("@ID") { Value = id }); command.CommandType = System.Data.CommandType.Text; SQLiteDataReader dr; dr = command.ExecuteReader(); while (dr.Read()) { user = new TBotUser(); user.SetID((long)dr["ID"]); user.SetIsUsername((string)dr["Username"]); user.SetIsAdmin(Convert.ToBoolean((int)dr["IsAdmin"])); } dbConn.Close(); return(user); } } catch (SQLiteException ex) { throw ex; } catch (Exception ex) { throw ex; } }
public List <TBotUser> GetUserList() { try { using (SQLiteConnection dbConn = new SQLiteConnection(string.Format("Data Source={0};Version=3;", myDB))) { var userList = new List <TBotUser>(); string sqlCMD = "select * from tblTBotUser;"; SQLiteCommand command = new SQLiteCommand(sqlCMD, dbConn); dbConn.Open(); command = new SQLiteCommand(sqlCMD, dbConn); command.ExecuteNonQueryAsync().Wait(); var dr = command.ExecuteReader(); while (dr.Read()) { TBotUser user = new TBotUser(); user.SetID((long)dr["ID"]); user.SetIsUsername((string)dr["Username"]); user.SetIsAdmin(Convert.ToBoolean((int)dr["IsAdmin"])); userList.Add(user); } dbConn.Close(); return(userList); } } catch (SQLiteException ex) { throw ex; } catch (Exception ex) { throw ex; } }