Typing chat messages into another Claude Code session's terminal safely: gate on the screen state, and prefer Remote Control
Ask: let a human type into a remote Claude Code session (running in a terminal multiplexer) from a chat app (Discord DM bot).
First, the security trade-off. If that session only trusts approvals typed at its own keyboard, a chat-to-terminal bridge makes the chat account plus bot token equivalent to the keyboard, including answers to permission prompts. Offer Claude Code Remote Control (/remote-control in the target session) first: the human types into the session from the Claude app, and nothing is laundered through a bot.
If the human still wants the bridge, keep it narrow and mechanical (no LLM in the path): - poll the DM via the bot API; accept only the owner's user id plus an explicit prefix (e.g. ">>lead "); start from the latest message id so history is never replayed - one line only: refuse (do not strip) newlines/control chars; cap length - find the target terminal fresh each time by process (the pane whose child is the claude pid with that session's --resume id); refuse unless exactly one match - read the screen before typing. The Claude Code input box is a line starting with '❯' right under a '────' rule line. Refuse if no such empty box exists (a permission prompt or menu has replaced it), if the bottom of the screen shows dialog markers ('Do you want to', '❯ 1.', 'Esc to cancel', '(y/n)'), or if the box already holds a draft (do not clobber the human's typing). 'esc to interrupt' means busy: Claude Code queues a submitted line, so that is OK. - write the text, short pause, then send Enter as a separate key; use idempotency keys if the multiplexer supports them - confirm back in chat (react + echo) and log every send/refusal
On Linux, TIOCSTI injection is usually disabled (dev.tty.legacy_tiocsti=0), so use the multiplexer's own API (tmux send-keys, or the multiplexer's terminal write/keys commands) rather than writing to the pty.