1
Fork 0
mirror of https://github.com/Steffo99/greed.git synced 2024-11-21 05:24:19 +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:
"""Send a message containing the product data."""
if self.image is None:
r = requests.get(f"https://api.telegram.org/bot{w.cfg['Telegram']['token']}/sendMessage",
params={"chat_id": chat_id,
"text": self.text(w),
"parse_mode": "HTML"})
msg = w.bot.send_message(chat_id, self.text(w))
else:
r = requests.post(f"https://api.telegram.org/bot{w.cfg['Telegram']['token']}/sendPhoto",
files={"photo": self.image},
params={"chat_id": chat_id,
"caption": self.text(w),
"parse_mode": "HTML"})
return r.json()
msg = w.bot.send_photo(chat_id, self.image, caption=self.text(w))
return msg.to_dict()
def set_image(self, file: telegram.File):
"""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
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
def edit_message_text(self, *args, **kwargs):
# All messages are sent in HTML parse mode

View file

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

View file

@ -242,6 +242,9 @@ text_completed = "Completada"
# Text: refunded order
text_refunded = "Reembolsada"
# Text: product not for sale
text_not_for_sale = "No para la venta"
# Add product: name?
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 = "כסף הוחזר"
# Text: product not for sale
text_not_for_sale = "לא למכירה"
# Add product: name?
ask_product_name = "איזה שם תרצה לתת למוצר?"

View file

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

View file

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

View file

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

View file

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

View file

@ -244,6 +244,9 @@ text_completed = "已完成"
# Text: refunded order
text_refunded = "已退还"
# Text: product not for sale
text_not_for_sale = "不出售"
# Add 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
message = product.send_as_message(w=self, chat_id=self.chat.id)
# 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
inline_keyboard = telegram.InlineKeyboardMarkup(
[[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
if product.image is None:
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),
reply_markup=inline_keyboard)
else:
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),
reply_markup=inline_keyboard)
# Create the keyboard with the cancel button
@ -1024,11 +1024,15 @@ class Worker(threading.Thread):
self.loc.get("ask_product_price"))
# Display the current name if you're editing an existing product
if product:
self.bot.send_message(self.chat.id,
self.loc.get("edit_current_value",
value=(str(self.Price(product.price))
if product.price is not None else 'Non in vendita')),
reply_markup=cancel)
if product.price is not None:
value_text = str(self.Price(product.price))
else:
value_text = self.loc.get("text_not_for_sale")
self.bot.send_message(
self.chat.id,
self.loc.get("edit_current_value", value=value_text),
reply_markup=cancel
)
# Wait for an answer
price = self.__wait_for_regex(r"([0-9]+(?:[.,][0-9]{1,2})?|[Xx])",
cancellable=True)