Используется последняя версия VK API
This commit is contained in:
parent
0a293094cb
commit
6a1c4c3287
@ -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())) {
|
||||
|
@ -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<Integer, JSONObject> 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();
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user