Browse Source

added support for sqlite database

master
Gourav Kumar 11 months ago
parent
commit
8cf97d2475
  1. 2
      .env.sample
  2. 34
      word_bot.py
  3. BIN
      wordbot.sqlite

2
.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

34
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")
)
return pgcon
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 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()

BIN
wordbot.sqlite

Binary file not shown.
Loading…
Cancel
Save