1
Fork 0
mirror of https://github.com/Steffo99/greed.git synced 2024-12-18 01:34:22 +00:00

Code refactor (#178)

Co-authored-by: Stefano Pigozzi <me@steffo.eu>
This commit is contained in:
oleg20111511 2022-05-20 03:55:12 +03:00 committed by GitHub
parent 0bd8a0c994
commit 55e14b10d5
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 43 additions and 18 deletions

View file

@ -130,17 +130,10 @@ class Product(TableDeclarativeBase):
def send_as_message(self, w: "worker.Worker", chat_id: int) -> dict: def send_as_message(self, w: "worker.Worker", chat_id: int) -> dict:
"""Send a message containing the product data.""" """Send a message containing the product data."""
if self.image is None: if self.image is None:
r = requests.get(f"https://api.telegram.org/bot{w.cfg['Telegram']['token']}/sendMessage", msg = w.bot.send_message(chat_id, self.text(w))
params={"chat_id": chat_id,
"text": self.text(w),
"parse_mode": "HTML"})
else: else:
r = requests.post(f"https://api.telegram.org/bot{w.cfg['Telegram']['token']}/sendPhoto", msg = w.bot.send_photo(chat_id, self.image, caption=self.text(w))
files={"photo": self.image}, return msg.to_dict()
params={"chat_id": chat_id,
"caption": self.text(w),
"parse_mode": "HTML"})
return r.json()
def set_image(self, file: telegram.File): def set_image(self, file: telegram.File):
"""Download an image from Telegram and store it in the image column. """Download an image from Telegram and store it in the image column.

View file

@ -67,6 +67,10 @@ def factory(cfg: nuconfig.NuConfig):
# All messages are sent in HTML parse mode # All messages are sent in HTML parse mode
return self.bot.send_message(parse_mode="HTML", *args, **kwargs) return self.bot.send_message(parse_mode="HTML", *args, **kwargs)
@catch_telegram_errors
def send_photo(self, *args, **kwargs):
return self.bot.send_photo(parse_mode="HTML", *args, **kwargs)
@catch_telegram_errors @catch_telegram_errors
def edit_message_text(self, *args, **kwargs): def edit_message_text(self, *args, **kwargs):
# All messages are sent in HTML parse mode # All messages are sent in HTML parse mode

View file

@ -243,6 +243,9 @@ text_completed = "completed"
# Text: refunded order # Text: refunded order
text_refunded = "refunded" text_refunded = "refunded"
# Text: product not for sale
text_not_for_sale = "Not for sale"
# Add product: name? # Add product: name?
ask_product_name = "What should the product name be?" ask_product_name = "What should the product name be?"

View file

@ -242,6 +242,9 @@ text_completed = "Completada"
# Text: refunded order # Text: refunded order
text_refunded = "Reembolsada" text_refunded = "Reembolsada"
# Text: product not for sale
text_not_for_sale = "No para la venta"
# Add product: name? # Add product: name?
ask_product_name = "¿Cuál debería ser el nombre del producto?" ask_product_name = "¿Cuál debería ser el nombre del producto?"

View file

@ -239,6 +239,9 @@ text_completed = "הסתיים"
# Text: refunded order # Text: refunded order
text_refunded = "כסף הוחזר" text_refunded = "כסף הוחזר"
# Text: product not for sale
text_not_for_sale = "לא למכירה"
# Add product: name? # Add product: name?
ask_product_name = "איזה שם תרצה לתת למוצר?" ask_product_name = "איזה שם תרצה לתת למוצר?"

View file

@ -244,6 +244,9 @@ text_completed = "completato"
# Text: refunded order # Text: refunded order
text_refunded = "rimborsato" text_refunded = "rimborsato"
# Text: product not for sale
text_not_for_sale = "Non in vendita"
# Add product: name? # Add product: name?
ask_product_name = "Come si deve chiamare il prodotto?" ask_product_name = "Come si deve chiamare il prodotto?"

View file

@ -243,6 +243,9 @@ text_completed = "completo"
# Text: refunded order # Text: refunded order
text_refunded = "devolvido" text_refunded = "devolvido"
# Text: product not for sale
text_not_for_sale = "Não está a venda"
# Add product: name? # Add product: name?
ask_product_name = "Qual deve ser o nome do produto?" ask_product_name = "Qual deve ser o nome do produto?"

View file

@ -238,6 +238,9 @@ text_completed = "выполнен"
# Text: refunded order # Text: refunded order
text_refunded = "возмещен" text_refunded = "возмещен"
# Text: product not for sale
text_not_for_sale = "Не продаётся"
# Add product: name? # Add product: name?
ask_product_name = "Как назовем продукт?" ask_product_name = "Как назовем продукт?"

View file

@ -239,6 +239,9 @@ text_completed = "завершено"
# Text: refunded order # Text: refunded order
text_refunded = "повернуто" text_refunded = "повернуто"
# Text: product not for sale
text_not_for_sale = "Не продається"
# Add product: name? # Add product: name?
ask_product_name = "Як назвати продукт?" ask_product_name = "Як назвати продукт?"

View file

@ -244,6 +244,9 @@ text_completed = "已完成"
# Text: refunded order # Text: refunded order
text_refunded = "已退还" text_refunded = "已退还"
# Text: product not for sale
text_not_for_sale = "不出售"
# Add product: name? # Add product: name?
ask_product_name = "产品名称应为?" ask_product_name = "产品名称应为?"

View file

@ -507,7 +507,7 @@ class Worker(threading.Thread):
# Send the message without the keyboard to get the message id # Send the message without the keyboard to get the message id
message = product.send_as_message(w=self, chat_id=self.chat.id) message = product.send_as_message(w=self, chat_id=self.chat.id)
# Add the product to the cart # Add the product to the cart
cart[message['result']['message_id']] = [product, 0] cart[message['message_id']] = [product, 0]
# Create the inline keyboard to add the product to the cart # Create the inline keyboard to add the product to the cart
inline_keyboard = telegram.InlineKeyboardMarkup( inline_keyboard = telegram.InlineKeyboardMarkup(
[[telegram.InlineKeyboardButton(self.loc.get("menu_add_to_cart"), callback_data="cart_add")]] [[telegram.InlineKeyboardButton(self.loc.get("menu_add_to_cart"), callback_data="cart_add")]]
@ -515,12 +515,12 @@ class Worker(threading.Thread):
# Edit the sent message and add the inline keyboard # Edit the sent message and add the inline keyboard
if product.image is None: if product.image is None:
self.bot.edit_message_text(chat_id=self.chat.id, self.bot.edit_message_text(chat_id=self.chat.id,
message_id=message['result']['message_id'], message_id=message['message_id'],
text=product.text(w=self), text=product.text(w=self),
reply_markup=inline_keyboard) reply_markup=inline_keyboard)
else: else:
self.bot.edit_message_caption(chat_id=self.chat.id, self.bot.edit_message_caption(chat_id=self.chat.id,
message_id=message['result']['message_id'], message_id=message['message_id'],
caption=product.text(w=self), caption=product.text(w=self),
reply_markup=inline_keyboard) reply_markup=inline_keyboard)
# Create the keyboard with the cancel button # Create the keyboard with the cancel button
@ -1024,11 +1024,15 @@ class Worker(threading.Thread):
self.loc.get("ask_product_price")) self.loc.get("ask_product_price"))
# Display the current name if you're editing an existing product # Display the current name if you're editing an existing product
if product: if product:
self.bot.send_message(self.chat.id, if product.price is not None:
self.loc.get("edit_current_value", value_text = str(self.Price(product.price))
value=(str(self.Price(product.price)) else:
if product.price is not None else 'Non in vendita')), value_text = self.loc.get("text_not_for_sale")
reply_markup=cancel) self.bot.send_message(
self.chat.id,
self.loc.get("edit_current_value", value=value_text),
reply_markup=cancel
)
# Wait for an answer # Wait for an answer
price = self.__wait_for_regex(r"([0-9]+(?:[.,][0-9]{1,2})?|[Xx])", price = self.__wait_for_regex(r"([0-9]+(?:[.,][0-9]{1,2})?|[Xx])",
cancellable=True) cancellable=True)