Skip to content

Commit

Permalink
Add vin info command
Browse files Browse the repository at this point in the history
  • Loading branch information
b00bl1k committed May 20, 2024
1 parent e5081c1 commit b9a8274
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 58 deletions.
43 changes: 25 additions & 18 deletions avbot/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
CallbackContext, CommandHandler, Filters, MessageHandler,
CallbackQueryHandler)

from avbot import db
from avbot import models
from avbot import settings
from avbot import cache, db, models, settings, tasks
from avbot.i18n import translations, get_current_lang, setup_locale, _, __
from avbot.plate_formats import PLATE_FORMATS, get_plate_format_by_type
from avbot.utils import validate_vin

logger = logging.getLogger(__name__)

Expand All @@ -28,9 +27,6 @@
Please input one of the following commands:
""")

REPLIES = {}
VIN_LENGTH = 17


def ensure_user_created(telegram_id, from_user):
return db.get_or_create_user(
Expand Down Expand Up @@ -118,6 +114,21 @@ def on_setcountry_query(update: Update, context: CallbackContext, arg: str):
)


def on_vin_command(update: Update, context: CallbackContext):
chat_id = update.message.chat.id
message_id = update.message.message_id
user = context.user_data.get("user")
query = update.message.text
if not validate_vin(query):
update.message.reply_text(
_("VIN contains invalid characters"),
quote=True,
)
else:
tasks.vin_get_info.delay(
chat_id, message_id, query, user.id, language=get_current_lang())


def on_search_query(update: Update, context: CallbackContext):
chat_id = update.message.chat.id
message_id = update.message.message_id
Expand All @@ -135,18 +146,11 @@ def on_search_query(update: Update, context: CallbackContext):

found_count = len(found)
# TODO reduce results using user.country_code

if found_count == 0:
is_vin = (len(query) == VIN_LENGTH)
if settings.FWD_CHAT_ID:
msg = update.message.forward(settings.FWD_CHAT_ID)
if is_vin:
REPLIES.update({msg.message_id: update.message.message_id})
if not is_vin:
update.message.reply_text(
_("Invalid request, try /help command"),
quote=True,
)
update.message.reply_text(
_("Invalid request, try /help command"),
quote=True,
)
elif found_count == 1:
(validated_query, country_code, plate) = found[0]
search_query = db.add_search_query(
Expand Down Expand Up @@ -253,7 +257,7 @@ def on_reply_msg(update: Update, context: CallbackContext):
return
if update.message.reply_to_message:
replied_id = update.message.reply_to_message.message_id
original_message_id = REPLIES.pop(replied_id, None)
original_message_id = cache.get(f"forwarding-{replied_id}")
if original_message_id:
context.bot.send_message(
chat_id=update.message.reply_to_message.forward_from.id,
Expand Down Expand Up @@ -303,6 +307,9 @@ def register_commands(dp):
dp.add_handler(MessageHandler(Filters.regex(
re.compile(r"setcountry", re.IGNORECASE)
), on_setcountry_command), 1)
dp.add_handler(MessageHandler(Filters.regex(
re.compile(r"[0-9a-z]{17}", re.IGNORECASE)
), on_vin_command), 1)
dp.add_handler(MessageHandler(Filters.reply, on_reply_msg), 1)
dp.add_handler(MessageHandler(Filters.text, on_search_query), 1)
dp.add_handler(MessageHandler(Filters.update, on_unsupported_msg), 1)
Expand Down
4 changes: 4 additions & 0 deletions avbot/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def get_or_create_user(telegram_id, first_name, last_name, username,
return user


def get_user_by_id(user_id):
return session.query(models.User).get(user_id)


def get_user(telegram_id):
return session.query(models.User)\
.filter_by(telegram_id=telegram_id).first()
Expand Down
60 changes: 48 additions & 12 deletions avbot/locale/avbot.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-03-30 17:12+0500\n"
"POT-Creation-Date: 2024-05-20 22:56+0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand All @@ -17,48 +17,52 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.14.0\n"

#: avbot/commands.py:21
#: avbot/commands.py:20
msgid ""
"Welcome to @avtonomerbot\n"
"\n"
"Input /help for the detailed information.\n"
"Type /setlang to change language."
msgstr ""

#: avbot/commands.py:26
#: avbot/commands.py:25
msgid ""
"Bot for searching on the platesmania.com website\n"
"\n"
"Please input one of the following commands:\n"
msgstr ""

#: avbot/commands.py:83
#: avbot/commands.py:81
msgid "Current language: {}"
msgstr ""

#: avbot/commands.py:102
#: avbot/commands.py:100
msgid "Choose country:"
msgstr ""

#: avbot/commands.py:114
#: avbot/commands.py:112
msgid "Current country: {}"
msgstr ""

#: avbot/commands.py:147
#: avbot/commands.py:126
msgid "VIN contains invalid characters"
msgstr ""

#: avbot/commands.py:153
msgid "Invalid request, try /help command"
msgstr ""

#: avbot/commands.py:177 avbot/commands.py:187
#: avbot/commands.py:183 avbot/commands.py:193
msgid ""
"Request: {}\n"
"Specify type of the plate:"
msgstr ""

#: avbot/commands.py:225
#: avbot/commands.py:231
msgid "Something went wrong, please try again."
msgstr ""

#: avbot/commands.py:243
#: avbot/commands.py:249
msgid "Unsupported request. Use /help"
msgstr ""

Expand Down Expand Up @@ -90,11 +94,43 @@ msgstr ""
msgid "Ukraine"
msgstr ""

#: avbot/cmd/base.py:20
#: avbot/vininfo.py:18
msgid "Model"
msgstr ""

#: avbot/vininfo.py:20
msgid "License plate"
msgstr ""

#: avbot/vininfo.py:23
msgid "Manufactured year"
msgstr ""

#: avbot/vininfo.py:25
msgid "Engine power"
msgstr ""

#: avbot/vininfo.py:25
msgid "hp"
msgstr ""

#: avbot/vininfo.py:27
msgid "Engine displacement"
msgstr ""

#: avbot/vininfo.py:27
msgid "ccm"
msgstr ""

#: avbot/vininfo.py:29
msgid "Engine number"
msgstr ""

#: avbot/cmd/base.py:21
msgid "No data"
msgstr ""

#: avbot/cmd/base.py:24
#: avbot/cmd/base.py:25
msgid "Nothing found"
msgstr ""

Expand Down
63 changes: 51 additions & 12 deletions avbot/locale/en/LC_MESSAGES/avbot.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-03-30 17:12+0500\n"
"POT-Creation-Date: 2024-05-20 22:56+0500\n"
"PO-Revision-Date: 2024-01-07 01:41+0500\n"
"Last-Translator: Alexey <alex@b00bl1k.ru>\n"
"Language: en\n"
Expand All @@ -18,48 +18,52 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.14.0\n"

#: avbot/commands.py:21
#: avbot/commands.py:20
msgid ""
"Welcome to @avtonomerbot\n"
"\n"
"Input /help for the detailed information.\n"
"Type /setlang to change language."
msgstr ""

#: avbot/commands.py:26
#: avbot/commands.py:25
msgid ""
"Bot for searching on the platesmania.com website\n"
"\n"
"Please input one of the following commands:\n"
msgstr ""

#: avbot/commands.py:83
#: avbot/commands.py:81
msgid "Current language: {}"
msgstr ""

#: avbot/commands.py:102
#: avbot/commands.py:100
msgid "Choose country:"
msgstr ""

#: avbot/commands.py:114
#: avbot/commands.py:112
msgid "Current country: {}"
msgstr ""

#: avbot/commands.py:147
#: avbot/commands.py:126
msgid "VIN contains invalid characters"
msgstr ""

#: avbot/commands.py:153
msgid "Invalid request, try /help command"
msgstr ""

#: avbot/commands.py:177 avbot/commands.py:187
#: avbot/commands.py:183 avbot/commands.py:193
msgid ""
"Request: {}\n"
"Specify type of the plate:"
msgstr ""

#: avbot/commands.py:225
#: avbot/commands.py:231
msgid "Something went wrong, please try again."
msgstr ""

#: avbot/commands.py:243
#: avbot/commands.py:249
msgid "Unsupported request. Use /help"
msgstr ""

Expand Down Expand Up @@ -91,11 +95,43 @@ msgstr ""
msgid "Ukraine"
msgstr ""

#: avbot/cmd/base.py:20
#: avbot/vininfo.py:18
msgid "Model"
msgstr ""

#: avbot/vininfo.py:20
msgid "License plate"
msgstr ""

#: avbot/vininfo.py:23
msgid "Manufactured year"
msgstr ""

#: avbot/vininfo.py:25
msgid "Engine power"
msgstr ""

#: avbot/vininfo.py:25
msgid "hp"
msgstr ""

#: avbot/vininfo.py:27
msgid "Engine displacement"
msgstr ""

#: avbot/vininfo.py:27
msgid "ccm"
msgstr ""

#: avbot/vininfo.py:29
msgid "Engine number"
msgstr ""

#: avbot/cmd/base.py:21
msgid "No data"
msgstr ""

#: avbot/cmd/base.py:24
#: avbot/cmd/base.py:25
msgid "Nothing found"
msgstr ""

Expand Down Expand Up @@ -252,3 +288,6 @@ msgstr ""
#~ msgid "regulat plates (2004)"
#~ msgstr ""

#~ msgid "Model:"
#~ msgstr ""

Loading

0 comments on commit b9a8274

Please sign in to comment.