Removed bad opener. Allow for custom openers.

This commit is contained in:
2025-10-03 17:28:26 -07:00
parent 8a5fac0709
commit 2fbda0c4cd
3 changed files with 25 additions and 9 deletions

View File

@@ -32,5 +32,11 @@ MAX_TOKENS_HISTORY=2200
# Hard cap on number of messages kept in history
MAX_MESSAGES_HISTORY=30
# Optional opener (disabled by default).
# When enabled, the opener is sent only on a fresh start (no history),
# when a target is already resolved, and no catch-up reply was sent.
AUTO_OPENER_ENABLED=false
OPENER_TEXT=
# Optional: ensure unbuffered logs in some environments
PYTHONUNBUFFERED=1

5
README
View File

@@ -24,12 +24,15 @@ Telethon + OpenAI bot that engages unsolicited DMs with safe, time-wasting small
| HISTORY_FILE | no | chat_history.jsonl | Path to local JSONL file for conversation history |
| MAX_TOKENS_HISTORY | no | 2200 | Rough token budget for messages passed to the model |
| MAX_MESSAGES_HISTORY | no | 30 | Hard cap on number of messages kept in rolling history |
| AUTO_OPENER_ENABLED | no | false | If true, send an initial opener only on fresh start when a target is resolved and no catch-up reply was needed |
| OPENER_TEXT | no | — | The opener text to send when AUTO_OPENER_ENABLED=true |
| PYTHONUNBUFFERED | no | 1 | If set, forces unbuffered output in some environments |
Notes:
- Set one of TARGET_USERNAME, TARGET_USER_ID, or TARGET_DISPLAY_NAME. If none are set, the first inbound DM will become the target automatically.
- The opener is disabled by default to avoid sending context-specific messages; enable explicitly with AUTO_OPENER_ENABLED and provide OPENER_TEXT.
- Increase delays if you hit Telegram flood limits.
## License
MIT License — see LICENSE file for details.
MIT License — see LICENSE file for details.

23
main.py
View File

@@ -37,6 +37,10 @@ MAX_MESSAGES_HISTORY = int(os.environ.get("MAX_MESSAGES_HISTORY", "30"))
TARGET_DISPLAY_NAME = os.environ.get("TARGET_DISPLAY_NAME", "").strip()
TARGET_CACHE_FILE = Path(os.environ.get("TARGET_CACHE_FILE", "target_id.txt"))
# Opener controls (disabled by default)
AUTO_OPENER_ENABLED = os.environ.get("AUTO_OPENER_ENABLED", "false").lower() == "true"
OPENER_TEXT = os.environ.get("OPENER_TEXT", "").strip()
# ---------- Validation ----------
def _require(cond: bool, msg: str):
if not cond:
@@ -343,14 +347,17 @@ async def main():
if target_entity:
catchup_sent = await startup_catchup_if_needed(client, target_entity)
# Optional: send a gentle opener once (only if history is empty and we didn't just catch up)
if not HISTORY_FILE.exists() and not catchup_sent:
opener = "Oh neat, Houston. I'm up in the Pacific Northwest these days, sort of near the coast. What brought you from the UK to Houston?"
append_history("assistant", opener)
if target_entity:
await safe_send(client, target_entity, opener)
else:
print("Opener queued in history; will start replying when the target speaks.")
# Optional opener: disabled by default. If enabled, only send when:
# - no history file exists (fresh start)
# - no catch-up reply was sent
# - a target is already resolved
# - and OPENER_TEXT is provided
if not HISTORY_FILE.exists() and not catchup_sent and target_entity and AUTO_OPENER_ENABLED and OPENER_TEXT:
append_history("assistant", OPENER_TEXT)
await safe_send(client, target_entity, OPENER_TEXT)
elif not HISTORY_FILE.exists() and not catchup_sent and not AUTO_OPENER_ENABLED:
print("Opener is disabled (AUTO_OPENER_ENABLED=false). Waiting for incoming message.")
@client.on(events.NewMessage(incoming=True))
async def on_msg(event):