Пример #1
0
    def choose_and_send_file(self) -> None:
        """Call file picker and send chosen file based on mimetype"""
        chat_id = self.model.chats.id_by_index(self.model.current_chat)
        file_path = None
        if not chat_id:
            return self.present_error("No chat selected")
        try:
            with NamedTemporaryFile("w") as f, suspend(self.view) as s:
                s.call(config.FILE_PICKER_CMD.format(file_path=f.name))
                with open(f.name) as f:
                    file_path = f.read().strip()
        except FileNotFoundError:
            pass
        if not file_path or not os.path.isfile(file_path):
            return self.present_error("No file was selected")
        mime_map = {
            "animation": self.tg.send_animation,
            "image": self.tg.send_photo,
            "audio": self.tg.send_audio,
            "video": self._send_video,
        }
        mime = get_mime(file_path)
        if mime in ("image", "video", "animation"):
            resp = self.view.status.get_input(
                f"Upload <{file_path}> compressed?[Y/n]")
            self.render_status()
            if resp is None:
                return self.present_info("Uploading cancelled")
            if not is_yes(resp):
                mime = ""

        fun = mime_map.get(mime, self.tg.send_doc)
        fun(file_path, chat_id)
Пример #2
0
    def record_voice(self):
        file_path = f"/tmp/voice-{datetime.now()}.oga"
        with suspend(self.view) as s:
            s.call(
                config.VOICE_RECORD_CMD.format(
                    file_path=shlex.quote(file_path)
                )
            )
        resp = self.view.status.get_input(
            f"Do you want to send recording: {file_path}? [Y/n]"
        )
        if not is_yes(resp):
            self.present_info("Voice message discarded")
            return

        if not os.path.isfile(file_path):
            self.present_info(f"Can't load recording file {file_path}")
            return

        chat_id = self.model.chats.id_by_index(self.model.current_chat)
        if not chat_id:
            return
        duration = get_duration(file_path)
        waveform = get_waveform(file_path)
        self.tg.send_voice(file_path, chat_id, duration, waveform)
        self.present_info(f"Sent voice msg: {file_path}")