From 6a1c4c328722d1f3b1cfa1899bd04003ff730c29 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 10 Mar 2016 12:46:53 +0200 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7?= =?UTF-8?q?=D1=83=D0=B5=D1=82=D1=81=D1=8F=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5?= =?UTF-8?q?=D0=B4=D0=BD=D1=8F=D1=8F=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F?= =?UTF-8?q?=20VK=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/holdfast/samobot/IOUtil.java | 2 +- src/holdfast/samobot/MainThread.java | 34 +++++++++++-------- src/holdfast/samobot/VK.java | 15 +++++--- .../samobot/commands/AnnimonResponse.java | 6 ++-- src/holdfast/samobot/commands/Command.java | 11 +++--- 5 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/holdfast/samobot/IOUtil.java b/src/holdfast/samobot/IOUtil.java index deeccb1..328d397 100644 --- a/src/holdfast/samobot/IOUtil.java +++ b/src/holdfast/samobot/IOUtil.java @@ -79,7 +79,7 @@ public final class IOUtil { public static String post(String link, byte[] data) { try { final URL url = new URL(link); - HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setDoOutput(true); try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) { diff --git a/src/holdfast/samobot/MainThread.java b/src/holdfast/samobot/MainThread.java index 399b119..6a7dc1c 100644 --- a/src/holdfast/samobot/MainThread.java +++ b/src/holdfast/samobot/MainThread.java @@ -1,6 +1,8 @@ package holdfast.samobot; import holdfast.samobot.commands.*; +import java.util.HashMap; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.json.JSONArray; @@ -67,10 +69,10 @@ public class MainThread implements Runnable { JSONObject response = new JSONObject(obj).getJSONObject("response"); pts = response.getInt("new_pts"); - JSONArray messages = response.getJSONArray("messages"); - JSONArray profiles = response.getJSONArray("profiles"); - final int messagesLength = messages.length(); if (messagesLength == 1 && Util.random(80) == 5) { + JSONArray jsonMessages = response.getJSONObject("messages").getJSONArray("items"); + JSONArray jsonProfiles = response.getJSONArray("profiles"); + final int messagesLength = jsonMessages.length(); String message; if (Util.random(6) == 2) { message = MESSAGES[Util.random(MESSAGES.length)]; @@ -78,12 +80,19 @@ public class MainThread implements Runnable { message = AnnimonResponse.getResponse("jhgjh"); } VK.sendMessage(message, null, lastChatId, 0); - } else { - for (int i = 1; i < messagesLength; i++) { - final JSONObject profile = profiles.getJSONObject(i - 1); - if (profile.getInt("uid") != ACCOUNT_ID) { - final JSONObject currentMessage = messages.getJSONObject(i); - answer(currentMessage, profile); + } else if (messagesLength > 0) { + final int profilesLength = jsonProfiles.length(); + final Map profiles = new HashMap<>(profilesLength); + for (int i = 0; i < profilesLength; i++) { + final JSONObject profile = jsonProfiles.getJSONObject(i); + profiles.put(profile.getInt("id"), profile); + } + + for (int i = 0; i < messagesLength; i++) { + final JSONObject currentMessage = jsonMessages.getJSONObject(i); + final int userId = currentMessage.getInt("user_id"); + if (userId != ACCOUNT_ID) { + answer(currentMessage, profiles.get(userId)); } } } @@ -96,12 +105,9 @@ public class MainThread implements Runnable { private void answer(JSONObject currentMessage, JSONObject profile) throws Exception { String message = currentMessage.getString("body"); - final int forward = currentMessage.getInt("mid"); - final int chatId = lastChatId = currentMessage.optInt("chat_id", currentMessage.getInt("uid")); - final boolean toUser = !currentMessage.has("chat_id"); + final int chatId = lastChatId = currentMessage.optInt("chat_id", currentMessage.getInt("user_id")); final String userName = profile.getString("first_name"); - Matcher match = Pattern.compile("^("+Config.BOT_NAMES + "),? ?(.+)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE).matcher(message); if (!match.matches()) return; @@ -112,7 +118,7 @@ public class MainThread implements Runnable { final String cmd = words[0].toLowerCase(); for (Command command : commands) { if (!command.match(cmd)) continue; - if (command.execute(chatId, toUser, forward, userName, words, message)) { + if (command.execute(currentMessage, chatId, userName, words, message)) { Log.chatln(String.format("(%s): %s", userName, message)); StatisticsProcessor.updateUsername(userName); StatisticsProcessor.updateAnswersCounter(); diff --git a/src/holdfast/samobot/VK.java b/src/holdfast/samobot/VK.java index d585b6a..52f7c5a 100644 --- a/src/holdfast/samobot/VK.java +++ b/src/holdfast/samobot/VK.java @@ -11,7 +11,8 @@ import org.json.JSONObject; */ public class VK { - public static String url = "https://api.vk.com/method/"; + public static final String url = "https://api.vk.com/method/"; + private static final String API_VERSION = "5.48"; public static void sendMessage(String text, String attachment, int chatId, int forward) { sendMessage(text, attachment, chatId, (chatId > 10000), forward); @@ -43,7 +44,7 @@ public class VK { } public static int getLastPTS() { - String query = query("messages.getLongPollServer", "v=5.34&use_ssl=0&need_pts=1"); + String query = query("messages.getLongPollServer", "v="+API_VERSION+"&use_ssl=0&need_pts=1"); Log.queryln(" > get last pts"); Log.query(" < get last pts: "); Log.queryln(query, Log.SMALL); @@ -51,11 +52,11 @@ public class VK { } public static String getUploadServer() { - return (new JSONObject(query("photos.getMessagesUploadServer", "v=5.37")).getJSONObject("response")).getString("upload_url"); + return (new JSONObject(query("photos.getMessagesUploadServer", "v="+API_VERSION)).getJSONObject("response")).getString("upload_url"); } public static String getUnread(int pts) { - String result = VK.query("messages.getLongPollHistory", "pts=" + pts); + String result = VK.query("messages.getLongPollHistory", "v="+API_VERSION+"&pts=" + pts); Log.queryln(" > get unread " + pts, Log.GREEN); Log.query(" < get unread: "); Log.queryln(result, Log.SMALL); @@ -129,10 +130,14 @@ public class VK { } public static String uploadPhoto(String photoUrl) { + return uploadPhoto(IOUtil.download(photoUrl), getExtension(photoUrl)); + } + + public static String uploadPhoto(byte[] imageBytes, String extension) { String uploadServer = getUploadServer(); Log.queryln(" > upload photo to " + uploadServer); - String uploadResultRaw = IOUtil.upload(uploadServer, "photo=", IOUtil.download(photoUrl), getExtension(photoUrl)); + String uploadResultRaw = IOUtil.upload(uploadServer, "photo=", imageBytes, extension); Log.query(" < upload photo result: "); Log.queryln(uploadResultRaw, Log.SMALL); JSONObject uploadResult = new JSONObject(uploadResultRaw); diff --git a/src/holdfast/samobot/commands/AnnimonResponse.java b/src/holdfast/samobot/commands/AnnimonResponse.java index f3dee39..b90940e 100644 --- a/src/holdfast/samobot/commands/AnnimonResponse.java +++ b/src/holdfast/samobot/commands/AnnimonResponse.java @@ -33,7 +33,9 @@ public final class AnnimonResponse extends Command { public static String getResponse(String query) { try { - String response = IOUtil.get("https://query.yahooapis.com/v1/public/yql?q=" + return IOUtil.post("http://annimon.com/json/bot_vk.php", ("text="+URLEncoder.encode(query, "UTF-8")).getBytes("UTF-8")); + + /*String response = IOUtil.get("https://query.yahooapis.com/v1/public/yql?q=" + "select%20*%20from%20htmlpost%20where%20url%3D" + "\"http%3A%2F%2Fannimon.com%2Fjson%2Fbot_vk.php\"" + "%20and%20postdata%3D\"%26text%3D" + URLEncoder.encode(query, "UTF-8") + "%26\"" @@ -43,7 +45,7 @@ public final class AnnimonResponse extends Command { .getJSONObject("results") .getJSONObject("postresult") .getJSONObject("html") - .getString("body"); + .getString("body");*/ } catch (IOException ex) { Log.error(ex); return null; diff --git a/src/holdfast/samobot/commands/Command.java b/src/holdfast/samobot/commands/Command.java index 63df627..bab23d6 100644 --- a/src/holdfast/samobot/commands/Command.java +++ b/src/holdfast/samobot/commands/Command.java @@ -1,9 +1,8 @@ package holdfast.samobot.commands; -import holdfast.samobot.Log; import holdfast.samobot.VK; import java.io.IOException; -import java.net.URLEncoder; +import org.json.JSONObject; /** * @@ -11,6 +10,7 @@ import java.net.URLEncoder; */ public abstract class Command { + protected JSONObject currentMessage; protected String[] words; protected boolean toUser; protected int chatId; @@ -25,11 +25,12 @@ public abstract class Command { return false; } - public boolean execute(int chatId, boolean toUser, int forward, String userName, String[] words, String message) { + public boolean execute(JSONObject currentMessage, int chatId, String userName, String[] words, String message) { + this.currentMessage = currentMessage; this.chatId = chatId; - this.toUser = toUser; + this.toUser = !currentMessage.has("chat_id"); this.words = words; - this.forward = forward; + this.forward = currentMessage.getInt("id"); try { return execute(message, userName); } catch (IOException ioe) {