实现 /ip /whois 和 186ip 查询 (#17 @186526),添加 apache-commons-text

This commit is contained in:
A.C.Sukazyo Eyre 2021-12-25 20:07:46 +08:00
parent bd29b4355e
commit e548dd1537
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
7 changed files with 102 additions and 4 deletions

View File

@ -20,6 +20,7 @@ repositories {
dependencies {
compileOnlyApi "com.github.spotbugs:spotbugs-annotations:${libSpotbugsVersion}"
implementation "org.apache.commons:commons-text:${libApacheCommonsTextVersion}"
api "cc.sukazyo:messiva:${libMessivaVersion}"

View File

@ -1,10 +1,11 @@
## Core
VERSION = 0.4.2.9
VERSION = 0.4.2.10
# dependencies
libSpotbugsVersion = 4.5.0
libSpotbugsVersion = 4.5.2
libApacheCommonsTextVersion = 1.9
libMessivaVersion = 0.1.0.1

View File

@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny;
* the final field that will be updated by gradle automatically.
*/
public class GradleProjectConfigures {
public static final String VERSION = "0.4.2.9";
public static final long COMPILE_TIMESTAMP = 1640371604243L;
public static final String VERSION = "0.4.2.10";
public static final long COMPILE_TIMESTAMP = 1640434011159L;
}

View File

@ -7,6 +7,7 @@ import cc.sukazyo.cono.morny.bot.api.EventListener;
import cc.sukazyo.cono.morny.bot.api.InputCommand;
import cc.sukazyo.cono.morny.bot.event.on_commands.EventHack;
import cc.sukazyo.cono.morny.bot.event.on_commands.GetUsernameAndId;
import cc.sukazyo.cono.morny.bot.event.on_commands.Ip186Query;
import cc.sukazyo.cono.morny.data.MornyJrrp;
import cc.sukazyo.cono.morny.data.TelegramStickers;
import cc.sukazyo.cono.morny.util.CommonFormatUtils;
@ -56,6 +57,10 @@ public class OnCommandExecute extends EventListener {
case "/jrrp":
onCommandJrrpExec(event);
break;
case "/ip":
case "/whois":
Ip186Query.exec(event, command);
break;
default:
return nonCommandExecutable(event, command);
}

View File

@ -0,0 +1,47 @@
package cc.sukazyo.cono.morny.bot.event.on_commands;
import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.bot.api.InputCommand;
import cc.sukazyo.cono.morny.data.ip186.IP186QueryResponse;
import cc.sukazyo.cono.morny.data.ip186.IP186QueryHandler;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.SendMessage;
import org.apache.commons.text.StringEscapeUtils;
import javax.annotation.Nonnull;
public class Ip186Query {
public static void exec (@Nonnull Update event, @Nonnull InputCommand command) {
if (!command.hasArgs()) { MornyCoeur.getAccount().execute(new SendMessage(
event.message().chat().id(),
"[Unavailable] No ip defined."
).replyToMessageId(event.message().messageId())); return; }
if (command.getArgs().length > 1) { MornyCoeur.getAccount().execute(new SendMessage(
event.message().chat().id(),
"[Unavailable] Too much arguments."
).replyToMessageId(event.message().messageId())); return; }
try {
IP186QueryResponse response = switch (command.getCommand()) {
case "/ip" -> IP186QueryHandler.queryIp(command.getArgs()[0]);
case "/whois" -> IP186QueryHandler.queryWhois(command.getArgs()[0]);
default -> throw new IllegalArgumentException("Unknown 186-IP query method " + command.getCommand());
};
MornyCoeur.getAccount().execute(new SendMessage(
event.message().chat().id(),
response.url() + "\n<code>" + StringEscapeUtils.escapeHtml4(response.body()) + "</code>"
).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId()));
} catch (Exception e) {
MornyCoeur.getAccount().execute(new SendMessage(
event.message().chat().id(),
"[Exception] in query:\n<code>" + e.getMessage() + "</code>"
).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId()));
}
}
}

View File

@ -0,0 +1,40 @@
package cc.sukazyo.cono.morny.data.ip186;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import javax.annotation.Nonnull;
import java.io.IOException;
public class IP186QueryHandler {
public static final String SITE_URL = "https://ip.186526.xyz/";
private static final String QUERY_IP_PARAM = "type=json&format=true";
private static final String QUERY_WHOIS_PARAM = "type=plain";
private static final OkHttpClient httpClient = new OkHttpClient();
public static IP186QueryResponse queryIp (String ip) throws IOException {
final String requestUrl = SITE_URL + ip;
return commonQuery(requestUrl, QUERY_IP_PARAM);
}
public static IP186QueryResponse queryWhois (String domain) throws IOException {
final String requestUrl = SITE_URL + "whois/" + domain;
return commonQuery(requestUrl, QUERY_WHOIS_PARAM);
}
@Nonnull
private static IP186QueryResponse commonQuery (String requestUrl, String queryIpParam) throws IOException {
Request request = new Request.Builder().url(requestUrl + "?" + queryIpParam).build();
try (Response response = httpClient.newCall(request).execute()) {
final ResponseBody body = response.body();
if (body == null) throw new IOException("Null body.");
return new IP186QueryResponse(requestUrl, body.string());
}
}
}

View File

@ -0,0 +1,4 @@
package cc.sukazyo.cono.morny.data.ip186;
public record IP186QueryResponse(String url, String body) {
}