added support for sqlite database
This commit is contained in:
parent
5260e4fbf5
commit
8cf97d2475
@ -1,9 +1,11 @@
|
|||||||
# Database
|
# Database
|
||||||
|
DATABASE=sqlite
|
||||||
DB_NAME=abcd
|
DB_NAME=abcd
|
||||||
DB_USER=abcd
|
DB_USER=abcd
|
||||||
DB_PWD="abcd!#$"
|
DB_PWD="abcd!#$"
|
||||||
DB_HOST=localhost
|
DB_HOST=localhost
|
||||||
DB_PORT=5432
|
DB_PORT=5432
|
||||||
|
DB_FILE="sample.sqlite"
|
||||||
|
|
||||||
# Keys
|
# Keys
|
||||||
WORDBOT_TOKEN=0000000000:abcdefghij_kl_mn-op_qrst
|
WORDBOT_TOKEN=0000000000:abcdefghij_kl_mn-op_qrst
|
32
word_bot.py
32
word_bot.py
@ -9,6 +9,7 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import requests
|
import requests
|
||||||
|
import sqlite3
|
||||||
import telegram
|
import telegram
|
||||||
import telegram.ext
|
import telegram.ext
|
||||||
import time
|
import time
|
||||||
@ -21,21 +22,28 @@ from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, Callb
|
|||||||
|
|
||||||
load_dotenv()
|
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)
|
level=logging.DEBUG)
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
|
|
||||||
|
|
||||||
def connect_db():
|
def connect_db():
|
||||||
pgcon = psycopg2.connect(
|
database = os.getenv('DATABASE')
|
||||||
database=os.getenv("DB_NAME"),
|
if database == 'postgres':
|
||||||
user=os.getenv("DB_USER"),
|
con = psycopg2.connect(
|
||||||
password=os.getenv("DB_PWD"),
|
database=os.getenv("DB_NAME"),
|
||||||
host=os.getenv("DB_HOST"),
|
user=os.getenv("DB_USER"),
|
||||||
port=os.getenv("DB_PORT")
|
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(
|
def start(
|
||||||
@ -94,14 +102,14 @@ def word_def(
|
|||||||
Sends the message
|
Sends the message
|
||||||
"""
|
"""
|
||||||
|
|
||||||
pgcon = connect_db()
|
con = connect_db()
|
||||||
if pgcon is None:
|
if con is None:
|
||||||
print("Db connection failed")
|
print("Db connection failed")
|
||||||
|
|
||||||
word_query = 'select * from gre_words order by random() limit 1'
|
word_query = 'select * from gre_words order by random() limit 1'
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
cur = pgcon.cursor()
|
cur = con.cursor()
|
||||||
cur.execute(word_query)
|
cur.execute(word_query)
|
||||||
row = cur.fetchall()
|
row = cur.fetchall()
|
||||||
selected_word = row[0][1].lower()
|
selected_word = row[0][1].lower()
|
||||||
|
BIN
wordbot.sqlite
Normal file
BIN
wordbot.sqlite
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user