| 115 | }; |
| 116 | |
| 117 | export function registerBotMessageCommands(bot: Command) { |
| 118 | const message = bot |
| 119 | .command('message') |
| 120 | .description('Send and manage messages on connected platforms'); |
| 121 | |
| 122 | // ── send ──────────────────────────────────────────────── |
| 123 | |
| 124 | message |
| 125 | .command('send <botIdOrAtKey>') |
| 126 | .description( |
| 127 | 'Send a message to a channel. Pass a per-agent bot id, or "@<messenger-install-id>" ' + |
| 128 | 'to send through a System Bot messenger installation (see `lh bot messengers list`).', |
| 129 | ) |
| 130 | .requiredOption('--target <channelId>', 'Target channel / conversation ID') |
| 131 | .requiredOption('--message <text>', 'Message content') |
| 132 | .option( |
| 133 | '--attachment <pathOrUrl>', |
| 134 | 'Attach a file by local path or remote URL (repeatable). ' + |
| 135 | 'Local paths are base64-encoded; http(s) URLs are passed as fetchUrl.', |
| 136 | collectOptions, |
| 137 | [], |
| 138 | ) |
| 139 | .option('--reply-to <messageId>', 'Reply to a specific message') |
| 140 | .option('--json', 'Output JSON') |
| 141 | .action( |
| 142 | async ( |
| 143 | botIdOrAtKey: string, |
| 144 | options: { |
| 145 | attachment: string[]; |
| 146 | json?: boolean; |
| 147 | message: string; |
| 148 | replyTo?: string; |
| 149 | target: string; |
| 150 | }, |
| 151 | ) => { |
| 152 | const attachments = await resolveAttachmentFlags(options.attachment); |
| 153 | const target = resolveSendTargetArg(botIdOrAtKey); |
| 154 | |
| 155 | const client = await getTrpcClient(); |
| 156 | const result = await client.botMessage.sendMessage.mutate({ |
| 157 | ...target, |
| 158 | attachments, |
| 159 | channelId: options.target, |
| 160 | content: options.message, |
| 161 | replyTo: options.replyTo, |
| 162 | }); |
| 163 | |
| 164 | if (options.json) { |
| 165 | outputJson(result); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | const r = result as any; |
| 170 | const suffix = attachments?.length ? ` with ${attachments.length} attachment(s)` : ''; |
| 171 | console.log( |
| 172 | `${pc.green('✓')} Message sent${r.messageId ? ` (${pc.dim(r.messageId)})` : ''}${suffix}`, |
| 173 | ); |
| 174 | }, |