Exemplo n.º 1
0
async def start_cmd_internal_workflow(
    state: FSMContext,
    cmd: InternalCommand,
    message_kwargs,
    fill_status,
    callback: Callable,
    message_id=None,
):
    """
    The same mechanism that is used in the processing of actuator commands,
    only adapted for the internal needs of the bot.
    (Main menu commands)
    """
    mediator = MediatorDependency.mediator
    cmd_fill_status = cmd.fill_status

    if cmd_fill_status == ArgumentsFillStatus.FILLED:
        try:
            await callback(**cmd.filled_args)
        except NoSuchUser:
            await mediator.send(OutgoingMessage(chat_id=state.chat, text=UNKNOWN_USER))
        await state.reset_state()
    elif cmd_fill_status == ArgumentsFillStatus.NOT_FILLED:
        await state_storage.update_data(
            user=cmd.user_id, chat=cmd.user_id, cmd=cmd, callback=callback
        )
        new_kwargs = await cmd.get_next_step()
        message_kwargs.update(new_kwargs)
        # Arguments input state:
        await Command.argument_internal.set()
        await mediator.send(OutgoingMessage(**message_kwargs))
Exemplo n.º 2
0
 async def _check_replies(self, message: OutgoingMessage,
                          reply_to_message_id: int):
     if message.replies:
         replies = message.get_replies(reply_to_message_id)
         for reply in replies:
             await self.handle_user_message(reply
                                            )  # TODO replies for channel
Exemplo n.º 3
0
 async def send(self, message: OutgoingMessage):
     """Send message to telegram API"""
     try:
         return await self.telegram_dispatcher.send(message)
     except aiogram.utils.exceptions.MessageIsTooLong:
         error_message = OutgoingMessage(chat_id=message.chat_id,
                                         text="Message is too long!")
         return await self.telegram_dispatcher.send(error_message)
Exemplo n.º 4
0
 async def turn_off_actuator(self, actuator_name: str):
     """Turn off the actuator"""
     self.connected_actuators.pop(actuator_name)
     self.memory_storage.remove_client(actuator_name)
     admins = await self.mediator.users.get_admins()
     granters = await self.get_granters(actuator_name)
     for user in set(admins + granters):
         message = OutgoingMessage(
             chat_id=user.telegram_id,
             text=
             f"{emojies.ACTUATOR_TURNED_OFF} Actuator {actuator_name} has been turned OFF!",
         )
         await self.mediator.send(message)
Exemplo n.º 5
0
async def continue_cmd_workflow(state,
                                cmd: ActuatorCommand,
                                message_kwargs,
                                fill_status,
                                mediator,
                                message_id=None):
    cmd_fill_status = cmd.fill_status
    if cmd_fill_status == ArgumentsFillStatus.FILLED:
        # Команда заполнена
        await _finish_cmd_workflow(state, cmd, mediator, message_id)
    elif cmd_fill_status == ArgumentsFillStatus.NOT_FILLED:
        # Команда не заполнена:

        await state_storage.update_data(user=cmd.user_id,
                                        chat=cmd.user_id,
                                        cmd=cmd)
        next_step_kwargs = await cmd.get_next_step()
        message_kwargs.update(next_step_kwargs)
        # Arguments input state:
        await Command.arguments.set()
        await mediator.send(OutgoingMessage(**message_kwargs))
Exemplo n.º 6
0
    async def turn_on_actuator(self, actuator_name: str) -> asyncio.Queue:
        """Turn on the actuator"""
        admins = await self.mediator.users.get_admins()
        if self.is_connected(actuator_name):
            text = (
                f"{emojies.ACTUATOR_ALREADY_TURNED_ON} Actuator {actuator_name} already turned ON!"
            )
            users = admins
            result = None
        else:
            actuator_queue: asyncio.Queue = asyncio.Queue()
            self.connected_actuators[actuator_name] = actuator_queue
            granters = await self.get_granters(actuator_name)
            users = set(admins + granters)
            text = f"{emojies.ACTUATOR_TURNED_ON} Actuator {actuator_name} has been turned ON!"
            result = actuator_queue

        for user in users:
            message = OutgoingMessage(chat_id=user.telegram_id, text=text)
            await self.mediator.send(message)
        if result:
            return result
        else:
            raise exceptions.ActuatorAlreadyConnected
Exemplo n.º 7
0
async def start_actuator_command_workflow(message,
                                          state,
                                          mediator,
                                          message_id=None):
    command_state = await state.get_state()
    user_id = chat_id = message.chat.id

    message_kwargs = {"chat_id": user_id, "message_id": message_id}

    actuator_name, command, args = ActuatorCommand.parse_cmd_string(
        message.text)

    if not await mediator.users.has_grant(user_id, actuator_name):
        await message.answer(text=UNKNOWN_COMMAND_OR_ACTUATOR)
        await state.reset_state()
        return

    is_admin = await mediator.users.is_admin(user_id)

    if command is None and command_state is None:
        # Пришло только имя клиента - показываем возможные команды
        try:
            message_kwargs["text"] = get_client_commands(
                mediator, actuator_name, is_admin)
        except NoSuchActuatorInRAM:
            _LOGGER.warning("No actuator %s in RAM!", actuator_name)
            message_kwargs["text"] = UNKNOWN_ACTUATOR
            await state.reset_state()
            return
        finally:
            await mediator.send(OutgoingMessage(**message_kwargs))

        await Command.client.set()
        await state_storage.update_data(user=user_id,
                                        chat=chat_id,
                                        client=actuator_name)
        return
    elif command is None and command_state is not None:
        # Не указана команда
        message_kwargs["text"] = COMMAND_IS_NOT_FILLED + CONTEXT_CANCEL_MENU
        await mediator.send(OutgoingMessage(**message_kwargs))
        return

    # Указаны клиент и команда:
    exception = False
    try:
        cmd = ActuatorCommand(actuator_name, command, args, user_id, is_admin)
        if not cmd.cmd_scheme:
            return
        await continue_cmd_workflow(
            state,
            cmd,
            message_kwargs,
            CommandFillStatus.FILL_COMMAND,
            mediator,
            message_id,
        )
    except NoSuchCommand as e:
        exception = True
        message_kwargs["text"] = COMMAND_IS_NOT_EXIST.format(command=e.cmd)
    except NoSuchActuatorInRAM:
        exception = True
        message_kwargs["text"] = NO_SUCH_CLIENT.format(client=actuator_name)
    if exception:
        await mediator.send(OutgoingMessage(**message_kwargs))
        await state.reset_state()