1
Fork 0
mirror of https://github.com/Steffo99/greed.git synced 2024-11-24 14:54:18 +00:00

Send all messages in parse_mode="HTML"

This commit is contained in:
Steffo 2018-04-19 09:55:36 +02:00
parent 5da9535b97
commit f823d5cc7a
2 changed files with 22 additions and 32 deletions

View file

@ -143,15 +143,18 @@ class DuckBot:
@catch_telegram_errors @catch_telegram_errors
def send_message(self, *args, **kwargs): def send_message(self, *args, **kwargs):
return self.bot.send_message(*args, **kwargs) # All messages are sent in HTML parse mode
return self.bot.send_message(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):
return self.bot.edit_message_text(*args, **kwargs) # All messages are sent in HTML parse mode
return self.bot.edit_message_text(parse_mode="HTML", *args, **kwargs)
@catch_telegram_errors @catch_telegram_errors
def edit_message_caption(self, *args, **kwargs): def edit_message_caption(self, *args, **kwargs):
return self.bot.edit_message_caption(*args, **kwargs) # All messages are sent in HTML parse mode
return self.bot.edit_message_caption(parse_mode="HTML", *args, **kwargs)
@catch_telegram_errors @catch_telegram_errors
def edit_message_reply_markup(self, *args, **kwargs): def edit_message_reply_markup(self, *args, **kwargs):

View file

