diff --git a/src/main/java/santaspeen/vk/api/features/onNewVkEvent.java b/src/main/java/santaspeen/vk/api/features/onNewVkEvent.java new file mode 100644 index 0000000..7f66b69 --- /dev/null +++ b/src/main/java/santaspeen/vk/api/features/onNewVkEvent.java @@ -0,0 +1,10 @@ +package santaspeen.vk.api.features; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface onNewVkEvent {} diff --git a/src/main/java/santaspeen/vk/api/parsers/parseMessage.java b/src/main/java/santaspeen/vk/api/parsers/parseMessage.java index 5431c2e..af05882 100644 --- a/src/main/java/santaspeen/vk/api/parsers/parseMessage.java +++ b/src/main/java/santaspeen/vk/api/parsers/parseMessage.java @@ -4,6 +4,8 @@ import org.json.JSONObject; import santaspeen.vk.api.utils; +import java.util.Arrays; + public class parseMessage{ public long date = 0; @@ -20,8 +22,27 @@ public class parseMessage{ public long peerId = 0; public long conversationMessageId = 0; + public int[] outboxMessage = { + 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, + 82, 98, 99, 102, 106, 114, 115, 118, 122, 126, 127, 130, 131, 134, 138, 146, 162, 194, 195, 198, 202, 210, + 226, 227, 230, 234, 242, 243, 246, 250, 254, 255, 258, 259, 262, 266, 274, 290, 322, 386, 387, 390, 394, + 402, 418, 450, 451, 454, 458, 466, 482, 483, 486, 490, 498, 499, 502, 506, 510, 511, 514, 515, 518, 522, + 530, 546, 578, 642, 770, 771, 774, 778, 786, 802, 834, 898, 899, 902, 906, 914, 930, 962, 963, 966, 970, + 978, 994, 995, 998, 1002, 1010, 1011, 1014, 1018, 1022, 1023, 8227, 65538, 65539, 65542, 65546, 65554, + 65570, 65602, 65666, 65794, 66050, 66051, 66054, 66058, 66066, 66082, 66114, 66178, 66306, 66307, 66310, + 66314, 66322, 66338, 66370, 66434, 66435, 66438, 66442, 66450, 66466, 66498, 66499, 66502, 66506, 66514, + 66530, 66531, 66534, 66538, 66546, 66547, 66550, 66554, 66558, 66559, 131074, 131075, 131078, 131082, + 131090, 131106, 131138, 131202, 131330, 131586, 196610, 196611, 196614, 196618, 196626, 196642, 196674, + 196738, 196866, 197122, 197123, 197126, 197130, 197138, 197154, 197186, 197250, 197378, 197382, 197386, + 197394, 197410, 197442, 197506, 197507, 197510, 197514, 197522, 197538, 197570, 197574, 197578, 197586, + 197602, 197603, 197606, 197610, 197618, 197619, 197622, 197626, 197630, 197631, 262146, 262147, 262150, + 262154, 262162, 262178, 262210, 262274, 262402, 262658, 327682, 393218, 393219, 393222, 393226, 393234, + 393250, 393282, 393346, 393474, 393730, 458754, 458755, 458758, 458762, 458770, 458786, 458818, 458882, + 459010, 459266, 459267, 459270, 459274, 459282, 459298, 459330, 459394, 459522, 459523, 459526, 459530, + 459538, 459554, 459586, 459650, 459651, 459654, 459658, 459666, 459682, 459714, 459715, 459718, 459722, + 459730, 459746, 459747, 459750, 459754, 459762, 459763, 459766, 459770, 459774, 459775}; + - // TODO: Сделать isHidden в userMessage /** * Init of message parser. * Tested on 5.120 LongPoll version. @@ -49,12 +70,10 @@ public parseMessage(JSONObject groupMessage, JSONArray userMessage, long userId) } else if (userMessage != null){ - date = utils.getLong(userMessage.get(4)); - if (((JSONObject) userMessage.get(6)).opt("title") != null) - fromId = userId; - else - fromId = Long.parseLong((userMessage.getJSONObject(6)).getString("from")); + System.out.println(Arrays.binarySearch(outboxMessage, userMessage.getInt(2))); + + date = utils.getLong(userMessage.get(4)); userAttachments = userMessage.getJSONObject(7); id = userMessage.getLong(1); @@ -62,6 +81,14 @@ public parseMessage(JSONObject groupMessage, JSONArray userMessage, long userId) randomId = userMessage.getLong(8); peerId = userMessage.getLong(3); + if (((JSONObject) userMessage.get(6)).opt("title") != null){ + if (Arrays.binarySearch(outboxMessage, userMessage.getInt(2)) > 0) + fromId = peerId; + else + fromId = userId; + } else + fromId = Long.parseLong((userMessage.getJSONObject(6)).getString("from")); + } } } diff --git a/src/main/java/santaspeen/vk/api/vkApi.java b/src/main/java/santaspeen/vk/api/vkApi.java index 26b483d..1116ae8 100644 --- a/src/main/java/santaspeen/vk/api/vkApi.java +++ b/src/main/java/santaspeen/vk/api/vkApi.java @@ -13,6 +13,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.HttpURLConnection; @@ -79,10 +80,18 @@ private void notToBind(Class[] params, Method method, Object obj, String expe ". Expected " + expected+"."); } - public Thread bindCommands(Object... objects) throws VkApiError { + /** + * Bind commands with Interface + * + * @since v0.9 + * + * @param objects Classes with Interface + * + */ + public void bindCommands(Object... objects) throws VkApiError { if (objects.length == 0) - throw new VkApiError("No classes into commands();"); + throw new VkApiError("No classes into bindCommands();"); for (Object obj: objects){ @@ -142,41 +151,56 @@ public Thread bindCommands(Object... objects) throws VkApiError { } } - System.out.println(commandsText); - System.out.println(commandsOnlyBy); - System.out.println(commandsStartsWith); - System.out.println(commandsReturnLongPoll); - - System.out.println(commandsMethod); - System.out.println(commandsObj); + } - Thread thread = new Thread(() -> { - try { - getLongPollServer(); + public Thread startWithThread(){ + Thread thread = new Thread(this::start); + thread.setName("Bot thread"); + thread.start(); + return thread; + } + + public void start(){ + try { + getLongPollServer(); + + long lastTs = 0; + while (true){ - long lastTs = 0; - while (true){ + JSONObject longPoll = listenLongPoll(25); + parseLongPoll parse = parse(longPoll); - JSONObject longPoll = listenLongPoll(25); - parseLongPoll parse = parse(longPoll); + if (lastTs != parse.ts) { + lastTs = parse.ts; - if (lastTs != parse.ts) { - long start = System.currentTimeMillis(); + if (parse.failed > 0) + getLongPollServer(); - lastTs = parse.ts; + if (parse.isMessage()) { - if (parse.failed > 0) - getLongPollServer(); + parseMessage message = parse.message(); - if (parse.isMessage()) { + boolean inOnlyBy = false; + String text = message.text.toLowerCase(); + long fromId = message.fromId; - parseMessage message = parse.message(); + int i = 0; + for (String comm: commandsText) { + + int[] onlyByMassive = commandsOnlyBy.get(i); + + if (onlyByMassive.length == 0) + inOnlyBy = true; + else + for (int onlyBy: onlyByMassive){ + if (onlyBy == fromId) { + inOnlyBy = true; + break; + } + } - String text = message.text.toLowerCase(); - long fromId = message.fromId; + if (inOnlyBy){ - int i = 0; - for (String comm: commandsText) { comm = comm.toLowerCase(); JSONObject longPollToInvoke = null; @@ -199,21 +223,22 @@ public Thread bindCommands(Object... objects) throws VkApiError { commandsMethod.get(i).invoke(commandsObj.get(i), message, this); continue; - } i++; - } + } + } i++; } } } - } catch (VkApiError | IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); } - }); - thread.setName("Bot thread"); - return thread; + } catch (IllegalArgumentException longPollError){ + System.err.println("Illegal Argument - JSONObject returnLongPoll\n"); + System.exit(1); + } catch (VkApiError | InvocationTargetException | IllegalAccessException e) { + e.printStackTrace(); + } } /** - * Set account type. + * Set the account type. * Default: * VkAPIAccountTypes.GROUP. * @@ -245,7 +270,7 @@ public JSONObject parseJson(String in) { } /** - * Parse JSON + * Parse JSON array * * @since v0.8 * @@ -258,7 +283,6 @@ public JSONArray parseJsonArray(String in) { catch (JSONException e){return new JSONArray();} } - private String errorOrResponse(String data, String method) throws VkApiError { JSONObject rqAns = parseJson(data); @@ -277,6 +301,22 @@ private String errorOrResponse(String data, String method) throws VkApiError { return rqAns.get("response").toString(); } + /** + * Encode params for URL + * + * @since 0.9.2 + * + * @param in What to decode + * + * @return Decoded value + */ + public String encodeParam(String in) { + try { + return encode(in, "UTF-8"); + } catch (UnsupportedEncodingException e){e.printStackTrace(); + return null; + } + } /** * Main API wrapper. @@ -319,7 +359,7 @@ public String method(String method, String... params) throws VkApiError { * @return Is send message */ public boolean messagesSend(String peerId, String message) throws VkApiError { - JSONObject send = parseJson(method("messages.send", "peer_id="+peerId+"&random_id=0&message="+encode(message))); + JSONObject send = parseJson(method("messages.send", "peer_id="+peerId+"&random_id=0&message="+encodeParam(message))); return send != null; } @@ -340,7 +380,7 @@ public boolean messagesSend(String peerId, String message) throws VkApiError { * @return Is send message */ public boolean messagesSend(int peerId, String message) throws VkApiError { - JSONObject send = parseJson(method("messages.send", "peer_id="+peerId+"&random_id=0&message="+ encode(message))); + JSONObject send = parseJson(method("messages.send", "peer_id="+peerId+"&random_id=0&message="+ encodeParam(message))); return send != null; } @@ -361,7 +401,7 @@ public boolean messagesSend(int peerId, String message) throws VkApiError { * @return Is send message */ public boolean messagesSend(long peerId, String message) throws VkApiError { - String send = method("messages.send", "peer_id="+peerId+"&random_id=0&message="+ encode(message)); + String send = method("messages.send", "peer_id="+peerId+"&random_id=0&message="+ encodeParam(message)); return send != null; } @@ -483,9 +523,8 @@ public static String get(String urlToRead) throws VkApiError { rd.close(); return result.toString(); } - catch (SocketTimeoutException e){throw new VkApiError("Connection failed..");} - catch (UnknownHostException e){throw new VkApiError("Connection failed..");} - catch (IOException e){throw new VkApiError(String.valueOf(e));} + catch (SocketTimeoutException | UnknownHostException e){throw new VkApiError("Connection failed..");} + catch (IOException e) { throw new VkApiError(String.valueOf(e));} } } diff --git a/src/test/java/LongPollAPIFromInterface.java b/src/test/java/LongPollAPIFromInterface.java index c0feadd..8ac1272 100644 --- a/src/test/java/LongPollAPIFromInterface.java +++ b/src/test/java/LongPollAPIFromInterface.java @@ -1,44 +1,42 @@ +import com.sun.org.apache.regexp.internal.RE; import org.json.JSONObject; import santaspeen.vk.api.features.VkAPIAccountTypes; import santaspeen.vk.api.exceptions.VkApiError; +import santaspeen.vk.api.features.onNewVkEvent; import santaspeen.vk.api.features.onVkMessage; import santaspeen.vk.api.parsers.parseMessage; import santaspeen.vk.api.VkApi; +import java.util.Arrays; + public class LongPollAPIFromInterface { - public static final VkApi api = new VkApi("1a8dfd16d88cca3661e1cb08a28ddf1c33636baa428eed4ef902fa6460f3a35b40c02fcd34bb3d09e2061"); + public static final VkApi api = new VkApi("TOKEN"); public static void main(String[] args) throws VkApiError { - api.setAccountType(VkAPIAccountTypes.USER); // (!) Если токен группы, не обязательно. + api.setAccountType(VkAPIAccountTypes.GROUP); // (!) Если токен группы, не обязательно. - api.bindCommands(new LongPollAPIFromInterface()).start(); + api.bindCommands(new LongPollAPIFromInterface()); - } + Thread thread = api.startWithThread(); + System.out.println(thread.getName()); - @onVkMessage(text = "hi", startsWith = true, returnLongPoll = true) - public void helloWorld(parseMessage msg, VkApi api, JSONObject longPoll) throws VkApiError { - - String out = "Текст: "+msg.text+"\nОт: @id"+msg.fromId+"\nВ: "+msg.peerId; + } - System.out.println(out); - System.out.println(longPoll); - api.messagesSend(msg.peerId, "!"); + @onNewVkEvent() // 0.9.2 - Not work yet + public void newEvent(JSONObject event){ + System.out.println(event); } - @onVkMessage(text = "ks", startsWith = true) - public void helloWorld2(parseMessage msg, VkApi api) throws VkApiError { + @onVkMessage(text = "hi", startsWith = true, returnLongPoll = true) + public void newMessage(parseMessage msg, VkApi api, JSONObject longPoll) throws VkApiError { - String out = "Текст: "+msg.text+"\nОт: @id"+msg.fromId+"\nВ: "+msg.peerId; + System.out.println("Текст: "+msg.text+"\nОт: @id"+msg.fromId+"\nВ: "+msg.peerId); + System.out.println(longPoll); - System.out.println(out); - api.messagesSend(msg.peerId, "!!"); + if (api.userId != msg.fromId) + api.messagesSend(msg.peerId, "hi"); } +} -// In chat In private -// msgid flags peer_id timestamp text ХЗ, честно вложения random_id msgid flags peer_id timestamp text ХЗ, честно вложения random_id -// Non-self [4,63214,532497,2000000035,1597576801,"hi",{"from":"370926160"},{},0] [4,63219,49, 370926160, 1597577183,"hi",{"title":" ... "},{},0] -// Self [4,63215,8227, 2000000035,1597576801,"hi",{"from":"583018016"},{},0] [4,63220,35, 370926160, 1597577184,"hi",{"title":" ... "},{},0] - -} \ No newline at end of file