|
@ -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") |
|
|
return pgcon |
|
|
) |
|
|
|
|
|
print("connected to postgres") |
|
|
|
|
|
elif database == 'sqlite': |
|
|
|
|
|
con = sqlite3.connect(os.getenv('DB_FILE')) |
|
|
|
|
|
print("connected to sqlite") |
|
|
|
|
|
|
|
|
|
|
|
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() |
|
|