Change schema. Change functions to match new schema.
add_message_id for the new table. get_callback_id now gets id from message_id table. Removed the null case from add_vote. Creating post no longer adds an entry to poll table. It instead adds an entry to message_ids table via add_message_id function
This commit is contained in:
parent
c464088c57
commit
1a4db8d2c5
27
bot.py
27
bot.py
|
@ -13,20 +13,23 @@ logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - \
|
||||||
|
|
||||||
def get_callback_id(db):
|
def get_callback_id(db):
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
query = ('SELECT max(callback) FROM poll')
|
query = ('SELECT max(callback) FROM message_ids')
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
return int(cursor.fetchone()[0]) + 1
|
return int(cursor.fetchone()[0]) + 1
|
||||||
|
|
||||||
|
|
||||||
|
def add_message_id(db, message_id, callback):
|
||||||
|
cursor = db.cursor()
|
||||||
|
query = ("INSERT INTO message_ids(callback, message_id) values(?,?)")
|
||||||
|
cursor.execute(query, (callback, message_id))
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
|
||||||
def add_vote(db, callback_id, user=None):
|
def add_vote(db, callback_id, user=None):
|
||||||
if user is not None:
|
user_id = user.id
|
||||||
user_id = user.id
|
name = str(user.first_name)
|
||||||
name = str(user.first_name)
|
if user.last_name:
|
||||||
if user.last_name:
|
name += " " + str(user.last_name)
|
||||||
name += " " + str(user.last_name)
|
|
||||||
else:
|
|
||||||
user_id = 0
|
|
||||||
name = ""
|
|
||||||
|
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
query = ("INSERT INTO poll(callback, user_id, name) values(?,?,?)")
|
query = ("INSERT INTO poll(callback, user_id, name) values(?,?,?)")
|
||||||
|
@ -38,13 +41,13 @@ def post(bot, update, db):
|
||||||
chat_id = update.message.chat_id
|
chat_id = update.message.chat_id
|
||||||
if chat_id in config.allowed_users:
|
if chat_id in config.allowed_users:
|
||||||
callback_id = get_callback_id(db)
|
callback_id = get_callback_id(db)
|
||||||
add_vote(db, callback_id)
|
|
||||||
photo = update.message.photo[0]
|
photo = update.message.photo[0]
|
||||||
file_id = photo.file_id
|
file_id = photo.file_id
|
||||||
keyboard = InlineKeyboardMarkup([[InlineKeyboardButton(text="❤️",
|
keyboard = InlineKeyboardMarkup([[InlineKeyboardButton(text="❤️",
|
||||||
callback_data=callback_id)]])
|
callback_data=callback_id)]])
|
||||||
bot.send_photo(chat_id=config.channel_id, photo=file_id,
|
reply = bot.send_photo(chat_id=config.channel_id, photo=file_id,
|
||||||
reply_markup=keyboard)
|
reply_markup=keyboard)
|
||||||
|
add_message_id(db, reply.message_id, callback_id)
|
||||||
else:
|
else:
|
||||||
bot.send_message(chat_id=chat_id, text="Not Authorized")
|
bot.send_message(chat_id=chat_id, text="Not Authorized")
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
CREATE TABLE IF NOT EXISTS poll(callback int NOT NULL, user_id int, name varchar, UNIQUE(callback, user_id));
|
CREATE TABLE IF NOT EXISTS poll(callback int NOT NULL, user_id int, name varchar, UNIQUE(callback, user_id));
|
||||||
INSERT INTO poll values(0, 0, NULL);
|
CREATE TABLE IF NOT EXISTS message_ids(callback int PRIMARY KEY, message_id int NOT NULL UNIQUE);
|
||||||
|
INSERT INTO message_ids values(0, 0);
|
||||||
|
|
Loading…
Reference in New Issue