import type { SuperEvent } from './super-event.js' ; import { log , VERSION , } from './config.js' ; import type { AppserviceManager } from './appservice-manager.js' ; import type { UserManager } from './user-manager.js' ; import type { WechatyManager } from './wechaty-manager.js' ; import { Manager } from './manager.js' ; export class DialogManager extends Manager { public appserviceManager ! : AppserviceManager ; public userManager ! : UserManager ; public wechatyManager ! : WechatyManager ; constructor ( ) { super ( ) ; log . verbose ( 'Dialogmanager' , 'constructor()' ) ; } public teamManager ( managers ) { this . appserviceManager = managers . appserviceManager ; this . userManager = managers . userManager ; this . wechatyManager = managers . wechatyManager ; } public async gotoEnableWechatyDialog ( superEvent ) { log . verbose ( 'MatrixHandler' , 'gotoEnableDialog()' ) ; const room = superEvent . room ( ) ; const matrixUser = superEvent . sender ( ) ; const intent = this . appserviceManager . bridge . getIntent ( ) ; await intent . sendText ( room . getId ( ) , 'You are not enable `matrix-appservice-wechaty` yet. Please talk to the `wechaty` bot to check you in.' ) ; await this . userManager . enable ( matrixUser ) ; await intent . sendText ( room . getId ( ) , 'I had enabled it for you ;-)' ) ; } public async gotoSetupDialog ( superEvent ) { log . verbose ( 'MatrixHandler' , 'gotoSetupDialog()' ) ; const matrixUser = superEvent . sender ( ) ; const text = superEvent . event . content ! [ 'body' ] as string || '' ; if ( /^!logout$/i . test ( text ) ) { log . verbose ( 'MatrixHandler' , 'gotoSetupDialog() !logout' ) ; await this . wechatyManager . destroy ( matrixUser . getId ( ) ) ; await this . appserviceManager . sendMessage ( 'logout success.' , superEvent . room ( ) ) ; } else if ( /^!login$/i . test ( text ) ) { log . verbose ( 'MatrixHandler' , 'gotoSetupDialog() !login' ) ; let wechaty = this . wechatyManager . wechaty ( matrixUser . getId ( ) ) ; if ( ! wechaty ) { wechaty = this . wechatyManager . create ( matrixUser . getId ( ) ) ; } await this . appserviceManager . sendMessage ( 'Starting Wechaty Bridge...' , superEvent . room ( ) ) ; await wechaty . start ( ) ; await this . appserviceManager . sendMessage ( 'Wechaty Bridge started.' , superEvent . room ( ) ) ; } else if ( /^!version$/i . test ( text ) ) { log . verbose ( 'MatrixHandler' , 'gotoSetupDialog() !version' ) ; await this . appserviceManager . sendMessage ( `version: ${ VERSION }` , superEvent . room ( ) ) ; } else { const help = [ 'Avaiable commands: !login, !logout' , ] . join ( '' ) ; await this . appserviceManager . sendMessage ( help , superEvent . room ( ) ) ; } } }