@@ -128,14 +128,49 @@ const ImessageReadCommand = cmd({
128128 }
129129
130130 if ( ! args . query ?. trim ( ) ) {
131- prompts . log . error ( "Please provide a phone number or chat identifier " )
131+ prompts . log . error ( "Please provide a phone number, lead ID, or contact name " )
132132 prompts . outro ( "Done" )
133133 return
134134 }
135135
136- const digits = args . query . replace ( / \D / g, "" )
137- const isPhone = digits . length >= 7
138- const normalized = isPhone ? normalizeHandle ( args . query ) : args . query
136+ let digits = args . query . replace ( / \D / g, "" )
137+ let isPhone = digits . length >= 7
138+ let normalized = isPhone ? normalizeHandle ( args . query ) : args . query
139+
140+ // If not a phone number, try to resolve as lead ID or name
141+ const isLeadId = / ^ \d + $ / . test ( args . query . trim ( ) ) && digits . length < 7
142+ if ( isLeadId || ! isPhone ) {
143+ try {
144+ const { irisFetch : _fetch } = await import ( "./iris-api" )
145+ if ( isLeadId ) {
146+ const res = await _fetch ( `/api/v1/leads/${ args . query . trim ( ) } ` )
147+ if ( res . ok ) {
148+ const data = ( await res . json ( ) ) as any
149+ const lead = data ?. data ?? data
150+ if ( lead ?. phone ) {
151+ const name = lead . name || lead . nickname || `Lead #${ args . query } `
152+ if ( ! args . json ) prompts . log . info ( `Resolved lead #${ args . query } → ${ name } (${ lead . phone } )` )
153+ normalized = normalizeHandle ( lead . phone )
154+ isPhone = true
155+ }
156+ }
157+ } else {
158+ const res = await _fetch ( `/api/v1/leads?search=${ encodeURIComponent ( args . query ) } &per_page=5` )
159+ if ( res . ok ) {
160+ const data = ( await res . json ( ) ) as any
161+ const leads = data ?. data ?. data ?? data ?. data ?? [ ]
162+ const withPhone = Array . isArray ( leads ) ? leads . find ( ( l : any ) => l . phone ) : null
163+ if ( withPhone ) {
164+ const name = withPhone . name || withPhone . nickname || args . query
165+ if ( ! args . json ) prompts . log . info ( `Resolved "${ args . query } " → ${ name } (${ withPhone . phone } )` )
166+ normalized = normalizeHandle ( withPhone . phone )
167+ isPhone = true
168+ }
169+ }
170+ }
171+ } catch { }
172+ }
173+
139174 const whereClause = isPhone
140175 ? `c.chat_identifier LIKE '%${ normalized } %'`
141176 : `c.chat_identifier LIKE '%${ args . query . replace ( / ' / g, "''" ) } %'`
@@ -254,7 +289,7 @@ const ImessageSendCommand = cmd({
254289 describe : "send an iMessage to a phone number or contact" ,
255290 builder : ( yargs ) =>
256291 yargs
257- . positional ( "handle" , { type : "string" , demandOption : true , describe : "phone number or Apple ID email " } )
292+ . positional ( "handle" , { type : "string" , demandOption : true , describe : "phone number, lead ID, or contact name " } )
258293 . positional ( "message" , { type : "string" , demandOption : true , describe : "message text to send" } ) ,
259294 async handler ( args ) {
260295 UI . empty ( )
@@ -266,11 +301,62 @@ const ImessageSendCommand = cmd({
266301 return
267302 }
268303
269- // Normalize phone — prepend +1 if 10 digits
304+ // Resolve handle: could be phone, lead ID, or contact name
270305 let handle = args . handle . trim ( )
271306 const digits = handle . replace ( / \D / g, "" )
272- if ( digits . length === 10 ) handle = `+1${ digits } `
273- else if ( digits . length === 11 && digits . startsWith ( "1" ) ) handle = `+${ digits } `
307+
308+ // Check if it's a lead ID (pure number, < 7 digits — not a phone)
309+ const isLeadId = / ^ \d + $ / . test ( handle ) && digits . length < 7
310+ const isPhone = ! isLeadId && digits . length >= 7
311+
312+ if ( isLeadId || ( ! isPhone && ! handle . includes ( "@" ) ) ) {
313+ // Resolve from CRM
314+ try {
315+ const { irisFetch : _fetch } = await import ( "./iris-api" )
316+ if ( isLeadId ) {
317+ const res = await _fetch ( `/api/v1/leads/${ handle } ` )
318+ if ( res . ok ) {
319+ const data = ( await res . json ( ) ) as any
320+ const lead = data ?. data ?? data
321+ if ( lead ?. phone ) {
322+ const name = lead . name || lead . nickname || `Lead #${ handle } `
323+ prompts . log . info ( `Resolved lead #${ handle } → ${ name } (${ lead . phone } )` )
324+ handle = lead . phone
325+ } else {
326+ prompts . log . error ( `Lead #${ handle } has no phone number` )
327+ prompts . outro ( "Done" )
328+ return
329+ }
330+ } else {
331+ prompts . log . error ( `Lead #${ handle } not found` )
332+ prompts . outro ( "Done" )
333+ return
334+ }
335+ } else {
336+ // Search by name
337+ const res = await _fetch ( `/api/v1/leads?search=${ encodeURIComponent ( handle ) } &per_page=5` )
338+ if ( res . ok ) {
339+ const data = ( await res . json ( ) ) as any
340+ const leads = data ?. data ?. data ?? data ?. data ?? [ ]
341+ const withPhone = Array . isArray ( leads ) ? leads . find ( ( l : any ) => l . phone ) : null
342+ if ( withPhone ) {
343+ const name = withPhone . name || withPhone . nickname || handle
344+ prompts . log . info ( `Resolved "${ handle } " → ${ name } (${ withPhone . phone } )` )
345+ handle = withPhone . phone
346+ } else {
347+ prompts . log . error ( `No lead with phone found for "${ handle } "` )
348+ prompts . outro ( "Done" )
349+ return
350+ }
351+ }
352+ }
353+ } catch { }
354+ }
355+
356+ // Normalize phone — prepend +1 if 10 digits
357+ const finalDigits = handle . replace ( / \D / g, "" )
358+ if ( finalDigits . length === 10 ) handle = `+1${ finalDigits } `
359+ else if ( finalDigits . length === 11 && finalDigits . startsWith ( "1" ) ) handle = `+${ finalDigits } `
274360
275361 // Clean up stray backslash escapes from upstream (MCP/shell) then escape for AppleScript
276362 const cleanMessage = args . message . replace ( / \\ ( [ ^ \\ ] ) / g, "$1" )
0 commit comments