Используется последняя версия VK API

This commit is contained in:
Victor 2016-03-10 12:46:53 +02:00
parent 0a293094cb
commit 6a1c4c3287
5 changed files with 41 additions and 27 deletions

View File

@ -79,7 +79,7 @@ public final class IOUtil {
public static String post(String link, byte[] data) { public static String post(String link, byte[] data) {
try { try {
final URL url = new URL(link); final URL url = new URL(link);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST"); con.setRequestMethod("POST");
con.setDoOutput(true); con.setDoOutput(true);
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) { try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {

View File

@ -1,6 +1,8 @@
package holdfast.samobot; package holdfast.samobot;
import holdfast.samobot.commands.*; import holdfast.samobot.commands.*;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.json.JSONArray; import org.json.JSONArray;
@ -67,10 +69,10 @@ public class MainThread implements Runnable {
JSONObject response = new JSONObject(obj).getJSONObject("response"); JSONObject response = new JSONObject(obj).getJSONObject("response");
pts = response.getInt("new_pts"); 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) { 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; String message;
if (Util.random(6) == 2) { if (Util.random(6) == 2) {
message = MESSAGES[Util.random(MESSAGES.length)]; message = MESSAGES[Util.random(MESSAGES.length)];
@ -78,12 +80,19 @@ public class MainThread implements Runnable {
message = AnnimonResponse.getResponse("jhgjh"); message = AnnimonResponse.getResponse("jhgjh");
} }
VK.sendMessage(message, null, lastChatId, 0); VK.sendMessage(message, null, lastChatId, 0);
} else { } else if (messagesLength > 0) {
for (int i = 1; i < messagesLength; i++) { final int profilesLength = jsonProfiles.length();
final JSONObject profile = profiles.getJSONObject(i - 1); final Map<Integer, JSONObject> profiles = new HashMap<>(profilesLength);
if (profile.getInt("uid") != ACCOUNT_ID) { for (int i = 0; i < profilesLength; i++) {
final JSONObject currentMessage = messages.getJSONObject(i); final JSONObject profile = jsonProfiles.getJSONObject(i);
answer(currentMessage, profile); 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 { private void answer(JSONObject currentMessage, JSONObject profile) throws Exception {
String message = currentMessage.getString("body"); String message = currentMessage.getString("body");
final int forward = currentMessage.getInt("mid"); final int chatId = lastChatId = currentMessage.optInt("chat_id", currentMessage.getInt("user_id"));
final int chatId = lastChatId = currentMessage.optInt("chat_id", currentMessage.getInt("uid"));
final boolean toUser = !currentMessage.has("chat_id");
final String userName = profile.getString("first_name"); final String userName = profile.getString("first_name");
Matcher match = Pattern.compile("^("+Config.BOT_NAMES + "),? ?(.+)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE).matcher(message); Matcher match = Pattern.compile("^("+Config.BOT_NAMES + "),? ?(.+)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE).matcher(message);
if (!match.matches()) return; if (!match.matches()) return;
@ -112,7 +118,7 @@ public class MainThread implements Runnable {
final String cmd = words[0].toLowerCase(); final String cmd = words[0].toLowerCase();
for (Command command : commands) { for (Command command : commands) {
if (!command.match(cmd)) continue; 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)); Log.chatln(String.format("(%s): %s", userName, message));
StatisticsProcessor.updateUsername(userName); StatisticsProcessor.updateUsername(userName);
StatisticsProcessor.updateAnswersCounter(); StatisticsProcessor.updateAnswersCounter();

View File

@ -11,7 +11,8 @@ import org.json.JSONObject;
*/ */
public class VK { 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) { public static void sendMessage(String text, String attachment, int chatId, int forward) {
sendMessage(text, attachment, chatId, (chatId > 10000), forward); sendMessage(text, attachment, chatId, (chatId > 10000), forward);
@ -43,7 +44,7 @@ public class VK {
} }
public static int getLastPTS() { 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.queryln(" > get last pts");
Log.query(" < get last pts: "); Log.query(" < get last pts: ");
Log.queryln(query, Log.SMALL); Log.queryln(query, Log.SMALL);
@ -51,11 +52,11 @@ public class VK {
} }
public static String getUploadServer() { 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) { 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.queryln(" > get unread " + pts, Log.GREEN);
Log.query(" < get unread: "); Log.query(" < get unread: ");
Log.queryln(result, Log.SMALL); Log.queryln(result, Log.SMALL);
@ -129,10 +130,14 @@ public class VK {
} }
public static String uploadPhoto(String photoUrl) { public static String uploadPhoto(String photoUrl) {
return uploadPhoto(IOUtil.download(photoUrl), getExtension(photoUrl));
}
public static String uploadPhoto(byte[] imageBytes, String extension) {
String uploadServer = getUploadServer(); String uploadServer = getUploadServer();
Log.queryln(" > upload photo to " + uploadServer); 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.query(" < upload photo result: ");
Log.queryln(uploadResultRaw, Log.SMALL); Log.queryln(uploadResultRaw, Log.SMALL);
JSONObject uploadResult = new JSONObject(uploadResultRaw); JSONObject uploadResult = new JSONObject(uploadResultRaw);

View File

@ -33,7 +33,9 @@ public final class AnnimonResponse extends Command {
public static String getResponse(String query) { public static String getResponse(String query) {
try { 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" + "select%20*%20from%20htmlpost%20where%20url%3D"
+ "\"http%3A%2F%2Fannimon.com%2Fjson%2Fbot_vk.php\"" + "\"http%3A%2F%2Fannimon.com%2Fjson%2Fbot_vk.php\""
+ "%20and%20postdata%3D\"%26text%3D" + URLEncoder.encode(query, "UTF-8") + "%26\"" + "%20and%20postdata%3D\"%26text%3D" + URLEncoder.encode(query, "UTF-8") + "%26\""
@ -43,7 +45,7 @@ public final class AnnimonResponse extends Command {
.getJSONObject("results") .getJSONObject("results")
.getJSONObject("postresult") .getJSONObject("postresult")
.getJSONObject("html") .getJSONObject("html")
.getString("body"); .getString("body");*/
} catch (IOException ex) { } catch (IOException ex) {
Log.error(ex); Log.error(ex);
return null; return null;

View File

@ -1,9 +1,8 @@
package holdfast.samobot.commands; package holdfast.samobot.commands;
import holdfast.samobot.Log;
import holdfast.samobot.VK; import holdfast.samobot.VK;
import java.io.IOException; import java.io.IOException;
import java.net.URLEncoder; import org.json.JSONObject;
/** /**
* *
@ -11,6 +10,7 @@ import java.net.URLEncoder;
*/ */
public abstract class Command { public abstract class Command {
protected JSONObject currentMessage;
protected String[] words; protected String[] words;
protected boolean toUser; protected boolean toUser;
protected int chatId; protected int chatId;
@ -25,11 +25,12 @@ public abstract class Command {
return false; 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.chatId = chatId;
this.toUser = toUser; this.toUser = !currentMessage.has("chat_id");
this.words = words; this.words = words;
this.forward = forward; this.forward = currentMessage.getInt("id");
try { try {
return execute(message, userName); return execute(message, userName);
} catch (IOException ioe) { } catch (IOException ioe) {