From 3e747e80a2e70cf62fa7fb12e945b7457298f020 Mon Sep 17 00:00:00 2001 From: SantaSpeen Date: Sun, 16 Aug 2020 17:34:07 +0300 Subject: [PATCH] 0.9.1 - fix Intarface --- src/main/java/santaspeen/vk/api/vkApi.java | 38 +++++++++++++++------ src/test/java/LongPollAPIAndParse.java | 1 - src/test/java/LongPollAPIFromInterface.java | 15 +++++++- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/main/java/santaspeen/vk/api/vkApi.java b/src/main/java/santaspeen/vk/api/vkApi.java index 9c171af..26b483d 100644 --- a/src/main/java/santaspeen/vk/api/vkApi.java +++ b/src/main/java/santaspeen/vk/api/vkApi.java @@ -104,13 +104,21 @@ public Thread bindCommands(Object... objects) throws VkApiError { // Validate parameter types Class[] params = method.getParameterTypes(); + + if (params.length < 2){ + notToBind(params, method, obj, "parseMessage and VkApi. (JSONObject - optional)"); + continue; + } + if (params[0] != parseMessage.class || params[1] != VkApi.class){ - if (params.length < 3 || params[2] != JSONObject.class){ - notToBind(params, method, obj, "parseMessage, VkApi JSONObject"); - continue; - } else if (params.length < 2) { + + if (params.length == 2) { notToBind(params, method, obj, "parseMessage and VkApi"); continue; + + } else if (params.length > 3 || params[2] != JSONObject.class){ + notToBind(params, method, obj, "parseMessage, VkApi and JSONObject"); + continue; } } @@ -127,12 +135,21 @@ public Thread bindCommands(Object... objects) throws VkApiError { commandsStartsWith.add(startsWith); commandsReturnLongPoll.add(returnLongPoll); - commandsMethod.add(method); - commandsObj.add(obj); } + + commandsMethod.add(method); + commandsObj.add(obj); } } + 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(); @@ -198,14 +215,14 @@ public Thread bindCommands(Object... objects) throws VkApiError { /** * Set account type. * Default: - * VkApi.GROUP. + * VkAPIAccountTypes.GROUP. * * Use: * api.setAccountType(vkApi.USER); * * @throws VkApiError Api Error - * @since 0.5 - *@param type > Account type + * @since v0.5 + * @param type > Account type */ public void setAccountType(VkAPIAccountTypes type) throws VkApiError { if (type.equals(VkAPIAccountTypes.USER)) @@ -246,10 +263,11 @@ private String errorOrResponse(String data, String method) throws VkApiError { JSONObject rqAns = parseJson(data); if (rqAns == null){ + rqAns = new JSONObject(); JSONObject emulateJsonError = new JSONObject(); emulateJsonError.put("error_code", 0); emulateJsonError.put("error_msg", "JSONException"); - rqAns.append("error", emulateJsonError); + rqAns.put("error", emulateJsonError); } if (rqAns.optJSONObject("error") != null){ diff --git a/src/test/java/LongPollAPIAndParse.java b/src/test/java/LongPollAPIAndParse.java index 206e011..1812d46 100644 --- a/src/test/java/LongPollAPIAndParse.java +++ b/src/test/java/LongPollAPIAndParse.java @@ -1,4 +1,3 @@ -import com.sun.istack.internal.NotNull; import org.json.JSONObject; import santaspeen.vk.api.features.VkAPIAccountTypes; import santaspeen.vk.api.exceptions.VkApiError; diff --git a/src/test/java/LongPollAPIFromInterface.java b/src/test/java/LongPollAPIFromInterface.java index c553f2b..c0feadd 100644 --- a/src/test/java/LongPollAPIFromInterface.java +++ b/src/test/java/LongPollAPIFromInterface.java @@ -24,8 +24,21 @@ public void helloWorld(parseMessage msg, VkApi api, JSONObject longPoll) throws System.out.println(out); System.out.println(longPoll); - api.messagesSend(msg.peerId, "Ку"); + api.messagesSend(msg.peerId, "!"); } + @onVkMessage(text = "ks", startsWith = true) + public void helloWorld2(parseMessage msg, VkApi api) throws VkApiError { + + String out = "Текст: "+msg.text+"\nОт: @id"+msg.fromId+"\nВ: "+msg.peerId; + + System.out.println(out); + api.messagesSend(msg.peerId, "!!"); + } + +// 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