11#!/usr/bin/env node
22import fs from "node:fs" ;
3+ import path from "node:path" ;
34import { createInterface } from "node:readline/promises" ;
45
56import { Command } from "commander" ;
@@ -23,7 +24,9 @@ const program = new Command();
2324program
2425 . name ( "owpenbot" )
2526 . description ( "OpenCode WhatsApp + Telegram bridge" )
26- . argument ( "[path]" ) ;
27+ . argument ( "[path]" )
28+ . option ( "--non-interactive" , "Run setup defaults and exit" , false )
29+ . option ( "--check" , "Validate config/auth and exit" , false ) ;
2730
2831const runStart = async ( pathOverride ?: string ) => {
2932 if ( pathOverride ?. trim ( ) ) {
@@ -35,7 +38,7 @@ const runStart = async (pathOverride?: string) => {
3538 process . env . OPENCODE_DIRECTORY = config . opencodeDirectory ;
3639 }
3740 const bridge = await startBridge ( config , logger ) ;
38- logger . info ( "Commands: owpenbot whatsapp login, owpenbot pairing list, owpenbot status" ) ;
41+ logger . info ( "Commands: owpenwork whatsapp login, owpenwork pairing list, owpenwork status" ) ;
3942
4043 const shutdown = async ( ) => {
4144 logger . info ( "shutting down" ) ;
@@ -47,6 +50,132 @@ const runStart = async (pathOverride?: string) => {
4750 process . on ( "SIGTERM" , shutdown ) ;
4851} ;
4952
53+ async function runSetupWizard ( config : ReturnType < typeof loadConfig > , nonInteractive : boolean ) {
54+ const { config : existing } = readConfigFile ( config . configPath ) ;
55+ const next : OwpenbotConfigFile = existing ?? { version : 1 } ;
56+
57+ if ( nonInteractive ) {
58+ next . version = 1 ;
59+ next . channels = next . channels ?? { } ;
60+ next . channels . whatsapp = {
61+ dmPolicy : "pairing" ,
62+ allowFrom : [ ] ,
63+ selfChatMode : false ,
64+ accounts : {
65+ [ config . whatsappAccountId ] : {
66+ authDir : config . whatsappAuthDir ,
67+ } ,
68+ } ,
69+ } ;
70+ writeConfigFile ( config . configPath , next ) ;
71+ console . log ( `Wrote ${ config . configPath } ` ) ;
72+ return ;
73+ }
74+
75+ const rl = createInterface ( { input : process . stdin , output : process . stdout } ) ;
76+ const phoneMode = await rl . question (
77+ "WhatsApp setup: (1) Personal number (2) Dedicated number [1]: " ,
78+ ) ;
79+ const mode = phoneMode . trim ( ) === "2" ? "dedicated" : "personal" ;
80+
81+ let dmPolicy : DmPolicy = "pairing" ;
82+ let allowFrom : string [ ] = [ ] ;
83+ let selfChatMode = false ;
84+
85+ if ( mode === "personal" ) {
86+ let normalized = "" ;
87+ while ( ! normalized ) {
88+ const number = await rl . question ( "Your WhatsApp number (E.164, e.g. +15551234567): " ) ;
89+ const candidate = normalizeWhatsAppId ( number ) ;
90+ if ( ! / ^ \+ \d + $ / . test ( candidate ) ) {
91+ console . log ( "Invalid number. Try again." ) ;
92+ continue ;
93+ }
94+ normalized = candidate ;
95+ }
96+ allowFrom = [ normalized ] ;
97+ dmPolicy = "allowlist" ;
98+ selfChatMode = true ;
99+ } else {
100+ const policyInput = await rl . question (
101+ "DM policy: (1) Pairing (2) Allowlist (3) Open (4) Disabled [1]: " ,
102+ ) ;
103+ const policyChoice = policyInput . trim ( ) ;
104+ if ( policyChoice === "2" ) dmPolicy = "allowlist" ;
105+ else if ( policyChoice === "3" ) dmPolicy = "open" ;
106+ else if ( policyChoice === "4" ) dmPolicy = "disabled" ;
107+ else dmPolicy = "pairing" ;
108+
109+ const listInput = await rl . question (
110+ "Allowlist numbers (comma-separated, optional): " ,
111+ ) ;
112+ if ( listInput . trim ( ) ) {
113+ allowFrom = listInput
114+ . split ( "," )
115+ . map ( ( item ) => normalizeWhatsAppId ( item ) )
116+ . filter ( Boolean ) ;
117+ }
118+ if ( dmPolicy === "open" ) {
119+ allowFrom = allowFrom . length ? allowFrom : [ "*" ] ;
120+ }
121+ }
122+
123+ rl . close ( ) ;
124+
125+ next . version = 1 ;
126+ next . channels = next . channels ?? { } ;
127+ next . channels . whatsapp = {
128+ dmPolicy,
129+ allowFrom,
130+ selfChatMode,
131+ accounts : {
132+ [ config . whatsappAccountId ] : {
133+ authDir : config . whatsappAuthDir ,
134+ } ,
135+ } ,
136+ } ;
137+ writeConfigFile ( config . configPath , next ) ;
138+ console . log ( `Wrote ${ config . configPath } ` ) ;
139+ }
140+
141+ async function runGuidedFlow ( pathArg : string | undefined , opts : { nonInteractive : boolean ; check : boolean } ) {
142+ if ( pathArg ?. trim ( ) ) {
143+ process . env . OPENCODE_DIRECTORY = pathArg . trim ( ) ;
144+ }
145+ let config = loadConfig ( process . env , { requireOpencode : false } ) ;
146+ if ( ! process . env . OPENCODE_DIRECTORY ) {
147+ process . env . OPENCODE_DIRECTORY = config . opencodeDirectory ;
148+ }
149+
150+ if ( opts . check ) {
151+ const authPath = path . join ( config . whatsappAuthDir , "creds.json" ) ;
152+ const linked = fs . existsSync ( authPath ) ;
153+ const cfgExists = fs . existsSync ( config . configPath ) ;
154+ console . log ( `Config: ${ cfgExists ? "yes" : "no" } ` ) ;
155+ console . log ( `WhatsApp linked: ${ linked ? "yes" : "no" } ` ) ;
156+ process . exit ( linked && cfgExists ? 0 : 1 ) ;
157+ }
158+
159+ const cfgExists = fs . existsSync ( config . configPath ) ;
160+ const cfgHasWhatsApp = config . configFile . channels ?. whatsapp ;
161+ if ( ! cfgExists || ! cfgHasWhatsApp ) {
162+ await runSetupWizard ( config , opts . nonInteractive ) ;
163+ if ( opts . nonInteractive ) {
164+ console . log ( "Next: owpenwork whatsapp login" ) ;
165+ return ;
166+ }
167+ }
168+
169+ config = loadConfig ( process . env , { requireOpencode : false } ) ;
170+
171+ const authPath = path . join ( config . whatsappAuthDir , "creds.json" ) ;
172+ if ( ! fs . existsSync ( authPath ) ) {
173+ await loginWhatsApp ( config , createLogger ( config . logLevel ) ) ;
174+ }
175+
176+ await runStart ( pathArg ) ;
177+ }
178+
50179program
51180 . command ( "start" )
52181 . description ( "Start the bridge" )
@@ -57,7 +186,8 @@ program.action((pathArg: string | undefined) => {
57186 program . outputHelp ( ) ;
58187 return ;
59188 }
60- return runStart ( pathArg ) ;
189+ const opts = program . opts < { nonInteractive : boolean ; check : boolean } > ( ) ;
190+ return runGuidedFlow ( pathArg , { nonInteractive : Boolean ( opts . nonInteractive ) , check : Boolean ( opts . check ) } ) ;
61191} ) ;
62192
63193program
@@ -66,91 +196,7 @@ program
66196 . option ( "--non-interactive" , "Write defaults without prompts" , false )
67197 . action ( async ( opts ) => {
68198 const config = loadConfig ( process . env , { requireOpencode : false } ) ;
69- const { config : existing } = readConfigFile ( config . configPath ) ;
70- const next : OwpenbotConfigFile = existing ?? { version : 1 } ;
71-
72- if ( opts . nonInteractive ) {
73- next . version = 1 ;
74- next . channels = next . channels ?? { } ;
75- next . channels . whatsapp = {
76- dmPolicy : "pairing" ,
77- allowFrom : [ ] ,
78- selfChatMode : false ,
79- accounts : {
80- [ config . whatsappAccountId ] : {
81- authDir : config . whatsappAuthDir ,
82- } ,
83- } ,
84- } ;
85- writeConfigFile ( config . configPath , next ) ;
86- console . log ( `Wrote ${ config . configPath } ` ) ;
87- return ;
88- }
89-
90- const rl = createInterface ( { input : process . stdin , output : process . stdout } ) ;
91- const phoneMode = await rl . question (
92- "WhatsApp setup: (1) Personal number (2) Dedicated number [1]: " ,
93- ) ;
94- const mode = phoneMode . trim ( ) === "2" ? "dedicated" : "personal" ;
95-
96- let dmPolicy : DmPolicy = "pairing" ;
97- let allowFrom : string [ ] = [ ] ;
98- let selfChatMode = false ;
99-
100- if ( mode === "personal" ) {
101- let normalized = "" ;
102- while ( ! normalized ) {
103- const number = await rl . question ( "Your WhatsApp number (E.164, e.g. +15551234567): " ) ;
104- const candidate = normalizeWhatsAppId ( number ) ;
105- if ( ! / ^ \+ \d + $ / . test ( candidate ) ) {
106- console . log ( "Invalid number. Try again." ) ;
107- continue ;
108- }
109- normalized = candidate ;
110- }
111- allowFrom = [ normalized ] ;
112- dmPolicy = "allowlist" ;
113- selfChatMode = true ;
114- } else {
115- const policyInput = await rl . question (
116- "DM policy: (1) Pairing (2) Allowlist (3) Open (4) Disabled [1]: " ,
117- ) ;
118- const policyChoice = policyInput . trim ( ) ;
119- if ( policyChoice === "2" ) dmPolicy = "allowlist" ;
120- else if ( policyChoice === "3" ) dmPolicy = "open" ;
121- else if ( policyChoice === "4" ) dmPolicy = "disabled" ;
122- else dmPolicy = "pairing" ;
123-
124- const listInput = await rl . question (
125- "Allowlist numbers (comma-separated, optional): " ,
126- ) ;
127- if ( listInput . trim ( ) ) {
128- allowFrom = listInput
129- . split ( "," )
130- . map ( ( item ) => normalizeWhatsAppId ( item ) )
131- . filter ( Boolean ) ;
132- }
133- if ( dmPolicy === "open" ) {
134- allowFrom = allowFrom . length ? allowFrom : [ "*" ] ;
135- }
136- }
137-
138- rl . close ( ) ;
139-
140- next . version = 1 ;
141- next . channels = next . channels ?? { } ;
142- next . channels . whatsapp = {
143- dmPolicy,
144- allowFrom,
145- selfChatMode,
146- accounts : {
147- [ config . whatsappAccountId ] : {
148- authDir : config . whatsappAuthDir ,
149- } ,
150- } ,
151- } ;
152- writeConfigFile ( config . configPath , next ) ;
153- console . log ( `Wrote ${ config . configPath } ` ) ;
199+ await runSetupWizard ( config , Boolean ( opts . nonInteractive ) ) ;
154200 } ) ;
155201
156202program
0 commit comments