Compare commits
9
Commits
5363ca9116
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c12b94633e | ||
|
|
5f8250380a | ||
|
|
3bb09413b1 | ||
|
|
32aa9f3225 | ||
|
|
fa81921fad | ||
|
|
9bf2d8d41b | ||
|
|
472fcf4f9d | ||
|
|
b5c58984c5 | ||
|
|
99bf9f07fa |
@@ -0,0 +1,50 @@
|
||||
* Change Log
|
||||
** 0.1.0 <2023-06-16 Fri>
|
||||
Add ApiFetcher to make requests to the server.
|
||||
** 0.2.0 <2023-06-16 Fri>
|
||||
Add CityAPI to get information about city from the server.
|
||||
** 0.2.1 <2023-06-16 Fri>
|
||||
Add the city handler to have cities-list in bot's command list.
|
||||
** 0.2.2 <2023-06-16 Fri>
|
||||
Update docker-compose file to run src.main as package.
|
||||
** 0.2.3 <2023-06-16 Fri>
|
||||
Fix fetcher's send_json to make post a request and get a response.
|
||||
** 0.3.0 <2023-06-17 Sat>
|
||||
Add the config file for DotsBotApi.
|
||||
** 0.4.0 <2023-06-17 Sat>
|
||||
Add UserAPI for register new user.
|
||||
** 0.4.1 <2023-06-17 Sat>
|
||||
Add the CI/CD configuration.
|
||||
** 0.4.2 <2023-06-17 Sat>
|
||||
Fixed the CI/CD configuration.
|
||||
** 0.4.3 <2023-06-17 Sat>
|
||||
Fixed the CI/CD configuration by adding all deps.
|
||||
** 0.5.0 <2023-06-20 Tue>
|
||||
Add methods to select a city and return message.
|
||||
** 0.5.1 <2023-06-20 Tue>
|
||||
Add the handler to select a city.
|
||||
** 0.6.0 <2023-06-20 Tue>
|
||||
Add company methods to get the list and to select a company.
|
||||
** 0.6.1 <2023-06-20 Tue>
|
||||
Add the company handlers.
|
||||
** 0.7.0 <2023-06-20 Tue>
|
||||
Add the method to show the items by categories.
|
||||
** 0.7.1 <2023-06-20 Tue>
|
||||
Add the handler to show items.
|
||||
** 0.8.0 <2023-06-20 Tue>
|
||||
Add the methods to show cart items and to add an item to a cart.
|
||||
** 0.8.1 <2023-06-20 Tue>
|
||||
Add the handlers to show cart items and to add an item to a cart.
|
||||
** 0.9.0 <2023-06-22 Thu>
|
||||
Add method to send and get the order.
|
||||
** 0.9.2 <2023-06-22 Thu>
|
||||
Add the order handler.
|
||||
** 0.9.5 <2023-07-10 Mon>
|
||||
Refactor the listeners of the commands.
|
||||
** 0.9.6 <2023-07-18 Tue>
|
||||
Update the config class for the dots bot api.
|
||||
** 0.9.7 <2023-07-18 Tue>
|
||||
Update the README.
|
||||
** 0.9.7 <2023-07-18 Tue>
|
||||
Add the CHANGELOG.
|
||||
|
||||
@@ -21,6 +21,7 @@ Setup
|
||||
USERNAME=@example:example.com
|
||||
PASSWORD=password
|
||||
SERVER=https://example.com
|
||||
BOT_API_BASE_URL=https://domain
|
||||
|
||||
- change the ``config.toml``: `more about allowlist and blocklist
|
||||
formats <https://simple-matrix-bot-lib.readthedocs.io/en/latest/examples.html#id2>`__
|
||||
|
||||
@@ -56,3 +56,23 @@ class CartAPI:
|
||||
case {"error": error_message}:
|
||||
return error_message
|
||||
|
||||
async def get_order(self, username: str):
|
||||
endpoint = f"/api/v2/order?matrixUsername={username}"
|
||||
|
||||
status, json_data = await self.__fetcher.fetch_json(endpoint)
|
||||
|
||||
if status == 200:
|
||||
return {"ok": json_data}
|
||||
|
||||
return {"error": "Сталася помилка, спробуйте пізніше."}
|
||||
|
||||
async def get_order_message(self, username: str) -> str:
|
||||
response = await self.get_order(username)
|
||||
|
||||
match response:
|
||||
case {"ok": json_data}:
|
||||
return self.__formatter.format(json_data)
|
||||
|
||||
case {"error": error_message}:
|
||||
return error_message
|
||||
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
from environs import Env
|
||||
|
||||
env = Env()
|
||||
env.read_env()
|
||||
|
||||
class DotsBotApiConfig():
|
||||
_base_url = "https://domain"
|
||||
_base_url = env('BOT_API_BASE_URL')
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self._base_url=}"
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
|
||||
class OrderFormatter():
|
||||
def format(self, order) -> str:
|
||||
id = order['id']
|
||||
order_type = order['delivery']['deliveryTypeText']
|
||||
order_address = order['delivery']['deliveryAddress']
|
||||
item_price = order['prices']['itemsPrice']
|
||||
package_price = order['prices']['packagePrice']
|
||||
delivery_price = order['prices']['deliveryPrice']
|
||||
full_price = order['prices']['fullPrice']
|
||||
return f"Ваше замовлення має id: {id}.\n" \
|
||||
f"Вид доставки: {order_type}.\n" \
|
||||
f"Доставка до: {order_address}.\n" \
|
||||
f"Ціна товарів: {item_price} ₴.\n" \
|
||||
f"Ціна пакування: {package_price} ₴.\n" \
|
||||
f"Ціна доставки: {delivery_price} ₴.\n\n" \
|
||||
f"Остаточна ціна: {full_price} ₴."
|
||||
|
||||
@@ -6,6 +6,7 @@ from src.handlers.city import city_list_handler, select_city_handler
|
||||
from src.handlers.company import company_list_handler, select_company_handler
|
||||
from src.handlers.category import category_list_handler
|
||||
from src.handlers.cart import cart_list_handler, add_item_handler
|
||||
from src.handlers.cart import order_handler
|
||||
from src.handlers.user import register_handler
|
||||
|
||||
from src.fmt.city_formatter import CityFormatter
|
||||
@@ -22,11 +23,19 @@ def setup(bot: botlib.Bot, prefix: str):
|
||||
if not match.prefix():
|
||||
return
|
||||
|
||||
# No registered user required
|
||||
|
||||
if match.command('help'):
|
||||
await help_handler(bot=bot, room_id=room.room_id, sender=message.sender, admin_id=bot.config.admin_id)
|
||||
|
||||
@bot.listener.on_message_event
|
||||
async def guests_commands(room: nio.rooms.MatrixRoom, message: nio.events.room_events.Event):
|
||||
match = botlib.MessageMatch(room, message, bot, prefix=prefix)
|
||||
|
||||
if not match.is_not_from_this_bot():
|
||||
return
|
||||
|
||||
if not match.prefix():
|
||||
return
|
||||
|
||||
if match.command('city-list'):
|
||||
await city_list_handler(bot=bot, room_id=room.room_id, sender=message.sender, admin_id=bot.config.admin_id)
|
||||
|
||||
@@ -38,7 +47,15 @@ def setup(bot: botlib.Bot, prefix: str):
|
||||
if match.command('register'):
|
||||
await register_handler(bot=bot, room_id=room.room_id, sender=message.sender, admin_id=bot.config.admin_id, args=match.args())
|
||||
|
||||
# A registered user required
|
||||
@bot.listener.on_message_event
|
||||
async def authorized_commands(room: nio.rooms.MatrixRoom, message: nio.events.room_events.Event):
|
||||
match = botlib.MessageMatch(room, message, bot, prefix=prefix)
|
||||
|
||||
if not match.is_not_from_this_bot():
|
||||
return
|
||||
|
||||
if not match.prefix():
|
||||
return
|
||||
|
||||
if match.command('company-list'):
|
||||
await company_list_handler(bot=bot, room_id=room.room_id, sender=message.sender, admin_id=bot.config.admin_id)
|
||||
@@ -62,3 +79,6 @@ def setup(bot: botlib.Bot, prefix: str):
|
||||
count = int(count)
|
||||
await add_item_handler(bot=bot, room_id=room.room_id, sender=message.sender, admin_id=bot.config.admin_id, item_name=name, count=count)
|
||||
|
||||
if match.command('order'):
|
||||
await order_handler(bot=bot, room_id=room.room_id, sender=message.sender, admin_id=bot.config.admin_id)
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ from src.api.v2.cart_api import CartAPI
|
||||
from src.fetcher.v2.api_fetcher import ApiFetcher
|
||||
from src.fmt.item_formatter import ItemFormatter
|
||||
from src.fmt.cart_item_formatter import CartItemFormatter
|
||||
from src.fmt.order_formatter import OrderFormatter
|
||||
|
||||
dots_bot_api_config = DotsBotApiConfig()
|
||||
|
||||
@@ -22,6 +23,17 @@ async def cart_list_handler(room_id: str, bot: botlib.Bot, sender: str, admin_id
|
||||
|
||||
await bot.api.send_markdown_message(room_id=room_id, message=msg)
|
||||
|
||||
async def order_handler(room_id: str, bot: botlib.Bot, sender: str, admin_id: str):
|
||||
|
||||
session = aiohttp.ClientSession(json_serialize=ujson.dumps)
|
||||
apiFetcher = ApiFetcher(dots_bot_api_config.get_base_url(), session)
|
||||
formatter = OrderFormatter()
|
||||
cartAPI = CartAPI(apiFetcher, formatter)
|
||||
|
||||
msg: str = await cartAPI.get_order_message(sender)
|
||||
|
||||
await bot.api.send_markdown_message(room_id=room_id, message=msg)
|
||||
|
||||
async def add_item_handler(room_id: str, bot: botlib.Bot, sender: str, admin_id: str, item_name: str, count: int):
|
||||
|
||||
session = aiohttp.ClientSession(json_serialize=ujson.dumps)
|
||||
|
||||
+20
-4
@@ -2,9 +2,25 @@ import simplematrixbotlib as botlib
|
||||
|
||||
|
||||
async def help_handler(room_id: str, bot: botlib.Bot, sender: str, admin_id: str):
|
||||
msg: str = 'Hi **{}**\n\n' \
|
||||
'Prefix: `!`\n' \
|
||||
'Its a simplematrixbotlib template!\n' \
|
||||
'Admin contact: **{}**'
|
||||
msg: str = 'Привіт, **{}**\n\n' \
|
||||
'Префікс команда: `!`\n' \
|
||||
'[dots-matrix-bot](https://gitlab.com/KKlochko/dots-matrix-bot) - це вільне програмне забезпечення за лацензією GNU AGPLv3 чи будь-яка новіша версія.\n' \
|
||||
'Контакт адміна: **{}**\n\n' \
|
||||
'\n\nКоманди:\n' \
|
||||
'help - допомога.\n' \
|
||||
'register - пов\'язати користувача та акаунт матриці.\n' \
|
||||
'Приклад: `!register username 3806712312121`.\n\n' \
|
||||
'city-list - перелік міст.\n' \
|
||||
'company-list - перелік компаній.\n' \
|
||||
'category-list - перелік категорій та товарів.\n' \
|
||||
'cart - перелік товарів у кошику та їх вартість.\n\n' \
|
||||
'select-city - обрати місто.\n' \
|
||||
'Приклад: `!select-city Чернігів`.\n\n' \
|
||||
'select-company - обрати компанію.\n' \
|
||||
'Приклад: `!select-company Назва Компанії`.\n\n' \
|
||||
'add-item - додати товар у кошик.\n' \
|
||||
'Приклад: `!add-item !add-item Назва товару, 2`.\n\n' \
|
||||
'order - замовити.'
|
||||
|
||||
await bot.api.send_markdown_message(room_id=room_id, message=msg.format(sender, admin_id))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user