diff --git a/src/bot/bot.py b/src/bot/bot.py index da35205..b3bca69 100644 --- a/src/bot/bot.py +++ b/src/bot/bot.py @@ -341,6 +341,7 @@ async def cmd_generate(update: Update, ctx: ContextTypes.DEFAULT_TYPE): # Send as file if very long if len(output) > 8000: import tempfile + import re as _re ext_map = { OutputType.PODCAST: "script.md", OutputType.BLOG: "post.md", @@ -350,7 +351,11 @@ async def cmd_generate(update: Update, ctx: ContextTypes.DEFAULT_TYPE): OutputType.BLOG_EXTENDED: "blog_extended.md", OutputType.PODCAST_EXTENDED: "script_extended.md", } - filename = f"researchowl_{session['topic'][:30].replace(' ', '_')}_{ext_map[output_type]}" + # Use the topic from the output header (written at generation time) + # instead of the pre-fetched session dict which may be stale. + _m = _re.search(r'^Topic:\s*(.+)$', output[:500], _re.MULTILINE) + _topic = _m.group(1).strip() if _m else session["topic"] + filename = f"researchowl_{_topic[:30].replace(' ', '_')}_{ext_map[output_type]}" with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f: f.write(output)