@ -12,7 +12,6 @@ import re
import utils import utils
import os import os
from html import escape from html import escape
import io
import requests import requests
@ -56,6 +55,7 @@ class ChatWorker(threading.Thread):
else: else:
self.sentry_client = None self.sentry_client = None
# noinspection PyBroadException
def run(self): def run(self):
"""The conversation code.""" """The conversation code."""
# Welcome the user to the bot # Welcome the user to the bot
@ -249,8 +249,7 @@ class ChatWorker(threading.Thread):
# Send the previously created keyboard to the user (ensuring it can be clicked only 1 time) # Send the previously created keyboard to the user (ensuring it can be clicked only 1 time)
self.bot.send_message(self.chat.id, self.bot.send_message(self.chat.id,
strings.conversation_open_user_menu.format(credit=utils.Price(self.user.credit)), strings.conversation_open_user_menu.format(credit=utils.Price(self.user.credit)),
reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True), reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True))
parse_mode="HTML")
# Wait for a reply from the user # Wait for a reply from the user
selection = self.__wait_for_specific_message([strings.menu_order, strings.menu_order_status, selection = self.__wait_for_specific_message([strings.menu_order, strings.menu_order_status,
strings.menu_add_credit, strings.menu_bot_info, strings.menu_add_credit, strings.menu_bot_info,
@ -300,13 +299,11 @@ class ChatWorker(threading.Thread):
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['result']['message_id'],
text=product.text(), text=product.text(),
parse_mode="HTML",
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['result']['message_id'],
caption=product.text(), caption=product.text(),
parse_mode="HTML",
reply_markup=inline_keyboard) reply_markup=inline_keyboard)
# Create the keyboard with the cancel button # Create the keyboard with the cancel button
inline_keyboard = telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton(strings.menu_cancel, inline_keyboard = telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton(strings.menu_cancel,
@ -347,13 +344,11 @@ class ChatWorker(threading.Thread):
self.bot.edit_message_text(chat_id=self.chat.id, self.bot.edit_message_text(chat_id=self.chat.id,
message_id=callback.message.message_id, message_id=callback.message.message_id,
text=product.text(cart_qty=cart[callback.message.message_id][1]), text=product.text(cart_qty=cart[callback.message.message_id][1]),
parse_mode="HTML",
reply_markup=product_inline_keyboard) reply_markup=product_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=callback.message.message_id, message_id=callback.message.message_id,
caption=product.text(cart_qty=cart[callback.message.message_id][1]), caption=product.text(cart_qty=cart[callback.message.message_id][1]),
parse_mode="HTML",
reply_markup=product_inline_keyboard) reply_markup=product_inline_keyboard)
# Create the cart summary # Create the cart summary
product_list = "" product_list = ""
@ -365,7 +360,7 @@ class ChatWorker(threading.Thread):
self.bot.edit_message_text(chat_id=self.chat.id, message_id=final.message_id, self.bot.edit_message_text(chat_id=self.chat.id, message_id=final.message_id,
text=strings.conversation_confirm_cart.format(product_list=product_list, text=strings.conversation_confirm_cart.format(product_list=product_list,
total_cost=str(total_cost)), total_cost=str(total_cost)),
reply_markup=final_inline_keyboard, parse_mode="HTML") reply_markup=final_inline_keyboard)
# If the Remove from cart button has been pressed... # If the Remove from cart button has been pressed...
elif callback.data == "cart_remove": elif callback.data == "cart_remove":
# Get the selected product, ensuring it exists # Get the selected product, ensuring it exists
@ -397,12 +392,11 @@ class ChatWorker(threading.Thread):
if product.image is None: if product.image is None:
self.bot.edit_message_text(chat_id=self.chat.id, message_id=callback.message.message_id, self.bot.edit_message_text(chat_id=self.chat.id, message_id=callback.message.message_id,
text=product.text(cart_qty=cart[callback.message.message_id][1]), text=product.text(cart_qty=cart[callback.message.message_id][1]),
parse_mode="HTML", reply_markup=product_inline_keyboard) reply_markup=product_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=callback.message.message_id, message_id=callback.message.message_id,
caption=product.text(cart_qty=cart[callback.message.message_id][1]), caption=product.text(cart_qty=cart[callback.message.message_id][1]),
parse_mode="HTML",
reply_markup=product_inline_keyboard) reply_markup=product_inline_keyboard)
# Create the cart summary # Create the cart summary
product_list = "" product_list = ""
@ -414,7 +408,7 @@ class ChatWorker(threading.Thread):
self.bot.edit_message_text(chat_id=self.chat.id, message_id=final.message_id, self.bot.edit_message_text(chat_id=self.chat.id, message_id=final.message_id,
text=strings.conversation_confirm_cart.format(product_list=product_list, text=strings.conversation_confirm_cart.format(product_list=product_list,
total_cost=str(total_cost)), total_cost=str(total_cost)),
reply_markup=final_inline_keyboard, parse_mode="HTML") reply_markup=final_inline_keyboard)
# If the done button has been pressed... # If the done button has been pressed...
elif callback.data == "cart_done": elif callback.data == "cart_done":
# End the loop # End the loop
@ -511,8 +505,7 @@ class ChatWorker(threading.Thread):
if selection == strings.menu_cash: if selection == strings.menu_cash:
# Go to the pay with cash function # Go to the pay with cash function
self.bot.send_message(self.chat.id, self.bot.send_message(self.chat.id,
strings.payment_cash.format(user_cash_id=self.user.identifiable_str()), strings.payment_cash.format(user_cash_id=self.user.identifiable_str()))
parse_mode="HTML")
# If the user has selected the Credit Card option... # If the user has selected the Credit Card option...
elif selection == strings.menu_credit_card: elif selection == strings.menu_credit_card:
# Go to the pay with credit card function # Go to the pay with credit card function
@ -621,7 +614,7 @@ class ChatWorker(threading.Thread):
def __bot_info(self): def __bot_info(self):
"""Send information about the bot.""" """Send information about the bot."""
self.bot.send_message(self.chat.id, strings.bot_info, parse_mode="HTML") self.bot.send_message(self.chat.id, strings.bot_info)
def __admin_menu(self): def __admin_menu(self):
"""Function called from the run method when the user is an administrator. """Function called from the run method when the user is an administrator.
@ -640,7 +633,7 @@ class ChatWorker(threading.Thread):
# Send the previously created keyboard to the user (ensuring it can be clicked only 1 time) # Send the previously created keyboard to the user (ensuring it can be clicked only 1 time)
self.bot.send_message(self.chat.id, strings.conversation_open_admin_menu, self.bot.send_message(self.chat.id, strings.conversation_open_admin_menu,
reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True), reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True),
parse_mode="HTML") )
# Wait for a reply from the user # Wait for a reply from the user
selection = self.__wait_for_specific_message([strings.menu_products, strings.menu_orders, selection = self.__wait_for_specific_message([strings.menu_products, strings.menu_orders,
strings.menu_user_mode, strings.menu_edit_credit, strings.menu_user_mode, strings.menu_edit_credit,
@ -718,7 +711,6 @@ class ChatWorker(threading.Thread):
# 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, strings.edit_current_value.format(value=escape(product.name)), self.bot.send_message(self.chat.id, strings.edit_current_value.format(value=escape(product.name)),
parse_mode="HTML",
reply_markup=cancel) reply_markup=cancel)
# Wait for an answer # Wait for an answer
name = self.__wait_for_regex(r"(.*)", cancellable=bool(product)) name = self.__wait_for_regex(r"(.*)", cancellable=bool(product))
@ -734,21 +726,18 @@ class ChatWorker(threading.Thread):
if product: if product:
self.bot.send_message(self.chat.id, self.bot.send_message(self.chat.id,
strings.edit_current_value.format(value=escape(product.description)), strings.edit_current_value.format(value=escape(product.description)),
parse_mode="HTML",
reply_markup=cancel) reply_markup=cancel)
# Wait for an answer # Wait for an answer
description = self.__wait_for_regex(r"(.*)", cancellable=bool(product)) description = self.__wait_for_regex(r"(.*)", cancellable=bool(product))
# Ask for the product price # Ask for the product price
self.bot.send_message(self.chat.id, self.bot.send_message(self.chat.id,
strings.ask_product_price, strings.ask_product_price)
parse_mode="HTML")
# 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, self.bot.send_message(self.chat.id,
strings.edit_current_value.format( strings.edit_current_value.format(
value=(str(utils.Price(product.price)) value=(str(utils.Price(product.price))
if product.price is not None else 'Non in vendita')), if product.price is not None else 'Non in vendita')),
parse_mode="HTML",
reply_markup=cancel) reply_markup=cancel)
# Wait for an answer # Wait for an answer
price = self.__wait_for_regex(r"([0-9]{1,3}(?:[.,][0-9]{1,2})?|[Xx])", price = self.__wait_for_regex(r"([0-9]{1,3}(?:[.,][0-9]{1,2})?|[Xx])",
@ -833,8 +822,7 @@ class ChatWorker(threading.Thread):
cancel_keyboard = telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton(strings.menu_cancel, cancel_keyboard = telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton(strings.menu_cancel,
callback_data="cmd_cancel")]]) callback_data="cmd_cancel")]])
# Send a small intro message on the Live Orders mode # Send a small intro message on the Live Orders mode
self.bot.send_message(self.chat.id, strings.conversation_live_orders_start, reply_markup=stop_keyboard, self.bot.send_message(self.chat.id, strings.conversation_live_orders_start, reply_markup=stop_keyboard)
parse_mode="HTML")
# Create the order keyboard # Create the order keyboard
order_keyboard = telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton(strings.menu_complete, order_keyboard = telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton(strings.menu_complete,
callback_data="order_complete")], callback_data="order_complete")],
@ -886,7 +874,8 @@ class ChatWorker(threading.Thread):
# If the user pressed the refund order button, refund the order... # If the user pressed the refund order button, refund the order...
elif update.data == "order_refund": elif update.data == "order_refund":
# Ask for a refund reason # Ask for a refund reason
reason_msg = self.bot.send_message(self.chat.id, strings.ask_refund_reason, reply_markup=cancel_keyboard) reason_msg = self.bot.send_message(self.chat.id, strings.ask_refund_reason,
reply_markup=cancel_keyboard)
# Wait for a reply # Wait for a reply
reply = self.__wait_for_regex("(.*)", cancellable=True) reply = self.__wait_for_regex("(.*)", cancellable=True)
# If the user pressed the cancel button, cancel the refund # If the user pressed the cancel button, cancel the refund
@ -968,8 +957,7 @@ class ChatWorker(threading.Thread):
self.session.commit() self.session.commit()
# Notify the user of the credit/debit # Notify the user of the credit/debit
self.bot.send_message(user.user_id, self.bot.send_message(user.user_id,
strings.notification_transaction_created.format(transaction=str(transaction)), strings.notification_transaction_created.format(transaction=str(transaction)))
parse_mode="HTML")
# Notify the admin of the success # Notify the admin of the success
self.bot.send_message(self.chat.id, strings.success_transaction_created.format(transaction=str(transaction))) self.bot.send_message(self.chat.id, strings.success_transaction_created.format(transaction=str(transaction)))
@ -983,8 +971,7 @@ class ChatWorker(threading.Thread):
# Send the previously created keyboard to the user (ensuring it can be clicked only 1 time) # Send the previously created keyboard to the user (ensuring it can be clicked only 1 time)
self.bot.send_message(self.chat.id, self.bot.send_message(self.chat.id,
strings.conversation_open_help_menu, strings.conversation_open_help_menu,
reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True), reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True))
parse_mode="HTML")
# Wait for a reply from the user # Wait for a reply from the user
selection = self.__wait_for_specific_message([strings.menu_guide, strings.menu_contact_shopkeeper, selection = self.__wait_for_specific_message([strings.menu_guide, strings.menu_contact_shopkeeper,
strings.menu_cancel]) strings.menu_cancel])
@ -1007,7 +994,7 @@ class ChatWorker(threading.Thread):
# Page number # Page number
page = 0 page = 0
# Create and send a placeholder message to be populated # Create and send a placeholder message to be populated
message = self.bot.send_message(self.chat.id, strings.loading_transactions, parse_mode="HTML") message = self.bot.send_message(self.chat.id, strings.loading_transactions)
# Loop used to move between pages # Loop used to move between pages
while True: while True:
# Retrieve the 10 transactions in that page # Retrieve the 10 transactions in that page
@ -1040,7 +1027,7 @@ class ChatWorker(threading.Thread):
transactions=transactions_string) transactions=transactions_string)
# Update the previously sent message # Update the previously sent message
self.bot.edit_message_text(chat_id=self.chat.id, message_id=message.message_id, text=text, self.bot.edit_message_text(chat_id=self.chat.id, message_id=message.message_id, text=text,
reply_markup=inline_keyboard, parse_mode="HTML") reply_markup=inline_keyboard)
# Wait for user input # Wait for user input
selection = self.__wait_for_inlinekeyboard_callback() selection = self.__wait_for_inlinekeyboard_callback()
# If Previous was selected... # If Previous was selected...