Skip to content

Commit addfeb8

Browse files
committed
v0.9.2 - Fix fromId in USER accounts
1 parent 3e747e8 commit addfeb8

File tree

4 files changed

+145
-71
lines changed

4 files changed

+145
-71
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package santaspeen.vk.api.features;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.METHOD)
10+
public @interface onNewVkEvent {}

src/main/java/santaspeen/vk/api/parsers/parseMessage.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.json.JSONObject;
55
import santaspeen.vk.api.utils;
66

7+
import java.util.Arrays;
8+
79
public class parseMessage{
810

911
public long date = 0;
@@ -20,8 +22,27 @@ public class parseMessage{
2022
public long peerId = 0;
2123
public long conversationMessageId = 0;
2224

25+
public int[] outboxMessage = {
26+
2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 26, 30, 31, 34, 35, 38, 42, 50, 51, 54, 58, 62, 63, 66, 67, 70, 74,
27+
82, 98, 99, 102, 106, 114, 115, 118, 122, 126, 127, 130, 131, 134, 138, 146, 162, 194, 195, 198, 202, 210,
28+
226, 227, 230, 234, 242, 243, 246, 250, 254, 255, 258, 259, 262, 266, 274, 290, 322, 386, 387, 390, 394,
29+
402, 418, 450, 451, 454, 458, 466, 482, 483, 486, 490, 498, 499, 502, 506, 510, 511, 514, 515, 518, 522,
30+
530, 546, 578, 642, 770, 771, 774, 778, 786, 802, 834, 898, 899, 902, 906, 914, 930, 962, 963, 966, 970,
31+
978, 994, 995, 998, 1002, 1010, 1011, 1014, 1018, 1022, 1023, 8227, 65538, 65539, 65542, 65546, 65554,
32+
65570, 65602, 65666, 65794, 66050, 66051, 66054, 66058, 66066, 66082, 66114, 66178, 66306, 66307, 66310,
33+
66314, 66322, 66338, 66370, 66434, 66435, 66438, 66442, 66450, 66466, 66498, 66499, 66502, 66506, 66514,
34+
66530, 66531, 66534, 66538, 66546, 66547, 66550, 66554, 66558, 66559, 131074, 131075, 131078, 131082,
35+
131090, 131106, 131138, 131202, 131330, 131586, 196610, 196611, 196614, 196618, 196626, 196642, 196674,
36+
196738, 196866, 197122, 197123, 197126, 197130, 197138, 197154, 197186, 197250, 197378, 197382, 197386,
37+
197394, 197410, 197442, 197506, 197507, 197510, 197514, 197522, 197538, 197570, 197574, 197578, 197586,
38+
197602, 197603, 197606, 197610, 197618, 197619, 197622, 197626, 197630, 197631, 262146, 262147, 262150,
39+
262154, 262162, 262178, 262210, 262274, 262402, 262658, 327682, 393218, 393219, 393222, 393226, 393234,
40+
393250, 393282, 393346, 393474, 393730, 458754, 458755, 458758, 458762, 458770, 458786, 458818, 458882,
41+
459010, 459266, 459267, 459270, 459274, 459282, 459298, 459330, 459394, 459522, 459523, 459526, 459530,
42+
459538, 459554, 459586, 459650, 459651, 459654, 459658, 459666, 459682, 459714, 459715, 459718, 459722,
43+
459730, 459746, 459747, 459750, 459754, 459762, 459763, 459766, 459770, 459774, 459775};
44+
2345

24-
// TODO: Сделать isHidden в userMessage
2546
/**
2647
* Init of message parser.
2748
* Tested on 5.120 LongPoll version.
@@ -49,19 +70,25 @@ public parseMessage(JSONObject groupMessage, JSONArray userMessage, long userId)
4970

5071
} else if (userMessage != null){
5172

52-
date = utils.getLong(userMessage.get(4));
5373

54-
if (((JSONObject) userMessage.get(6)).opt("title") != null)
55-
fromId = userId;
56-
else
57-
fromId = Long.parseLong((userMessage.getJSONObject(6)).getString("from"));
74+
System.out.println(Arrays.binarySearch(outboxMessage, userMessage.getInt(2)));
75+
76+
date = utils.getLong(userMessage.get(4));
5877

5978
userAttachments = userMessage.getJSONObject(7);
6079
id = userMessage.getLong(1);
6180
text = userMessage.getString(5);
6281
randomId = userMessage.getLong(8);
6382
peerId = userMessage.getLong(3);
6483

84+
if (((JSONObject) userMessage.get(6)).opt("title") != null){
85+
if (Arrays.binarySearch(outboxMessage, userMessage.getInt(2)) > 0)
86+
fromId = peerId;
87+
else
88+
fromId = userId;
89+
} else
90+
fromId = Long.parseLong((userMessage.getJSONObject(6)).getString("from"));
91+
6592
}
6693
}
6794
}

src/main/java/santaspeen/vk/api/vkApi.java

Lines changed: 82 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.io.BufferedReader;
1414
import java.io.IOException;
1515
import java.io.InputStreamReader;
16+
import java.io.UnsupportedEncodingException;
1617
import java.lang.reflect.InvocationTargetException;
1718
import java.lang.reflect.Method;
1819
import java.net.HttpURLConnection;
@@ -79,10 +80,18 @@ private void notToBind(Class<?>[] params, Method method, Object obj, String expe
7980
". Expected " + expected+".");
8081
}
8182

82-
public Thread bindCommands(Object... objects) throws VkApiError {
83+
/**
84+
* Bind commands with Interface
85+
*
86+
* @since v0.9
87+
*
88+
* @param objects Classes with Interface
89+
*
90+
*/
91+
public void bindCommands(Object... objects) throws VkApiError {
8392

8493
if (objects.length == 0)
85-
throw new VkApiError("No classes into commands();");
94+
throw new VkApiError("No classes into bindCommands();");
8695

8796
for (Object obj: objects){
8897

@@ -142,41 +151,56 @@ public Thread bindCommands(Object... objects) throws VkApiError {
142151
}
143152
}
144153

145-
System.out.println(commandsText);
146-
System.out.println(commandsOnlyBy);
147-
System.out.println(commandsStartsWith);
148-
System.out.println(commandsReturnLongPoll);
149-
150-
System.out.println(commandsMethod);
151-
System.out.println(commandsObj);
154+
}
152155

153-
Thread thread = new Thread(() -> {
154-
try {
155-
getLongPollServer();
156+
public Thread startWithThread(){
157+
Thread thread = new Thread(this::start);
158+
thread.setName("Bot thread");
159+
thread.start();
160+
return thread;
161+
}
162+
163+
public void start(){
164+
try {
165+
getLongPollServer();
166+
167+
long lastTs = 0;
168+
while (true){
156169

157-
long lastTs = 0;
158-
while (true){
170+
JSONObject longPoll = listenLongPoll(25);
171+
parseLongPoll parse = parse(longPoll);
159172

160-
JSONObject longPoll = listenLongPoll(25);
161-
parseLongPoll parse = parse(longPoll);
173+
if (lastTs != parse.ts) {
174+
lastTs = parse.ts;
162175

163-
if (lastTs != parse.ts) {
164-
long start = System.currentTimeMillis();
176+
if (parse.failed > 0)
177+
getLongPollServer();
165178

166-
lastTs = parse.ts;
179+
if (parse.isMessage()) {
167180

168-
if (parse.failed > 0)
169-
getLongPollServer();
181+
parseMessage message = parse.message();
170182

171-
if (parse.isMessage()) {
183+
boolean inOnlyBy = false;
184+
String text = message.text.toLowerCase();
185+
long fromId = message.fromId;
172186

173-
parseMessage message = parse.message();
187+
int i = 0;
188+
for (String comm: commandsText) {
189+
190+
int[] onlyByMassive = commandsOnlyBy.get(i);
191+
192+
if (onlyByMassive.length == 0)
193+
inOnlyBy = true;
194+
else
195+
for (int onlyBy: onlyByMassive){
196+
if (onlyBy == fromId) {
197+
inOnlyBy = true;
198+
break;
199+
}
200+
}
174201

175-
String text = message.text.toLowerCase();
176-
long fromId = message.fromId;
202+
if (inOnlyBy){
177203

178-
int i = 0;
179-
for (String comm: commandsText) {
180204
comm = comm.toLowerCase();
181205
JSONObject longPollToInvoke = null;
182206

@@ -199,21 +223,22 @@ public Thread bindCommands(Object... objects) throws VkApiError {
199223
commandsMethod.get(i).invoke(commandsObj.get(i), message, this);
200224
continue;
201225

202-
} i++;
203-
}
226+
}
227+
} i++;
204228
}
205229
}
206230
}
207-
} catch (VkApiError | IllegalAccessException | InvocationTargetException e) {
208-
e.printStackTrace();
209231
}
210-
});
211-
thread.setName("Bot thread");
212-
return thread;
232+
} catch (IllegalArgumentException longPollError){
233+
System.err.println("Illegal Argument - JSONObject returnLongPoll\n");
234+
System.exit(1);
235+
} catch (VkApiError | InvocationTargetException | IllegalAccessException e) {
236+
e.printStackTrace();
237+
}
213238
}
214239

215240
/**
216-
* Set account type.
241+
* Set the account type.
217242
* Default:
218243
* VkAPIAccountTypes.GROUP.
219244
*
@@ -245,7 +270,7 @@ public JSONObject parseJson(String in) {
245270
}
246271

247272
/**
248-
* Parse JSON
273+
* Parse JSON array
249274
*
250275
* @since v0.8
251276
*
@@ -258,7 +283,6 @@ public JSONArray parseJsonArray(String in) {
258283
catch (JSONException e){return new JSONArray();}
259284
}
260285

261-
262286
private String errorOrResponse(String data, String method) throws VkApiError {
263287
JSONObject rqAns = parseJson(data);
264288

@@ -277,6 +301,22 @@ private String errorOrResponse(String data, String method) throws VkApiError {
277301
return rqAns.get("response").toString();
278302
}
279303

304+
/**
305+
* Encode params for URL
306+
*
307+
* @since 0.9.2
308+
*
309+
* @param in What to decode
310+
*
311+
* @return Decoded value
312+
*/
313+
public String encodeParam(String in) {
314+
try {
315+
return encode(in, "UTF-8");
316+
} catch (UnsupportedEncodingException e){e.printStackTrace();
317+
return null;
318+
}
319+
}
280320

281321
/**
282322
* Main API wrapper.
@@ -319,7 +359,7 @@ public String method(String method, String... params) throws VkApiError {
319359
* @return Is send message
320360
*/
321361
public boolean messagesSend(String peerId, String message) throws VkApiError {
322-
JSONObject send = parseJson(method("messages.send", "peer_id="+peerId+"&random_id=0&message="+encode(message)));
362+
JSONObject send = parseJson(method("messages.send", "peer_id="+peerId+"&random_id=0&message="+encodeParam(message)));
323363
return send != null;
324364
}
325365

@@ -340,7 +380,7 @@ public boolean messagesSend(String peerId, String message) throws VkApiError {
340380
* @return Is send message
341381
*/
342382
public boolean messagesSend(int peerId, String message) throws VkApiError {
343-
JSONObject send = parseJson(method("messages.send", "peer_id="+peerId+"&random_id=0&message="+ encode(message)));
383+
JSONObject send = parseJson(method("messages.send", "peer_id="+peerId+"&random_id=0&message="+ encodeParam(message)));
344384
return send != null;
345385
}
346386

@@ -361,7 +401,7 @@ public boolean messagesSend(int peerId, String message) throws VkApiError {
361401
* @return Is send message
362402
*/
363403
public boolean messagesSend(long peerId, String message) throws VkApiError {
364-
String send = method("messages.send", "peer_id="+peerId+"&random_id=0&message="+ encode(message));
404+
String send = method("messages.send", "peer_id="+peerId+"&random_id=0&message="+ encodeParam(message));
365405
return send != null;
366406
}
367407

@@ -483,9 +523,8 @@ public static String get(String urlToRead) throws VkApiError {
483523
rd.close();
484524
return result.toString();
485525
}
486-
catch (SocketTimeoutException e){throw new VkApiError("Connection failed..");}
487-
catch (UnknownHostException e){throw new VkApiError("Connection failed..");}
488-
catch (IOException e){throw new VkApiError(String.valueOf(e));}
526+
catch (SocketTimeoutException | UnknownHostException e){throw new VkApiError("Connection failed..");}
527+
catch (IOException e) { throw new VkApiError(String.valueOf(e));}
489528

490529
}
491530
}
Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
1+
import com.sun.org.apache.regexp.internal.RE;
12
import org.json.JSONObject;
23
import santaspeen.vk.api.features.VkAPIAccountTypes;
34
import santaspeen.vk.api.exceptions.VkApiError;
5+
import santaspeen.vk.api.features.onNewVkEvent;
46
import santaspeen.vk.api.features.onVkMessage;
57
import santaspeen.vk.api.parsers.parseMessage;
68
import santaspeen.vk.api.VkApi;
79

10+
import java.util.Arrays;
11+
812
public class LongPollAPIFromInterface {
913

10-
public static final VkApi api = new VkApi("1a8dfd16d88cca3661e1cb08a28ddf1c33636baa428eed4ef902fa6460f3a35b40c02fcd34bb3d09e2061");
14+
public static final VkApi api = new VkApi("TOKEN");
1115

1216
public static void main(String[] args) throws VkApiError {
1317

14-
api.setAccountType(VkAPIAccountTypes.USER); // (!) Если токен группы, не обязательно.
18+
api.setAccountType(VkAPIAccountTypes.GROUP); // (!) Если токен группы, не обязательно.
1519

16-
api.bindCommands(new LongPollAPIFromInterface()).start();
20+
api.bindCommands(new LongPollAPIFromInterface());
1721

18-
}
22+
Thread thread = api.startWithThread();
23+
System.out.println(thread.getName());
1924

20-
@onVkMessage(text = "hi", startsWith = true, returnLongPoll = true)
21-
public void helloWorld(parseMessage msg, VkApi api, JSONObject longPoll) throws VkApiError {
22-
23-
String out = "Текст: "+msg.text+"\nОт: @id"+msg.fromId+"\nВ: "+msg.peerId;
25+
}
2426

25-
System.out.println(out);
26-
System.out.println(longPoll);
27-
api.messagesSend(msg.peerId, "!");
27+
@onNewVkEvent() // 0.9.2 - Not work yet
28+
public void newEvent(JSONObject event){
29+
System.out.println(event);
2830
}
2931

30-
@onVkMessage(text = "ks", startsWith = true)
31-
public void helloWorld2(parseMessage msg, VkApi api) throws VkApiError {
32+
@onVkMessage(text = "hi", startsWith = true, returnLongPoll = true)
33+
public void newMessage(parseMessage msg, VkApi api, JSONObject longPoll) throws VkApiError {
3234

33-
String out = "Текст: "+msg.text+"\nОт: @id"+msg.fromId+"\nВ: "+msg.peerId;
35+
System.out.println("Текст: "+msg.text+"\nОт: @id"+msg.fromId+"\nВ: "+msg.peerId);
36+
System.out.println(longPoll);
3437

35-
System.out.println(out);
36-
api.messagesSend(msg.peerId, "!!");
38+
if (api.userId != msg.fromId)
39+
api.messagesSend(msg.peerId, "hi");
3740
}
41+
}
3842

39-
// In chat In private
40-
// msgid flags peer_id timestamp text ХЗ, честно вложения random_id msgid flags peer_id timestamp text ХЗ, честно вложения random_id
41-
// Non-self [4,63214,532497,2000000035,1597576801,"hi",{"from":"370926160"},{},0] [4,63219,49, 370926160, 1597577183,"hi",{"title":" ... "},{},0]
42-
// Self [4,63215,8227, 2000000035,1597576801,"hi",{"from":"583018016"},{},0] [4,63220,35, 370926160, 1597577184,"hi",{"title":" ... "},{},0]
43-
44-
}

0 commit comments

Comments
 (0)