Small problem with next step handler. Changed db with todays_product bool
This commit is contained in:
parent
9bdd4a9b71
commit
1da29dab81
@ -26,6 +26,7 @@ import pandas
|
|||||||
|
|
||||||
from db import User, session, Product, Score
|
from db import User, session, Product, Score
|
||||||
from fetcher import *
|
from fetcher import *
|
||||||
|
import helper_functions as hf
|
||||||
|
|
||||||
|
|
||||||
load_dotenv(dotenv_path='.env') # load environment variables
|
load_dotenv(dotenv_path='.env') # load environment variables
|
||||||
@ -486,12 +487,22 @@ def time_in_range(start, end, current):
|
|||||||
"""Returns whether current is in the range [start, end]""" # docstring is missing!!!!!
|
"""Returns whether current is in the range [start, end]""" # docstring is missing!!!!!
|
||||||
return start <= current <= end
|
return start <= current <= end
|
||||||
|
|
||||||
|
def find_todays_product_from_db():
|
||||||
|
"""Find todays product from db based on todays_product
|
||||||
|
"""
|
||||||
|
product = None
|
||||||
|
for element in session.query(Product).all():
|
||||||
|
if element.todays_product:
|
||||||
|
product = element
|
||||||
|
break
|
||||||
|
return product
|
||||||
|
|
||||||
@bot.message_handler(commands=['daily', 'Daily'])
|
@bot.message_handler(commands=['daily', 'Daily'])
|
||||||
def daily_message(message):
|
def daily_message(message):
|
||||||
"""change user name
|
"""send daily challenge
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
message (Message): Message from telegram user, here /changename
|
message (Message): Message from telegram user, here /daily
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
None: None
|
None: None
|
||||||
@ -528,6 +539,35 @@ def daily_message(message):
|
|||||||
iteration-=1
|
iteration-=1
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
product_for_today=find_todays_product_from_db()
|
||||||
|
bot.send_message(chat_id=user_id, text=str(hf.make_markdown_proof(product_for_today.image_link)), parse_mode="MARKDOWNV2")
|
||||||
|
start_time = time.time()
|
||||||
|
bot.register_next_step_handler((message,start_time), get_user_guess)
|
||||||
|
|
||||||
|
def get_time_difference(start_time, end_time):
|
||||||
|
"""Get time difference
|
||||||
|
"""
|
||||||
|
return end_time - start_time
|
||||||
|
|
||||||
|
def get_user_guess(message, start_time):
|
||||||
|
"""Get users guess after typing /daily
|
||||||
|
|
||||||
|
Args:
|
||||||
|
message (Message): Message element to react to. In this case next step after /daily
|
||||||
|
start_time (time.time): Time the user got the image
|
||||||
|
"""
|
||||||
|
end_time = time.time()
|
||||||
|
user_id = int(message.from_user.id)
|
||||||
|
user_guess = str(message.text)
|
||||||
|
if get_time_difference(start_time, end_time) > 35:
|
||||||
|
bot.send_message(chat_id=user_id, text="You took too long to guess.\n"
|
||||||
|
"No more tries today.")
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
message_text=f"You guessed {user_guess}€"
|
||||||
|
bot.send_message(chat_id=user_id, text = message_text)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['addproduct', 'Addproduct'])
|
@bot.message_handler(commands=['addproduct', 'Addproduct'])
|
||||||
|
@ -9,14 +9,14 @@ __license__ = "None"
|
|||||||
|
|
||||||
import time
|
import time
|
||||||
import sys
|
import sys
|
||||||
from apscheduler.schedulers.background import BackgroundScheduler
|
|
||||||
import pandas
|
import pandas
|
||||||
import random
|
import random
|
||||||
|
from apscheduler.schedulers.background import BackgroundScheduler
|
||||||
|
|
||||||
|
|
||||||
from bot import bot
|
from bot import bot
|
||||||
from db import User, session, Product
|
from db import User, session, Product
|
||||||
|
|
||||||
|
|
||||||
CHALLENGE_READY = "0 8 * * *"
|
CHALLENGE_READY = "0 8 * * *"
|
||||||
CHALLENGE_OVER = "0 22 * * *"
|
CHALLENGE_OVER = "0 22 * * *"
|
||||||
GET_NEW_PRODUCT = "0 0 * * *"
|
GET_NEW_PRODUCT = "0 0 * * *"
|
||||||
@ -52,7 +52,7 @@ def start_challenges():
|
|||||||
,args = ("over", ))
|
,args = ("over", ))
|
||||||
|
|
||||||
# Get new product
|
# Get new product
|
||||||
my_scheduler.add_job(get_todays_product, 'cron'\
|
my_scheduler.add_job(set_todays_product, 'cron'\
|
||||||
,day_of_week = new_product_split[4] \
|
,day_of_week = new_product_split[4] \
|
||||||
, hour= new_product_split[1] \
|
, hour= new_product_split[1] \
|
||||||
, minute = new_product_split[0]\
|
, minute = new_product_split[0]\
|
||||||
@ -65,20 +65,42 @@ def start_challenges():
|
|||||||
my_scheduler.shutdown()
|
my_scheduler.shutdown()
|
||||||
start_challenges()
|
start_challenges()
|
||||||
|
|
||||||
def get_todays_product():
|
|
||||||
|
def set_todays_product():
|
||||||
"""Get a random product from the database
|
"""Get a random product from the database
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Product: random product
|
Product: random product
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Find old product and set todays_product false
|
||||||
|
|
||||||
products = []
|
products = []
|
||||||
for element in session.query(Product).all():
|
for element in session.query(Product).all():
|
||||||
products.append(element)
|
if element.todays_product:
|
||||||
|
element.todays_product = False
|
||||||
|
session.commit()
|
||||||
|
products.append(element)
|
||||||
|
else:
|
||||||
|
products.append(element)
|
||||||
|
|
||||||
# choose random product
|
# choose random product
|
||||||
random_product = products[int(len(products) * random.random())]
|
random_product = products[int(len(products) * random.random())]
|
||||||
|
|
||||||
return random_product
|
# find product_id in db and delete element
|
||||||
|
session.query(Product).filter(Product.product_id == random_product.product_id).delete()
|
||||||
|
|
||||||
|
# insert with todays_product True
|
||||||
|
product=Product(
|
||||||
|
product_id=random_product.product_id,
|
||||||
|
price=random_product.price,
|
||||||
|
image_link=random_product.image_link,
|
||||||
|
title=random_product.title,
|
||||||
|
todays_product=True
|
||||||
|
)
|
||||||
|
|
||||||
|
session.add(product)
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
|
||||||
def send_current_event(str_event):
|
def send_current_event(str_event):
|
||||||
@ -102,9 +124,7 @@ def send_current_event(str_event):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
send_current_event("start")
|
set_todays_product()
|
||||||
time.sleep(10)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
start_challenges()
|
start_challenges()
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
@ -49,10 +49,11 @@ class Product(Base):
|
|||||||
title = Column(String(5000))
|
title = Column(String(5000))
|
||||||
description = Column(String(5000))
|
description = Column(String(5000))
|
||||||
date = Column(DateTime)
|
date = Column(DateTime)
|
||||||
|
todays_product = Column(Boolean)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<Product(product_id='%s', price='%s', image_link='%s', title='%s', description='%s', date='%s')>" % (
|
return "<Product(product_id='%s', price='%s', image_link='%s', title='%s', description='%s', date='%s', todays_product='%s')>" % (
|
||||||
self.product_id, self.price, self.image_link, self.title, self.description, self.date)
|
self.product_id, self.price, self.image_link, self.title, self.description, self.date, self.todays_product)
|
||||||
|
|
||||||
|
|
||||||
# Create all tables by issuing CREATE TABLE commands to the DB.
|
# Create all tables by issuing CREATE TABLE commands to the DB.
|
||||||
|
70
source/helper_functions.py
Normal file
70
source/helper_functions.py
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
"""
|
||||||
|
script for helper functions for bot related stuff
|
||||||
|
"""
|
||||||
|
__author__ = "Florian Kellermann, Linus Eickhoff, Florian Kaiser"
|
||||||
|
__date__ = "28.05.2022"
|
||||||
|
__version__ = "1.0.0"
|
||||||
|
__license__ = "None"
|
||||||
|
|
||||||
|
|
||||||
|
def contains_markdownv1_symbols(text):
|
||||||
|
""" checks if text contains markdown symbols
|
||||||
|
:type text: string
|
||||||
|
|
||||||
|
:param text: text to check
|
||||||
|
|
||||||
|
:return: true if text contains markdown symbols
|
||||||
|
|
||||||
|
:rtype: bool
|
||||||
|
"""
|
||||||
|
if text.find("_") != -1 or text.find("*") != -1 or text.find("`") != -1: # check if text contains relevant markdown symbols
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def make_markdown_proof(text): # used to avoid errors related to markdown parsemode for telegram messaging
|
||||||
|
""" makes text markdown proof
|
||||||
|
:type text: string
|
||||||
|
|
||||||
|
:param text: text to make markdown proof
|
||||||
|
|
||||||
|
:return: markdown proof text
|
||||||
|
|
||||||
|
:rtype: string
|
||||||
|
"""
|
||||||
|
text = str(text)
|
||||||
|
|
||||||
|
text = text.replace("_", "\\_") # replace _ with \_ because \ is used as escape character in markdown, double escape is needed because \ is also a escape character in strings
|
||||||
|
text = text.replace("*", "\\*")
|
||||||
|
text = text.replace("`", "\\`")
|
||||||
|
text = text.replace("[", "\\[")
|
||||||
|
text = text.replace("]", "\\]")
|
||||||
|
text = text.replace("(", "\\(")
|
||||||
|
text = text.replace(")", "\\)")
|
||||||
|
text = text.replace("#", "\\#")
|
||||||
|
text = text.replace("+", "\\+")
|
||||||
|
text = text.replace("-", "\\-")
|
||||||
|
text = text.replace("!", "\\!")
|
||||||
|
text = text.replace(".", "\\.")
|
||||||
|
text = text.replace("?", "\\?")
|
||||||
|
text = text.replace("/", "\\/")
|
||||||
|
text = text.replace("~", "\\~")
|
||||||
|
text = text.replace("|", "\\|")
|
||||||
|
text = text.replace("<", "\\<")
|
||||||
|
text = text.replace(">", "\\>")
|
||||||
|
text = text.replace("&", "\\&")
|
||||||
|
text = text.replace("^", "\\^")
|
||||||
|
text = text.replace("$", "\\$")
|
||||||
|
text = text.replace("%", "\\%")
|
||||||
|
text = text.replace("=", "\\=")
|
||||||
|
text = text.replace("@", "\\@")
|
||||||
|
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print("this is a module for helper functions for the bot and should not be run directly")
|
||||||
|
print(make_markdown_proof("_test_"))
|
||||||
|
text = make_markdown_proof("_test_")
|
||||||
|
print(f"{text}")
|
Loading…
Reference in New Issue
Block a user