diff --git a/.env.sample b/.env.sample index fe1cd5d..fa412e3 100644 --- a/.env.sample +++ b/.env.sample @@ -1,9 +1,11 @@ # Database +DATABASE=sqlite DB_NAME=abcd DB_USER=abcd DB_PWD="abcd!#$" DB_HOST=localhost DB_PORT=5432 +DB_FILE="sample.sqlite" # Keys WORDBOT_TOKEN=0000000000:abcdefghij_kl_mn-op_qrst \ No newline at end of file diff --git a/word_bot.py b/word_bot.py index 6dfab6f..db1f60d 100644 --- a/word_bot.py +++ b/word_bot.py @@ -9,6 +9,7 @@ import logging import os import psycopg2 import requests +import sqlite3 import telegram import telegram.ext import time @@ -21,21 +22,28 @@ from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, Callb load_dotenv() -logging.basicConfig(filename='logs/word_bot.log', filemode='a', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', +log_file_name = f"logs/wordbot_log_{datetime.date.today().strftime('%Y_%m_%d')}.log" +logging.basicConfig(filename=log_file_name, filemode='a', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.DEBUG) logger = logging.getLogger() def connect_db(): - pgcon = psycopg2.connect( - database=os.getenv("DB_NAME"), - user=os.getenv("DB_USER"), - password=os.getenv("DB_PWD"), - host=os.getenv("DB_HOST"), - port=os.getenv("DB_PORT") - ) + database = os.getenv('DATABASE') + if database == 'postgres': + con = psycopg2.connect( + database=os.getenv("DB_NAME"), + user=os.getenv("DB_USER"), + password=os.getenv("DB_PWD"), + host=os.getenv("DB_HOST"), + port=os.getenv("DB_PORT") + ) + print("connected to postgres") + elif database == 'sqlite': + con = sqlite3.connect(os.getenv('DB_FILE')) + print("connected to sqlite") - return pgcon + return con def start( @@ -94,14 +102,14 @@ def word_def( Sends the message """ - pgcon = connect_db() - if pgcon is None: + con = connect_db() + if con is None: print("Db connection failed") word_query = 'select * from gre_words order by random() limit 1' while True: - cur = pgcon.cursor() + cur = con.cursor() cur.execute(word_query) row = cur.fetchall() selected_word = row[0][1].lower() diff --git a/wordbot.sqlite b/wordbot.sqlite new file mode 100644 index 0000000..5214eff Binary files /dev/null and b/wordbot.sqlite differ