From 55e14b10d5803abe825e9b4a2c867220a50e2193 Mon Sep 17 00:00:00 2001 From: oleg20111511 <47714428+oleg20111511@users.noreply.github.com> Date: Fri, 20 May 2022 03:55:12 +0300 Subject: [PATCH] Code refactor (#178) Co-authored-by: Stefano Pigozzi --- database.py | 13 +++---------- duckbot.py | 4 ++++ strings/en.py | 3 +++ strings/es_mx.py | 3 +++ strings/he.py | 3 +++ strings/it.py | 3 +++ strings/pt_br.py | 3 +++ strings/ru.py | 3 +++ strings/uk.py | 3 +++ strings/zh_cn.py | 3 +++ worker.py | 20 ++++++++++++-------- 11 files changed, 43 insertions(+), 18 deletions(-) diff --git a/database.py b/database.py index 2fab295..34ad5d1 100644 --- a/database.py +++ b/database.py @@ -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. diff --git a/duckbot.py b/duckbot.py index 0593f70..6296f48 100644 --- a/duckbot.py +++ b/duckbot.py @@ -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 diff --git a/strings/en.py b/strings/en.py index 5c29890..9bd6606 100644 --- a/strings/en.py +++ b/strings/en.py @@ -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?" diff --git a/strings/es_mx.py b/strings/es_mx.py index 3c7e260..8f8f60d 100644 --- a/strings/es_mx.py +++ b/strings/es_mx.py @@ -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?" diff --git a/strings/he.py b/strings/he.py index 374a487..8880b41 100644 --- a/strings/he.py +++ b/strings/he.py @@ -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 = "איזה שם תרצה לתת למוצר?" diff --git a/strings/it.py b/strings/it.py index bb33057..d0e2c18 100644 --- a/strings/it.py +++ b/strings/it.py @@ -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?" diff --git a/strings/pt_br.py b/strings/pt_br.py index b9e8ef1..d26d5e2 100644 --- a/strings/pt_br.py +++ b/strings/pt_br.py @@ -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?" diff --git a/strings/ru.py b/strings/ru.py index 801e3e6..c278e92 100644 --- a/strings/ru.py +++ b/strings/ru.py @@ -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 = "Как назовем продукт?" diff --git a/strings/uk.py b/strings/uk.py index 748fd19..dca18c9 100644 --- a/strings/uk.py +++ b/strings/uk.py @@ -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 = "Як назвати продукт?" diff --git a/strings/zh_cn.py b/strings/zh_cn.py index db05b14..1d1ca31 100644 --- a/strings/zh_cn.py +++ b/strings/zh_cn.py @@ -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 = "产品名称应为?" diff --git a/worker.py b/worker.py index bb8d27b..7b2e634 100644 --- a/worker.py +++ b/worker.py @@ -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)