part of tg
This commit is contained in:
parent
ef08733979
commit
1b310dbfbe
|
@ -22,7 +22,9 @@ class TgBotClass(AppConfig):
|
||||||
|
|
||||||
tgbot = TgBotApp()
|
tgbot = TgBotApp()
|
||||||
app = await tgbot.init_tg()
|
app = await tgbot.init_tg()
|
||||||
await tgbot.set_webhook(f"https://{settings.TGBOT['base_url']}/api/tgbot/")
|
await tgbot.set_webhook(
|
||||||
|
f"https://{settings.TGBOT['base_url']}/api/tgbot/",
|
||||||
|
)
|
||||||
return app
|
return app
|
||||||
|
|
||||||
async def some_function(self=None):
|
async def some_function(self=None):
|
||||||
|
|
|
@ -34,7 +34,7 @@ class Item(models.Model):
|
||||||
unique=True,
|
unique=True,
|
||||||
)
|
)
|
||||||
user_id = models.BigIntegerField()
|
user_id = models.BigIntegerField()
|
||||||
name = models.CharField(max_length=255)
|
name = models.CharField(max_length=255, unique_for_month=True)
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
updated_at = models.DateTimeField(auto_now=True)
|
updated_at = models.DateTimeField(auto_now=True)
|
||||||
tmc = models.ManyToManyField(CustomTable)
|
tmc = models.ManyToManyField(CustomTable)
|
||||||
|
|
|
@ -9,6 +9,7 @@ from telegram import (
|
||||||
KeyboardButton,
|
KeyboardButton,
|
||||||
InlineKeyboardMarkup,
|
InlineKeyboardMarkup,
|
||||||
InlineKeyboardButton,
|
InlineKeyboardButton,
|
||||||
|
MessageEntity,
|
||||||
)
|
)
|
||||||
from telegram.ext import (
|
from telegram.ext import (
|
||||||
Application,
|
Application,
|
||||||
|
@ -53,19 +54,32 @@ class TgBotApp:
|
||||||
|
|
||||||
inv = []
|
inv = []
|
||||||
async for e in Item.objects.filter(user_id=user.id):
|
async for e in Item.objects.filter(user_id=user.id):
|
||||||
inv.append(e)
|
inv.append(e.name)
|
||||||
|
keys = chunk(1, inv)
|
||||||
|
|
||||||
await update.message.reply_markdown_v2(
|
await update.message.reply_markdown_v2(
|
||||||
("Ваши инвентаризации\n" "\n".join([f"`/inv_{i.id}`" for i in inv])),
|
("Ваши инвентаризации"),
|
||||||
# reply_markup=ForceReply(selective=True),
|
reply_markup=ReplyKeyboardMarkup(
|
||||||
|
[[KeyboardButton(i) for i in arr] for arr in keys]
|
||||||
|
),
|
||||||
reply_parameters=ReplyParameters(message_id=update.message.message_id),
|
reply_parameters=ReplyParameters(message_id=update.message.message_id),
|
||||||
)
|
)
|
||||||
|
context.chat_data["step"] = "get_inv"
|
||||||
|
|
||||||
async def inv(self, update: Update, context: CallbackContext):
|
async def inv(self, update: Update, context: CallbackContext):
|
||||||
user = update.effective_user
|
user = update.effective_user
|
||||||
current_step = context.chat_data.get("step", None)
|
current_step = context.chat_data.get("step", None)
|
||||||
|
if not update.message:
|
||||||
|
return True
|
||||||
|
logger.info(update.message)
|
||||||
logger.info(f"Step {current_step} from user {user.full_name}")
|
logger.info(f"Step {current_step} from user {user.full_name}")
|
||||||
|
|
||||||
|
if current_step == "get_inv":
|
||||||
|
inv = await Item.objects.aget(name=update.message.text)
|
||||||
|
context.chat_data["inv"] = inv.id
|
||||||
|
context.chat_data["step"] = "name"
|
||||||
|
current_step = context.chat_data["step"]
|
||||||
|
|
||||||
if not current_step and update.message.text == "/inv":
|
if not current_step and update.message.text == "/inv":
|
||||||
inv = await Item.objects.acreate(user_id=user.id)
|
inv = await Item.objects.acreate(user_id=user.id)
|
||||||
await update.message.reply_markdown_v2(
|
await update.message.reply_markdown_v2(
|
||||||
|
@ -79,7 +93,7 @@ class TgBotApp:
|
||||||
context.chat_data["inv"] = inv.id
|
context.chat_data["inv"] = inv.id
|
||||||
context.chat_data["step"] = "name"
|
context.chat_data["step"] = "name"
|
||||||
|
|
||||||
elif current_step is "name":
|
elif current_step == "name":
|
||||||
inv = await Item.objects.aget(id=context.chat_data["inv"])
|
inv = await Item.objects.aget(id=context.chat_data["inv"])
|
||||||
inv.name = update.message.text
|
inv.name = update.message.text
|
||||||
await inv.asave()
|
await inv.asave()
|
||||||
|
@ -90,7 +104,11 @@ class TgBotApp:
|
||||||
keys = chunk(3, tmc)
|
keys = chunk(3, tmc)
|
||||||
|
|
||||||
await update.message.reply_markdown_v2(
|
await update.message.reply_markdown_v2(
|
||||||
(f"Инвентаризация `#{inv.id}`\n" f"Название объекта `{inv.name}`\n"),
|
(
|
||||||
|
f"Инвентаризация `#{inv.id}`\n"
|
||||||
|
f"Название объекта `{inv.name}`\n"
|
||||||
|
f"Количество ТМЦ {await inv.tmc.acount()}"
|
||||||
|
),
|
||||||
reply_parameters=ReplyParameters(message_id=update.message.message_id),
|
reply_parameters=ReplyParameters(message_id=update.message.message_id),
|
||||||
reply_markup=ReplyKeyboardMarkup(
|
reply_markup=ReplyKeyboardMarkup(
|
||||||
[[KeyboardButton(i) for i in arr] for arr in keys]
|
[[KeyboardButton(i) for i in arr] for arr in keys]
|
||||||
|
@ -99,7 +117,7 @@ class TgBotApp:
|
||||||
logger.info(tmc)
|
logger.info(tmc)
|
||||||
context.chat_data["step"] = "add_tmc"
|
context.chat_data["step"] = "add_tmc"
|
||||||
|
|
||||||
elif current_step is "add_tmc":
|
elif current_step == "add_tmc":
|
||||||
tmc_name = update.message.text
|
tmc_name = update.message.text
|
||||||
tmc = await CustomTable.objects.aget(name=tmc_name)
|
tmc = await CustomTable.objects.aget(name=tmc_name)
|
||||||
|
|
||||||
|
@ -125,7 +143,7 @@ class TgBotApp:
|
||||||
)
|
)
|
||||||
context.chat_data["tmc"] = tmc.id
|
context.chat_data["tmc"] = tmc.id
|
||||||
context.chat_data["step"] = "add_field"
|
context.chat_data["step"] = "add_field"
|
||||||
elif current_step is "add_field":
|
elif current_step == "add_field":
|
||||||
tmc = await CustomTable.objects.aget(id=context.chat_data["tmc"])
|
tmc = await CustomTable.objects.aget(id=context.chat_data["tmc"])
|
||||||
|
|
||||||
element_name = update.message.text
|
element_name = update.message.text
|
||||||
|
@ -147,8 +165,10 @@ class TgBotApp:
|
||||||
)
|
)
|
||||||
context.chat_data["element"] = element.id
|
context.chat_data["element"] = element.id
|
||||||
context.chat_data["step"] = "add_field_data"
|
context.chat_data["step"] = "add_field_data"
|
||||||
elif current_step is "add_field_data":
|
elif current_step == "add_field_data":
|
||||||
element = await Element.objects.aget(id=context.chat_data["element"])
|
inv = await Item.objects.aget(id=context.chat_data["inv"])
|
||||||
|
tmc = await CustomTable.objects.aget(id=inv.tmc)
|
||||||
|
element = await Element.objects.aget(id=inv.element)
|
||||||
|
|
||||||
if update.message.photo:
|
if update.message.photo:
|
||||||
element.photoid = update.message.photo[-1].file_id
|
element.photoid = update.message.photo[-1].file_id
|
||||||
|
@ -157,6 +177,23 @@ class TgBotApp:
|
||||||
|
|
||||||
await element.asave()
|
await element.asave()
|
||||||
|
|
||||||
|
await update.message.reply_markdown_v2(
|
||||||
|
(
|
||||||
|
f"Инвентаризация `#{inv.id}`\n"
|
||||||
|
f"Название объекта `{inv.name}`\n"
|
||||||
|
f"Название ТМЦ `{tmc.name}`\n"
|
||||||
|
f"Название элемента `{element.name}`\n"
|
||||||
|
f"Загрузите фото или пришлите текст"
|
||||||
|
),
|
||||||
|
reply_parameters=ReplyParameters(message_id=update.message.message_id),
|
||||||
|
reply_markup=ReplyKeyboardRemove(),
|
||||||
|
)
|
||||||
|
context.chat_data["element"] = element.id
|
||||||
|
context.chat_data["step"] = "add_field_data"
|
||||||
|
else:
|
||||||
|
logger.info(update.message.entities)
|
||||||
|
logger.info(f"no step for update {update}")
|
||||||
|
|
||||||
if "step" in context.chat_data and context.chat_data["step"] == current_step:
|
if "step" in context.chat_data and context.chat_data["step"] == current_step:
|
||||||
context.chat_data["step"] = None
|
context.chat_data["step"] = None
|
||||||
context.chat_data["inv"] = None
|
context.chat_data["inv"] = None
|
||||||
|
@ -170,7 +207,9 @@ class TgBotApp:
|
||||||
logger.error("no app")
|
logger.error("no app")
|
||||||
return
|
return
|
||||||
app = self._app
|
app = self._app
|
||||||
await app.bot.set_webhook(url, allowed_updates=Update.ALL_TYPES)
|
await app.bot.set_webhook(
|
||||||
|
url, allowed_updates=Update.ALL_TYPES, drop_pending_updates=True
|
||||||
|
)
|
||||||
|
|
||||||
async def init_tg(self):
|
async def init_tg(self):
|
||||||
self._app = (
|
self._app = (
|
||||||
|
@ -197,4 +236,5 @@ class TgBotApp:
|
||||||
)
|
)
|
||||||
await self._app.initialize()
|
await self._app.initialize()
|
||||||
await self._app.start()
|
await self._app.start()
|
||||||
|
self._app
|
||||||
return self._app
|
return self._app
|
||||||
|
|
Loading…
Reference in New Issue