Skip to content

An always-on second brain you talk to. Voice notes in Telegram → typed, linked knowledge in your Obsidian vault. Runs 24/7 on the Claude subscription you already have.

feat(chat): media input — photos, files, forwards; never silent

smixseeb28ffb54cf
diff --git a/README.md b/README.md
index 6555585..06b43d8 100644
--- a/README.md
+++ b/README.md
@@ -24,7 +24,7 @@ The real issue: organizing takes more effort than thinking. So the thinking neve
## What this actually does
-You talk to a Telegram bot. Voice or text - whatever is natural. You don't think about categories, tags, or where things go.
+You talk to a Telegram bot. Voice, text, photos, files, forwarded posts - whatever is natural. You don't think about categories, tags, or where things go.
The agent handles everything else:
@@ -93,6 +93,7 @@ You don't run maintenance. The agent does.
|----------|-----------|
| Voice note about a client call | Transcribes, creates CRM card with a follow-up |
| Quick text: "idea for the Q2 campaign" | Saves to ideas folder, links to related notes |
+| Forwarded post or a photo of a whiteboard | Saves the file to the vault, the agent reads it and files the takeaways |
| "Remind me about the call tomorrow at 3pm" | Creates a one-shot scheduled job, pings you in Telegram |
| "Every weekday at 18:30 check my inbox folder" | Creates a recurring job that stays quiet when there's nothing new |
| "Process" button | Runs the full pipeline right now |
@@ -182,7 +183,7 @@ cd agent-second-brain && bash upgrade.sh
```
vault/
-├── daily/ # Your daily entries (voice, text)
+├── daily/ # Your daily entries (voice, text, attachments)
├── goals/ # Vision → yearly → monthly → weekly
├── business/
│ ├── crm/ # Client cards
diff --git a/README.ru.md b/README.ru.md
index 1e62240..3576f18 100644
--- a/README.ru.md
+++ b/README.ru.md
@@ -24,7 +24,7 @@
## Что делает эта штука
-Ты пишешь боту в Telegram. Голосовые или текст - как удобно. Не думаешь про категории, теги или куда что положить.
+Ты пишешь боту в Telegram. Голосовые, текст, фото, файлы, пересланные посты - как удобно. Не думаешь про категории, теги или куда что положить.
Агент разбирается сам:
@@ -93,6 +93,7 @@ Agent Second Brain использует кривую забывания Эбби
|----------------|-------------|
| Голосовое о звонке с клиентом | Расшифровывает, создаёт CRM-карточку с follow-up |
| Текст: «идея для Q2 кампании» | Сохраняет в папку идей, связывает с похожими заметками |
+| Пересланный пост или фото доски | Сохраняет файл в хранилище, агент сам читает его и раскладывает суть |
| «Напомни про звонок завтра в 15:00» | Создаёт разовое задание по расписанию, пингует в Telegram |
| «По будням в 18:30 проверяй папку inbox» | Создаёт регулярное задание, которое молчит, если ничего нового |
| Кнопка «Process» | Запускает полный цикл обработки прямо сейчас |
@@ -182,7 +183,7 @@ cd agent-second-brain && bash upgrade.sh
```
vault/
-├── daily/ # Дневные записи (голосовые, текст)
+├── daily/ # Дневные записи (голосовые, текст, вложения)
├── goals/ # Видение → год → месяц → неделя
├── business/
│ ├── crm/ # Карточки клиентов
diff --git a/src/d_brain/bot/handlers/chat.py b/src/d_brain/bot/handlers/chat.py
index e53b51b..6d59a3a 100644
--- a/src/d_brain/bot/handlers/chat.py
+++ b/src/d_brain/bot/handlers/chat.py
@@ -9,6 +9,7 @@ import asyncio
import html
import logging
from datetime import datetime
+from typing import Any
from aiogram import Bot, F, Router
from aiogram.enums import ChatType
@@ -150,6 +151,74 @@ async def _typing_loop(bot: Bot, chat_id: int) -> None:
pass
+# --- Media input (photo / document / video / audio / animation / video_note) ---
+
+UNSUPPORTED_REPLY = (
+ "Я принимаю голос, текст, фото и файлы. "
+ "Этот тип сообщения обработать не могу."
+)
+
+_MEDIA_EXTRACTORS = (
+ # (kind, attr, default extension)
+ ("document", "document", None),
+ ("video", "video", "mp4"),
+ ("audio", "audio", "mp3"),
+ ("animation", "animation", "mp4"),
+ ("video_note", "video_note", "mp4"),
+)
+
+
+def extract_media(message: Any) -> tuple[str, str, str, str | None]:
+ """(kind, file_id, extension, original_name) for a media message.
+
+ Photos are a size ladder — take the largest. Documents/audio keep the
+ original file name (its extension wins over the default).
+ """
+ if getattr(message, "photo", None):
+ return ("photo", message.photo[-1].file_id, "jpg", None)
+ for kind, attr, default_ext in _MEDIA_EXTRACTORS:
+ obj = getattr(message, attr, None)
+ if obj is None:
+ continue
+ name = getattr(obj, "file_name", None)
+ ext = default_ext or "bin"
+ if name and "." in name:
+ ext = name.rsplit(".", 1)[-1].lower()
+ return (kind, obj.file_id, ext, name)
+ raise ValueError("message carries no known media")
+
+
+def forward_note(origin: Any) -> str:
+ """Human-readable forward attribution, or '' for a non-forward."""
+ if origin is None:
+ return ""
+ user = getattr(origin, "sender_user", None)
+ if user is not None:
+ return f"[переслано от: {user.full_name}]\n"
+ chat = getattr(origin, "chat", None)
+ if chat is not None:
+ return f"[переслано из: {chat.title}]\n"
+ name = getattr(origin, "sender_user_name", None)
+ if name:
+ return f"[переслано от: {name}]\n"
+ return "[переслано]\n"
+
+
+def build_media_prompt(
+ *, kind: str, rel_path: str, original_name: str | None, caption: str, fwd: str
+) -> str:
+ """Prompt for the brain: it lives in the vault and can Read the file
+ itself (images, PDFs, text) — we only hand it the path and context."""
+ name_part = f" (имя файла: {original_name})" if original_name else ""
+ caption_part = f"\nПодпись: {caption}" if caption else ""
+ return (
+ f"{fwd}Пользователь прислал {kind}: {rel_path}{name_part}{caption_part}\n"
+ "Прочитай файл (Read поддерживает изображения и PDF; для "
+ "видео/аудио опиши по подписи и контексту), сохрани суть в память "
+ "по правилам vault и кратко ответь, что сохранил."
+ )
+
+
# --- Handlers ---
@@ -216,17 +285,85 @@ async def handle_chat_text(message: Message, bot: Bot) -> None:
settings = get_settings()
storage = VaultStorage(settings.vault_path)
+ fwd = forward_note(getattr(message, "forward_origin", None))
+ text = f"{fwd}{message.text}" if fwd else message.text
+
# Safety net: save to daily
timestamp = datetime.fromtimestamp(message.date.timestamp())
- storage.append_to_daily(message.text, timestamp, "[text]")
+ storage.append_to_daily(text, timestamp, "[forward]" if fwd else "[text]")
# Log to session
session = SessionStore(settings.vault_path)
session.append(
message.from_user.id,
"text",
- text=message.text,
+ text=text,
msg_id=message.message_id,
)
- await _dispatch_text(bot, message.chat.id, message.from_user.id, message.text)
+ await _dispatch_text(bot, message.chat.id, message.from_user.id, text)
+
+
+ F.photo | F.document | F.video | F.audio | F.animation | F.video_note
+)
+async def handle_chat_media(message: Message, bot: Bot) -> None:
+ """Handle any file-bearing message: download into the vault's
+ attachments and hand the PATH to the brain — it reads the file itself."""
+ if not message.from_user:
+ return
+
+ settings = get_settings()
+ storage = VaultStorage(settings.vault_path)
+
+ try:
+ kind, file_id, ext, original_name = extract_media(message)
+ file = await bot.get_file(file_id)
+ if not file.file_path:
+ await message.answer("Не удалось скачать файл.")
+ return
+ file_bytes = await bot.download_file(file.file_path)
+ if not file_bytes:
+ await message.answer("Не удалось скачать файл.")
+ return
+
+ timestamp = datetime.fromtimestamp(message.date.timestamp())
+ rel_path = storage.save_attachment(
+ file_bytes.read(), timestamp.date(), timestamp, ext
+ )
+
+ caption = message.caption or ""
+ fwd = forward_note(getattr(message, "forward_origin", None))
+
+ # Safety net: save to daily with an Obsidian embed
+ daily_entry = f"{fwd}![[{rel_path}]]"
+ if caption:
+ daily_entry += f"\n\n{caption}"
+ storage.append_to_daily(daily_entry, timestamp, f"[{kind}]")
+
+ session = SessionStore(settings.vault_path)
+ session.append(
+ message.from_user.id,
+ kind,
+ text=caption or rel_path,
+ msg_id=message.message_id,
+ )
+
+ prompt = build_media_prompt(
+ kind=kind,
+ rel_path=rel_path,
+ original_name=original_name,
+ caption=caption,
+ fwd=fwd,
+ )
+ await _process_and_reply(bot, message.chat.id, message.from_user.id, prompt)
+
+ except Exception as e:
+ logger.exception("Error processing media in chat")
+ await message.answer(f"Error: {e}")
+
+
+async def handle_chat_other(message: Message) -> None:
+ """Catch-all: never go silent — tell the user what the bot accepts."""
+ await message.answer(UNSUPPORTED_REPLY)
diff --git a/tests/test_chat_handler.py b/tests/test_chat_handler.py
index b9b6803..9c3b246 100644
--- a/tests/test_chat_handler.py
+++ b/tests/test_chat_handler.py
@@ -170,3 +170,77 @@ def test_text_when_idle_goes_to_normal_turn(monkeypatch):
asyncio.run(chat._dispatch_text(bot, chat_id=10, user_id=1, text="привет"))
assert mgr.sent == [(1, "привет")]
assert mgr.steered == []
+
+
+# ── media input: photo / document / video / audio (v3.0.x) ────────────────
+
+
+class _Stub:
+ def __init__(self, **kw):
+ self.__dict__.update(kw)
+
+
+def test_extract_media_photo_takes_largest():
+ from d_brain.bot.handlers.chat import extract_media
+
+ msg = _Stub(
+ photo=[_Stub(file_id="small"), _Stub(file_id="big")],
+ document=None, video=None, audio=None, animation=None, video_note=None,
+ )
+ kind, file_id, ext, name = extract_media(msg)
+ assert (kind, file_id, ext) == ("photo", "big", "jpg")
+
+
+def test_extract_media_document_keeps_name_and_ext():
+ from d_brain.bot.handlers.chat import extract_media
+
+ doc = _Stub(file_id="d1", file_name="report Q2.pdf")
+ msg = _Stub(photo=None, document=doc, video=None, audio=None,
+ animation=None, video_note=None)
+ kind, file_id, ext, name = extract_media(msg)
+ assert (kind, file_id, ext, name) == ("document", "d1", "pdf", "report Q2.pdf")
+
+
+def test_extract_media_video_note_is_mp4():
+ from d_brain.bot.handlers.chat import extract_media
+
+ msg = _Stub(photo=None, document=None, video=None, audio=None,
+ animation=None, video_note=_Stub(file_id="v1"))
+ kind, file_id, ext, name = extract_media(msg)
+ assert (kind, ext) == ("video_note", "mp4")
+
+
+def test_forward_note_variants():
+ from d_brain.bot.handlers.chat import forward_note
+
+ user = _Stub(sender_user=_Stub(full_name="Ivan Petrov"))
+ assert "Ivan Petrov" in forward_note(user)
+ channel = _Stub(sender_user=None, chat=_Stub(title="AI News"))
+ assert "AI News" in forward_note(channel)
+ hidden = _Stub(sender_user=None, chat=None, sender_user_name="Hidden Guy")
+ assert "Hidden Guy" in forward_note(hidden)
+ assert forward_note(None) == ""
+
+
+def test_build_media_prompt_contract():
+ from d_brain.bot.handlers.chat import build_media_prompt
+
+ p = build_media_prompt(
+ kind="document",
+ rel_path="attachments/2026-06-10/img-120000.pdf",
+ original_name="report.pdf",
+ caption="квартальный отчёт",
+ fwd="[переслано от: Ivan]\n",
+ )
+ assert "attachments/2026-06-10/img-120000.pdf" in p
+ assert "report.pdf" in p
+ assert "квартальный отчёт" in p
+ assert "Ivan" in p
+ # the brain must be told to actually open the file
+ assert "Read" in p or "прочитай" in p.lower() or "посмотри" in p.lower()
+
+
+def test_unsupported_content_reply_exists():
+ from d_brain.bot.handlers.chat import UNSUPPORTED_REPLY
+
+ assert "голос" in UNSUPPORTED_REPLY or "voice" in UNSUPPORTED_REPLY.lower()