1
- package com .chiho .itchat4java ;
1
+ package com .chiho .itchat . shell ;
2
2
3
3
import com .alibaba .fastjson .JSON ;
4
4
import com .alibaba .fastjson .JSONException ;
5
5
import com .alibaba .fastjson .JSONObject ;
6
- import com .chiho .itchat4java .enums .AddFriendStatusEnum ;
7
- import com .chiho .itchat4java .enums .CmdTypeEnum ;
8
- import com .chiho .itchat4java .exceptions .ItChatException ;
9
- import com .chiho .itchat4java .interfaces .Callback ;
10
- import com .chiho .itchat4java .model .ContactDO ;
11
- import com .chiho .itchat4java .model .CreateChatroomDO ;
12
- import com .chiho .itchat4java .model .HeadImgDO ;
13
- import com .chiho .itchat4java .model .MessageDO ;
14
- import com .chiho .itchat4java .model .ModifyChatroomDO ;
15
- import com .chiho .itchat4java .model .MsgDO ;
16
- import com .chiho .itchat4java .model .QrCodeDO ;
17
- import com .chiho .itchat4java .model .RevokeDO ;
18
- import com .chiho .itchat4java .model .ShowMobileLoginDO ;
19
- import com .chiho .itchat4java .model .StatusResponseDO ;
20
- import com .chiho .itchat4java .model .UploadFileDO ;
21
- import com .chiho .itchat4java .model .WebInitDO ;
6
+ import com .chiho .itchat .shell .enums .CmdTypeEnum ;
7
+ import com .chiho .itchat .shell .enums .MessageTypeEnum ;
8
+ import com .chiho .itchat .shell .model .ContactDO ;
9
+ import com .chiho .itchat .shell .model .GroupMessageDO ;
10
+ import com .chiho .itchat .shell .model .MessageDO ;
11
+ import com .chiho .itchat .shell .model .WebInitDO ;
12
+ import com .chiho .itchat .shell .enums .AddFriendStatusEnum ;
13
+ import com .chiho .itchat .shell .exceptions .ItChatException ;
14
+ import com .chiho .itchat .shell .interfaces .Callback ;
15
+ import com .chiho .itchat .shell .model .CreateChatroomDO ;
16
+ import com .chiho .itchat .shell .model .FetchMessageDO ;
17
+ import com .chiho .itchat .shell .model .HeadImgDO ;
18
+ import com .chiho .itchat .shell .model .SendMsgDO ;
19
+ import com .chiho .itchat .shell .model .ModifyChatroomDO ;
20
+ import com .chiho .itchat .shell .model .QrCodeDO ;
21
+ import com .chiho .itchat .shell .model .RevokeDO ;
22
+ import com .chiho .itchat .shell .model .ShowMobileLoginDO ;
23
+ import com .chiho .itchat .shell .model .StatusResponseDO ;
24
+ import com .chiho .itchat .shell .model .UploadFileDO ;
22
25
import com .sun .istack .internal .NotNull ;
23
26
import java .io .File ;
24
27
import java .util .ArrayList ;
@@ -129,6 +132,7 @@ private final Object sendRequest( CmdTypeEnum cmd, Pair<String, String>... args
129
132
});
130
133
client .sendString (message );
131
134
semaphore .acquire ();
135
+
132
136
callbackMapper .remove (cmd .getType ());
133
137
if ( respObj [ 0 ] instanceof StatusResponseDO ) {
134
138
StatusResponseDO statusResponseDO = (StatusResponseDO )respObj [ 0 ];
@@ -143,6 +147,15 @@ private final Object sendRequest( CmdTypeEnum cmd, Pair<String, String>... args
143
147
}
144
148
}
145
149
150
+ /**
151
+ * get itchat is alive
152
+ *
153
+ * @return isAlive
154
+ */
155
+ public Boolean isAlive () throws ItChatException {
156
+ return (Boolean )sendRequest (CmdTypeEnum .IS_ALIVE );
157
+ }
158
+
146
159
/**
147
160
* log in like web wechat does
148
161
*
@@ -168,19 +181,19 @@ public void login( Boolean enableCmdQR, String picDir, Callback<QrCodeDO> qrCall
168
181
"u'" + new File ("src/main/resources/QR.png" ).getAbsolutePath () + "'" ));
169
182
}
170
183
if ( qrCallback != null ) {
171
- callbackMapper .put ("login_qrCallback" , param -> qrCallback .call ((QrCodeDO )param ));
184
+ callbackMapper .put (CmdTypeEnum . LOGIN_QRCALLBACK . getType () , param -> qrCallback .call ((QrCodeDO )param ));
172
185
pairs .add (new Pair <>("qrCallback" , "login_qrCallback" ));
173
186
} else {
174
187
callbackMapper .remove ("login_qrCallback" );
175
188
}
176
189
if ( loginCallback != null ) {
177
- callbackMapper .put ("login_loginCallback" , param -> loginCallback .run ());
190
+ callbackMapper .put (CmdTypeEnum . LOGIN_LOGINCALLBACK . getType () , param -> loginCallback .run ());
178
191
pairs .add (new Pair <>("loginCallback" , "login_loginCallback" ));
179
192
} else {
180
193
callbackMapper .remove ("login_loginCallback" );
181
194
}
182
195
if ( exitCallback != null ) {
183
- callbackMapper .put ("login_exitCallback" , param -> exitCallback .run ());
196
+ callbackMapper .put (CmdTypeEnum . LOGIN_EXITCALLBACK . getType () , param -> exitCallback .run ());
184
197
pairs .add (new Pair <>("exitCallback" , "login_exitCallback" ));
185
198
} else {
186
199
callbackMapper .remove ("login_exitCallback" );
@@ -233,7 +246,7 @@ public String getQR( String uuid, Boolean enableCmdQR, String picDir, Callback<Q
233
246
"u'" + new File ("src/main/resources/QR.png" ).getAbsolutePath () + "'" ));
234
247
}
235
248
if ( qrCallback != null ) {
236
- callbackMapper .put ("login_qrCallback" , param -> qrCallback .call ((QrCodeDO )param ));
249
+ callbackMapper .put (CmdTypeEnum . LOGIN_QRCALLBACK . getType () , param -> qrCallback .call ((QrCodeDO )param ));
237
250
pairs .add (new Pair <>("qrCallback" , "login_qrCallback" ));
238
251
} else {
239
252
callbackMapper .remove ("login_qrCallback" );
@@ -302,7 +315,7 @@ public ShowMobileLoginDO showMobileLogin() throws ItChatException {
302
315
public void startReceiving ( Runnable exitCallback ) throws ItChatException {
303
316
List <Pair > pairs = new ArrayList <>();
304
317
if ( exitCallback != null ) {
305
- callbackMapper .put ("startReceiving_exitCallback" , param -> exitCallback .run ());
318
+ callbackMapper .put (CmdTypeEnum . STARTRECEIVING_EXITCALLBACK . getType () , param -> exitCallback .run ());
306
319
pairs .add (new Pair <>("exitCallback" , "startReceiving_exitCallback" ));
307
320
} else {
308
321
callbackMapper .remove ("startReceiving_exitCallback" );
@@ -325,8 +338,8 @@ public void startReceiving( Runnable exitCallback ) throws ItChatException {
325
338
*
326
339
* @return return
327
340
*/
328
- public MsgDO getMsg () throws ItChatException {
329
- return (MsgDO )sendRequest (CmdTypeEnum .GET_MSG );
341
+ public FetchMessageDO getMsg () throws ItChatException {
342
+ return (FetchMessageDO )sendRequest (CmdTypeEnum .GET_MSG );
330
343
}
331
344
332
345
/**
@@ -689,15 +702,15 @@ public ModifyChatroomDO addMemberIntoChatroom( @NotNull String chatroomUserName,
689
702
*
690
703
* @return return
691
704
*/
692
- public MessageDO sendMsg ( String msg , @ NotNull String toUserName ) throws ItChatException {
705
+ public SendMsgDO sendMsg ( String msg , @ NotNull String toUserName ) throws ItChatException {
693
706
List <Pair > pairs = new ArrayList <>();
694
707
if ( msg != null ) {
695
708
pairs .add (new Pair <>("msg" , "u'" + msg + "'" ));
696
709
}
697
710
if ( toUserName != null ) {
698
711
pairs .add (new Pair <>("toUserName" , "u'" + toUserName + "'" ));
699
712
}
700
- return (MessageDO )sendRequest (CmdTypeEnum .SEND_MSG , pairs .toArray (new Pair [ 0 ]));
713
+ return (SendMsgDO )sendRequest (CmdTypeEnum .SEND_MSG , pairs .toArray (new Pair [ 0 ]));
701
714
}
702
715
703
716
/**
@@ -737,7 +750,7 @@ public UploadFileDO uploadFile( @NotNull String fileDir, Boolean isPicture, Bool
737
750
*
738
751
* @return return
739
752
*/
740
- public MessageDO sendFile ( @ NotNull String fileDir , @ NotNull String toUserName , String mediaId )
753
+ public SendMsgDO sendFile ( @ NotNull String fileDir , @ NotNull String toUserName , String mediaId )
741
754
throws ItChatException {
742
755
List <Pair > pairs = new ArrayList <>();
743
756
if ( fileDir != null ) {
@@ -749,7 +762,7 @@ public MessageDO sendFile( @NotNull String fileDir, @NotNull String toUserName,
749
762
if ( mediaId != null ) {
750
763
pairs .add (new Pair <>("mediaId" , "u'" + mediaId + "'" ));
751
764
}
752
- return (MessageDO )sendRequest (CmdTypeEnum .SEND_FILE , pairs .toArray (new Pair [ 0 ]));
765
+ return (SendMsgDO )sendRequest (CmdTypeEnum .SEND_FILE , pairs .toArray (new Pair [ 0 ]));
753
766
}
754
767
755
768
/**
@@ -761,7 +774,7 @@ public MessageDO sendFile( @NotNull String fileDir, @NotNull String toUserName,
761
774
*
762
775
* @return return
763
776
*/
764
- public MessageDO sendImage ( @ NotNull String fileDir , @ NotNull String toUserName , String mediaId )
777
+ public SendMsgDO sendImage ( @ NotNull String fileDir , @ NotNull String toUserName , String mediaId )
765
778
throws ItChatException {
766
779
List <Pair > pairs = new ArrayList <>();
767
780
if ( fileDir != null ) {
@@ -773,7 +786,7 @@ public MessageDO sendImage( @NotNull String fileDir, @NotNull String toUserName,
773
786
if ( mediaId != null ) {
774
787
pairs .add (new Pair <>("mediaId" , "u'" + mediaId + "'" ));
775
788
}
776
- return (MessageDO )sendRequest (CmdTypeEnum .SEND_IMAGE , pairs .toArray (new Pair [ 0 ]));
789
+ return (SendMsgDO )sendRequest (CmdTypeEnum .SEND_IMAGE , pairs .toArray (new Pair [ 0 ]));
777
790
}
778
791
779
792
/**
@@ -785,7 +798,7 @@ public MessageDO sendImage( @NotNull String fileDir, @NotNull String toUserName,
785
798
*
786
799
* @return return
787
800
*/
788
- public MessageDO sendVideo ( @ NotNull String fileDir , @ NotNull String toUserName , String mediaId )
801
+ public SendMsgDO sendVideo ( @ NotNull String fileDir , @ NotNull String toUserName , String mediaId )
789
802
throws ItChatException {
790
803
List <Pair > pairs = new ArrayList <>();
791
804
if ( fileDir != null ) {
@@ -797,7 +810,7 @@ public MessageDO sendVideo( @NotNull String fileDir, @NotNull String toUserName,
797
810
if ( mediaId != null ) {
798
811
pairs .add (new Pair <>("mediaId" , "u'" + mediaId + "'" ));
799
812
}
800
- return (MessageDO )sendRequest (CmdTypeEnum .SEND_VIDEO , pairs .toArray (new Pair [ 0 ]));
813
+ return (SendMsgDO )sendRequest (CmdTypeEnum .SEND_VIDEO , pairs .toArray (new Pair [ 0 ]));
801
814
}
802
815
803
816
/**
@@ -815,7 +828,7 @@ public MessageDO sendVideo( @NotNull String fileDir, @NotNull String toUserName,
815
828
*
816
829
* @return return
817
830
*/
818
- public MessageDO send ( @ NotNull String msg , @ NotNull String toUserName , String mediaId )
831
+ public SendMsgDO send ( @ NotNull String msg , @ NotNull String toUserName , String mediaId )
819
832
throws ItChatException {
820
833
List <Pair > pairs = new ArrayList <>();
821
834
if ( msg != null ) {
@@ -827,7 +840,7 @@ public MessageDO send( @NotNull String msg, @NotNull String toUserName, String m
827
840
if ( mediaId != null ) {
828
841
pairs .add (new Pair <>("mediaId" , "u'" + mediaId + "'" ));
829
842
}
830
- return (MessageDO )sendRequest (CmdTypeEnum .SEND , pairs .toArray (new Pair [ 0 ]));
843
+ return (SendMsgDO )sendRequest (CmdTypeEnum .SEND , pairs .toArray (new Pair [ 0 ]));
831
844
}
832
845
833
846
/**
@@ -895,13 +908,13 @@ public StatusResponseDO loadLoginStatus( String fileDir, Runnable loginCallback,
895
908
"u'" + new File ("src/main/resources/login.sav" ).getAbsolutePath () + "'" ));
896
909
}
897
910
if ( loginCallback != null ) {
898
- callbackMapper .put ("loginStatus_loginCallback" , param -> loginCallback .run ());
911
+ callbackMapper .put (CmdTypeEnum . LOGINSTATUS_LOGINCALLBACK . getType () , param -> loginCallback .run ());
899
912
pairs .add (new Pair <>("loginCallback" , "loginStatus_loginCallback" ));
900
913
} else {
901
914
callbackMapper .remove ("loginStatus_loginCallback" );
902
915
}
903
916
if ( exitCallback != null ) {
904
- callbackMapper .put ("loginStatus_exitCallback" , param -> exitCallback .run ());
917
+ callbackMapper .put (CmdTypeEnum . LOGINSTATUS_EXITCALLBACK . getType () , param -> exitCallback .run ());
905
918
pairs .add (new Pair <>("exitCallback" , "loginStatus_exitCallback" ));
906
919
} else {
907
920
callbackMapper .remove ("loginStatus_exitCallback" );
@@ -931,6 +944,9 @@ public void autoLogin( Boolean hotReload, String statusStorageDir, Boolean enabl
931
944
}
932
945
if ( statusStorageDir != null ) {
933
946
pairs .add (new Pair <>("statusStorageDir" , "u'" + statusStorageDir + "'" ));
947
+ } else {
948
+ pairs .add (new Pair <>("statusStorageDir" ,
949
+ "u'" + new File ("src/main/resources/login.sav" ).getAbsolutePath () + "'" ));
934
950
}
935
951
if ( enableCmdQR != null ) {
936
952
pairs .add (new Pair <>("enableCmdQR" , enableCmdQR ? "True" : "False" ));
@@ -942,19 +958,19 @@ public void autoLogin( Boolean hotReload, String statusStorageDir, Boolean enabl
942
958
"u'" + new File ("src/main/resources/QR.png" ).getAbsolutePath () + "'" ));
943
959
}
944
960
if ( qrCallback != null ) {
945
- callbackMapper .put ("login_qrCallback" , param -> qrCallback .call ((QrCodeDO )param ));
961
+ callbackMapper .put (CmdTypeEnum . LOGIN_QRCALLBACK . getType () , param -> qrCallback .call ((QrCodeDO )param ));
946
962
pairs .add (new Pair <>("qrCallback" , "login_qrCallback" ));
947
963
} else {
948
964
callbackMapper .remove ("login_qrCallback" );
949
965
}
950
966
if ( loginCallback != null ) {
951
- callbackMapper .put ("login_loginCallback" , param -> loginCallback .run ());
967
+ callbackMapper .put (CmdTypeEnum . LOGIN_LOGINCALLBACK . getType () , param -> loginCallback .run ());
952
968
pairs .add (new Pair <>("loginCallback" , "login_loginCallback" ));
953
969
} else {
954
970
callbackMapper .remove ("login_loginCallback" );
955
971
}
956
972
if ( exitCallback != null ) {
957
- callbackMapper .put ("login_exitCallback" , param -> exitCallback .run ());
973
+ callbackMapper .put (CmdTypeEnum . LOGIN_EXITCALLBACK . getType () , param -> exitCallback .run ());
958
974
pairs .add (new Pair <>("exitCallback" , "login_exitCallback" ));
959
975
} else {
960
976
callbackMapper .remove ("login_exitCallback" );
@@ -1033,4 +1049,16 @@ public List<ContactDO> searchMps( String name, String userName ) throws ItChatEx
1033
1049
}
1034
1050
return (List <ContactDO >)sendRequest (CmdTypeEnum .SEARCH_MPS , pairs .toArray (new Pair [ 0 ]));
1035
1051
}
1052
+
1053
+ public void addFriendMessageCallback (MessageTypeEnum messageType , Callback <MessageDO > callback ) {
1054
+ callbackMapper .put ("friend_" + messageType .getType () + "Callback" , param -> callback .call ((MessageDO )param ));
1055
+ }
1056
+
1057
+ public void addMpMessageCallback (MessageTypeEnum messageType , Callback <MessageDO > callback ) {
1058
+ callbackMapper .put ("mp_" + messageType .getType () + "Callback" , param -> callback .call ((MessageDO )param ));
1059
+ }
1060
+
1061
+ public void addGroupMessageCallback (MessageTypeEnum messageType , Callback <GroupMessageDO > callback ) {
1062
+ callbackMapper .put ("group_" + messageType .getType () + "Callback" , param -> callback .call ((GroupMessageDO )param ));
1063
+ }
1036
1064
}
0 commit comments