11import { checkConfigIsOk , getConfig } from './config' ;
22import { log , Message , ScanStatus , WechatyBuilder } from 'wechaty' ;
33import { getMessagePayload , LOGPRE } from './helper' ;
4- import { ipcMain } from 'electron' ;
5- import { summarize } from './summarize' ;
6- import { getAllDirs } from './helpers/getAllDirs' ;
74import { PuppetPadlocal } from 'wechaty-puppet-padlocal-plus' ;
85import { WechatyInterface } from 'wechaty/dist/esm/src/wechaty/wechaty-impl' ;
9- import { FileBox } from 'file-box' ;
6+ import { FileBox } from 'file-box' ;
107import { RoomInterface } from 'wechaty/dist/esm/src/user-modules/room' ;
118
12- let bot :WechatyInterface
13- export async function startBot ( mainWindow : Electron . BrowserWindow ) {
9+ let bot : WechatyInterface ;
10+
11+ export let botStatus = '已停止' ;
1412
13+ export async function startBot ( mainWindow : Electron . BrowserWindow ) {
1514 if ( ! checkConfigIsOk ( ) ) {
1615 console . log ( 'miss config' ) ;
1716 mainWindow . webContents . send ( 'toast' , `miss config` ) ;
1817 mainWindow . webContents . send ( 'show-config' , getConfig ( ) ) ;
1918 return ;
2019 }
2120
22-
23- if ( bot ) {
21+ if ( bot ) {
2422 // 清理,重新启动 bot
2523 await bot . stop ( ) ;
2624 bot = null ;
@@ -29,35 +27,37 @@ export async function startBot(mainWindow: Electron.BrowserWindow) {
2927
3028 const puppet = new PuppetPadlocal ( {
3129 token : config . PADLOCAL_API_KEY ,
32-
3330 } ) ;
3431 bot = WechatyBuilder . build ( {
3532 name : 'WXGroupSummary' ,
3633 puppet,
3734 } ) ;
38- bot
39- . on ( 'message' , async ( message ) => {
40- log . info ( LOGPRE , `on message: ${ message . toString ( ) } ` ) ;
35+ bot . on ( 'message' , async ( message ) => {
36+ log . info ( LOGPRE , `on message: ${ message . toString ( ) } ` ) ;
4137
42- await getMessagePayload ( message ) ;
38+ await getMessagePayload ( message ) ;
4339
44- // await dingDongBot(message);
45- } ) ;
40+ // await dingDongBot(message);
41+ botStatus = '运行中' ;
42+ } ) ;
4643 // 向 mainWindow 发送事件
4744 bot
4845 . on ( 'error' , ( error ) => {
4946 log . error ( LOGPRE , `on error: ${ error } ` ) ;
5047 mainWindow . webContents . send ( 'toast' , `错误: ${ error } ` ) ;
48+ botStatus = '错误' ;
5149 } )
5250 . on ( 'login' , ( user ) => {
5351 log . info ( LOGPRE , `${ user } login` ) ;
5452 mainWindow . webContents . send ( 'toast' , `${ user } login success` ) ;
5553 mainWindow . webContents . send ( 'login' ) ;
54+ botStatus = '登录成功' ;
5655 } )
5756 . on ( 'logout' , ( user , reason ) => {
5857 log . info ( LOGPRE , `${ user } logout, reason: ${ reason } ` ) ;
5958 mainWindow . webContents . send ( 'toast' , `${ user } logout, reason: ${ reason } ` ) ;
6059 mainWindow . webContents . send ( 'logout' ) ;
60+ botStatus = '已退出' ;
6161 } )
6262 . on ( 'scan' , ( qrcode , status ) => {
6363 if ( status === ScanStatus . Waiting && qrcode ) {
@@ -72,9 +72,11 @@ export async function startBot(mainWindow: Electron.BrowserWindow) {
7272 log . info ( LOGPRE , `onScan: ${ ScanStatus [ status ] } (${ status } )` ) ;
7373 mainWindow . webContents . send ( 'toast' , `onScan: ${ ScanStatus [ status ] } (${ status } )` ) ;
7474 }
75+ botStatus = '已扫描' ;
7576 } )
7677 . on ( 'stop' , ( ) => {
7778 mainWindow . webContents . send ( 'toast' , `stop` ) ;
79+ botStatus = '已停止' ;
7880 } ) ;
7981
8082 await bot . start ( ) ;
@@ -84,44 +86,45 @@ export async function startBot(mainWindow: Electron.BrowserWindow) {
8486
8587 return bot ;
8688}
89+
8790const roomCache = new Map < string , RoomInterface > ( ) ;
8891const getRoomByName = async ( name : string ) => {
89- if ( roomCache . has ( name ) ) {
90- return roomCache . get ( name )
92+ if ( roomCache . has ( name ) ) {
93+ return roomCache . get ( name ) ;
9194 }
9295 const roomList = await bot . Room . findAll ( ) ;
9396 for ( const room of roomList ) {
9497 if ( room . payload . topic === name ) {
9598 console . log ( '找到了名为 [' , name , '] 的群聊,其 ID 为:' , room . id ) ;
96- roomCache . set ( name , room )
99+ roomCache . set ( name , room ) ;
97100 return room ;
98101 }
99102 }
100- }
101- const sendMessage = async ( toRoomName :string , payload : any ) : Promise < Message > => {
103+ } ;
104+ const sendMessage = async ( toRoomName : string , payload : any ) : Promise < Message > => {
102105 const room = await getRoomByName ( toRoomName ) ;
103- const message = ( await room . say ( payload ) ) as Message ;
104- return message ;
105-
106+ const message = ( await room . say ( payload ) ) as Message ;
107+ return message ;
106108} ;
107109
108- export async function sendText ( toRoomName :string , text :string ) {
109- console . log ( 'sendText' , toRoomName , text )
110+ export async function sendText ( toRoomName : string , text : string ) {
111+ console . log ( 'sendText' , toRoomName , text ) ;
110112 const message = await sendMessage ( toRoomName , text ) ;
111113 return message ;
112114}
113- export async function sendImage ( toRoomName :string , imageFilePath :string ) {
114- console . log ( 'sendImage' , toRoomName , imageFilePath )
115+
116+ export async function sendImage ( toRoomName : string , imageFilePath : string ) {
117+ console . log ( 'sendImage' , toRoomName , imageFilePath ) ;
115118 // 图片大小建议不要超过 2 M
116119 const fileBox = FileBox . fromFile ( imageFilePath ) ;
117120
118121 const message = await sendMessage ( toRoomName , fileBox ) ;
119122 return message ;
120123}
121124
122- export async function sendAudio ( toRoomName :string , fileFilePath :string ) {
123- console . log ( 'sendAudio' , toRoomName , fileFilePath )
125+ export async function sendAudio ( toRoomName : string , fileFilePath : string ) {
126+ console . log ( 'sendAudio' , toRoomName , fileFilePath ) ;
124127 const fileBox = FileBox . fromFile ( fileFilePath ) ;
125128 const message = await sendMessage ( toRoomName , fileBox ) ;
126129 return message ;
127- }
130+ }
0 commit comments