Almost working, some problems still with defining todays product

This commit is contained in:
Kellermann 2022-05-27 23:36:23 +02:00
parent 5989d1d39b
commit 04862a0a1a
2 changed files with 78 additions and 84 deletions

View File

@ -131,8 +131,7 @@ def send_help(message):
"/gameinfo get game info\n"
"/scoreboard get scoreboard\n"
"/changename change your name\n"
"/challenge get todays challenge\n"
"/daily open and do challenge for today\n"
"/daily get todays challenge\n"
"/setAdmin set admin status of user (ADMIN)\n"
"/addproduct add product for further challenges (ADMIN)\n"
"/users get all users (ADMIN)\n")
@ -162,7 +161,7 @@ def send_gameinfo(message):
"Start by setting your name with /changename\n"
"You can get a new challenge every day.\n"
"You are informed when a new challenge is available.\n"
"To see the challenge type /challenge\n"
"To see the challenge type /daily\n"
"To guess the price type /guess\n"
"At 22:00 pm the scores and answer will be shown\n")
bot.reply_to(message, gameinfo_message)
@ -278,6 +277,7 @@ def set_admin(message):
user_id = message.from_user.id
try:
user = session.query(User).filter(User.telegram_id==user_id).first()
if not user.admin: # admin is a boolean
@ -288,6 +288,7 @@ def set_admin(message):
bot.reply_to(message, "Type the telegram_id and boolean of admin attribute like <telegram_id> <value>")
bot.register_next_step_handler(message, set_admin_handler)
except sqlalchemy.exc.IntegrityError:
session.rollback()
bot.reply_to(message, "Something went wrong.")
@ -418,26 +419,6 @@ def send_scoreboard(message):
bot.reply_to(message, str_weekly_board, parse_mode='MARKDOWN')
@bot.message_handler(commands=['challenge', 'Challenge'])
def send_challenge(message):
"""send challenge to user
Args:
message (Message): Message from telegram user, here /challenge
Returns:
None: None
Raises:
None: None
"""
bot.reply_to(message, "Challenge not implemented yet")
@bot.message_handler(commands=['changename', 'Changename'])
def change_name(message):
"""change user name
@ -548,6 +529,7 @@ def daily_message(message):
time.sleep(1)
@bot.message_handler(commands=['addproduct', 'Addproduct'])
def add_product(message):
"""Add product to database
@ -671,32 +653,6 @@ def main_loop():
bot.infinity_polling()
while 1:
product_split = UPDATE_PRODUCT.split(" ")
my_scheduler = BackgroundScheduler()
my_scheduler.add_job(get_todays_product, 'cron'\
,day_of_week = product_split[4]\
,hour= product_split[1]\
,minute = product_split[0]\
,month= product_split[3]\
,day=product_split[2])
my_scheduler.start()
print(f"Scheduler started for {product_split}")
time.sleep(10000)
print("Restarting scheduler")
my_scheduler.shutdown()
def get_todays_product():
"""Setting product for this day
"""
print(pandas.DataFrame(session.query(Product.price,Product.image_link).all()))
if __name__ == '__main__':
try:

View File

@ -11,6 +11,7 @@ import time
import sys
from apscheduler.schedulers.background import BackgroundScheduler
import pandas
import random
from bot import bot
from db import User, session, Product
@ -18,6 +19,7 @@ from db import User, session, Product
CHALLENGE_READY = "0 8 * * *"
CHALLENGE_OVER = "0 22 * * *"
GET_NEW_PRODUCT = "0 0 * * *"
def start_challenges():
"""Start the daily challenges, read the crontag codes and send messages
@ -26,22 +28,57 @@ def start_challenges():
over_split = CHALLENGE_OVER.split(" ")
new_product_split = GET_NEW_PRODUCT.split(" ")
my_scheduler = BackgroundScheduler()
# Sending the current event at the given crontags
# Challenge ready
my_scheduler.add_job(send_current_event, 'cron'\
,day_of_week = ready_split[4] , hour= ready_split[1] , minute = ready_split[0], month= ready_split[3] , day=ready_split[2]\
, day_of_week = ready_split[4] \
, hour= ready_split[1] \
, minute = ready_split[0]\
, month= ready_split[3] \
, day=ready_split[2]\
,args = ("start", ))
# Challenge over
my_scheduler.add_job(send_current_event, 'cron'\
,day_of_week = over_split[4] , hour= over_split[1] , minute = over_split[0], month= over_split[3] , day=over_split[2]\
, day_of_week = over_split[4] \
, hour= over_split[1] \
, minute = over_split[0]\
, month= over_split[3] \
, day=over_split[2]\
,args = ("over", ))
# Get new product
my_scheduler.add_job(get_todays_product, 'cron'\
,day_of_week = new_product_split[4] \
, hour= new_product_split[1] \
, minute = new_product_split[0]\
, month= new_product_split[3]\
, day=new_product_split[2])
my_scheduler.start()
time.sleep( 3600 )
my_scheduler.shutdown()
start_challenges()
def get_todays_product():
"""Get a random product from the database
Returns:
Product: random product
"""
products = []
for element in session.query(Product).all():
products.append(element)
# choose random product
random_product = products[int(len(products) * random.random())]
return random_product
def send_current_event(str_event):
@ -54,22 +91,23 @@ def send_current_event(str_event):
if str_event == "start":
for element in all_users["telegram_id"]:
bot.send_message(chat_id=int(element), text="Todays challenge is available!\nTry /daily to give it a try :)")
bot.send_message(chat_id=int(element), text="Todays challenge is available!\n"\
"Try /daily to give it a try :)")
elif str_event == "over":
for element in all_users["telegram_id"]:
bot.send_message(chat_id=int(element), text="Todays challenge is over!\nCheck the /scoreboard to see the leaderboard!")
bot.send_message(chat_id=int(element), text="Todays challenge is over!\n"\
"Check the /scoreboard to see the leaderboard!")
else:
sys.exit(-1)
if __name__ == "__main__":
send_current_event("start")
time.sleep(10)
try:
test = pandas.DataFrame(session.query(User.telegram_id).all())
for element in test["telegram_id"]:
print(element)
start_challenges()
sys.exit(-1)
except KeyboardInterrupt:
print("Ending")
sys.exit(-1)