This commit is contained in:
Joan Cano
2023-03-01 22:19:10 +01:00
parent f961bfd3cf
commit 53004cb3d1
10 changed files with 67 additions and 82 deletions

View File

@@ -2,6 +2,7 @@ import json
import os
import threading
import logging
import prettytable
from worker import Worker
from telegram import ForceReply, Update
@@ -21,9 +22,13 @@ logging.basicConfig(
logger = logging.getLogger(__name__)
def parse_json_file():
f = open("args.json")
f = open("products.json")
return json.load(f)
def save_json_file(products):
with open('products.json', 'w') as outfile:
json.dump(products, outfile, indent=2)
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Send a message when the command /help is issued."""
message = "Add with /add product;min_price;max_price"
@@ -32,10 +37,11 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
async def add_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
parsed = update.message.text.split(";")
logging.info(parsed)
if len(parsed) != 3:
message = "You must put the correct number of arguments: /add product;min_price;max_price"
if len(parsed) < 3:
#message = "You must pass the correct number of arguments: /add product;min_price;max_price;latitude(optional);longitude(optional);distance(optional);title_exclude(optional);title_description_exclude(optional)"
message = "You must pass the correct number of arguments: /add product;min_price;max_price"
else:
argument = {"product_name": f"{parsed[0][5:]}", #removes "/add "
product = {"product_name": f"{parsed[0][len('/add '):]}", #removes "/add "
"distance": "0",
"latitude": f"{LATITUDE}",
"longitude": f"{LONGITUDE}",
@@ -45,31 +51,40 @@ async def add_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> Non
"title_keyword_exclude" : [],
"exclude": []
}
p = threading.Thread(target=Worker.run, args=(argument, ))
products = parse_json_file()
products.append(product)
save_json_file(products)
p = threading.Thread(target=Worker.run, args=(product, ))
p.start()
message = f"Added {parsed[0][5:]}"
await update.message.reply_text(message)
async def remove_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text("Help!")
products = parse_json_file()
product_to_remove = update.message.text[len('/remove '):]
for product in products:
if product['product_name'] == product_to_remove:
products.remove(product)
save_json_file(products)
await update.message.reply_text(f"{product_to_remove} removed!")
async def list_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
args = parse_json_file()
product_list = ""
for product in args:
product_list = f"{product_list}\n{product['product_name']};{product['min_price']}-{product['max_price']}"
await update.message.reply_text(product_list)
products = parse_json_file()
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Echo the user message."""
await update.message.reply_text(update.message.text)
table = prettytable.PrettyTable(['Product', 'Min', 'Max'])
table.align['Product'] = 'l'
table.align['Min Price'] = 'r'
table.align['Max Price'] = 'r'
for product in products:
table.add_row([product['product_name'], f"{product['min_price']}", f"{product['max_price']}"])
await update.message.reply_markdown_v2(f'```{table}```')
def main()->None:
args = parse_json_file()
products = parse_json_file()
for argument in args:
logging.info(argument)
p = threading.Thread(target=Worker.run, args=(argument, ))
for product in products:
logging.info(product)
p = threading.Thread(target=Worker.run, args=(product, ))
p.start()
"""Start the bot."""