Skip to content

Commit ff007a2

Browse files
Added /wolfram-alpha command
Co-authored-by: java-coding-prodigy <java-coding-prodigy@github.com>
1 parent 032dee5 commit ff007a2

25 files changed

+1473
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ gradle-app.setting
145145
# End of https://www.toptal.com/developers/gitignore/api/netbeans,intellij,java,gradle,eclipse
146146
application/db/
147147
config.json
148+
application/config.json
148149
*.db
149150
*.db-shm
150151
*.db-wal

application/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ dependencies {
4848

4949
implementation 'org.jooq:jooq:3.15.3'
5050

51+
implementation 'io.mikael:urlbuilder:2.0.9'
52+
5153
implementation 'org.scilab.forge:jlatexmath:1.0.7'
5254
implementation 'org.scilab.forge:jlatexmath-font-greek:1.0.7'
5355
implementation 'org.scilab.forge:jlatexmath-font-cyrillic:1.0.7'
5456

5557
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.13.0'
5658
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.0'
59+
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.13.0'
5760
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.0'
5861

5962
implementation 'com.github.freva:ascii-table:1.2.0'

application/config.json.template

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@
3232
"hostBlacklist": ["bit.ly"],
3333
"suspiciousHostKeywords": ["discord", "nitro", "premium"],
3434
"isHostSimilarToKeywordDistanceThreshold": 2
35-
}
35+
},
36+
"wolframAlphaAppId": "79J52T-6239TVXHR7"
3637
}

application/src/main/java/org/togetherjava/tjbot/commands/Features.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.togetherjava.tjbot.commands.free.FreeChannelMonitor;
1111
import org.togetherjava.tjbot.commands.free.FreeCommand;
1212
import org.togetherjava.tjbot.commands.mathcommands.TeXCommand;
13+
import org.togetherjava.tjbot.commands.mathcommands.wolframalpha.WolframAlphaCommand;
1314
import org.togetherjava.tjbot.commands.moderation.*;
1415
import org.togetherjava.tjbot.commands.moderation.scam.ScamBlocker;
1516
import org.togetherjava.tjbot.commands.moderation.scam.ScamHistoryPurgeRoutine;
@@ -105,6 +106,7 @@ public enum Features {
105106
features.add(new QuarantineCommand(actionsStore, config));
106107
features.add(new UnquarantineCommand(actionsStore, config));
107108
features.add(new WhoIsCommand());
109+
features.add(new WolframAlphaCommand(config));
108110

109111
// Mixtures
110112
features.add(new FreeCommand(config, freeChannelMonitor));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package org.togetherjava.tjbot.commands.mathcommands.wolframalpha;
2+
3+
import io.mikael.urlbuilder.UrlBuilder;
4+
import net.dv8tion.jda.api.entities.Message;
5+
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
6+
import net.dv8tion.jda.api.interactions.callbacks.IDeferrableCallback;
7+
import net.dv8tion.jda.api.interactions.commands.OptionType;
8+
import net.dv8tion.jda.api.requests.restaction.WebhookMessageUpdateAction;
9+
import org.jetbrains.annotations.NotNull;
10+
import org.togetherjava.tjbot.commands.SlashCommandAdapter;
11+
import org.togetherjava.tjbot.commands.SlashCommandVisibility;
12+
import org.togetherjava.tjbot.config.Config;
13+
14+
import java.net.http.HttpClient;
15+
import java.net.http.HttpRequest;
16+
import java.net.http.HttpResponse;
17+
import java.util.concurrent.CompletableFuture;
18+
19+
/**
20+
* Command to send a query to the <a href="https://www.wolframalpha.com/">Wolfram Alpha</a> API.
21+
* Renders its response as images.
22+
*/
23+
public final class WolframAlphaCommand extends SlashCommandAdapter {
24+
private static final String QUERY_OPTION = "query";
25+
/**
26+
* WolframAlpha API endpoint to connect to.
27+
*
28+
* @see <a href=
29+
* "https://products.wolframalpha.com/docs/WolframAlpha-API-Reference.pdf">WolframAlpha API
30+
* Reference</a>.
31+
*/
32+
private static final String API_ENDPOINT = "http://api.wolframalpha.com/v2/query";
33+
private static final HttpClient CLIENT = HttpClient.newHttpClient();
34+
35+
private final String appId;
36+
37+
/**
38+
* Creates a new instance.
39+
*
40+
* @param config the config to use
41+
*/
42+
public WolframAlphaCommand(@NotNull Config config) {
43+
super("wolfram-alpha", "Renders mathematical queries using WolframAlpha",
44+
SlashCommandVisibility.GUILD);
45+
getData().addOption(OptionType.STRING, QUERY_OPTION, "the query to send to WolframAlpha",
46+
true);
47+
appId = config.getWolframAlphaAppId();
48+
}
49+
50+
@Override
51+
public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) {
52+
String query = event.getOption(QUERY_OPTION).getAsString();
53+
WolframAlphaHandler handler = new WolframAlphaHandler(query);
54+
55+
// The API call takes a bit
56+
event.deferReply().queue();
57+
58+
// Send query
59+
HttpRequest request = HttpRequest
60+
.newBuilder(UrlBuilder.fromString(API_ENDPOINT)
61+
.addParameter("appid", appId)
62+
.addParameter("format", "image,plaintext")
63+
.addParameter("input", query)
64+
.toUri())
65+
.GET()
66+
.build();
67+
68+
CompletableFuture<HttpResponse<String>> apiResponse =
69+
CLIENT.sendAsync(request, HttpResponse.BodyHandlers.ofString());
70+
71+
// Parse and respond
72+
apiResponse.thenApply(handler::handleApiResponse)
73+
.thenAccept(response -> sendResponse(response, event));
74+
}
75+
76+
private static void sendResponse(@NotNull WolframAlphaHandler.HandlerResponse response,
77+
@NotNull IDeferrableCallback event) {
78+
WebhookMessageUpdateAction<Message> action =
79+
event.getHook().editOriginalEmbeds(response.embeds());
80+
81+
for (WolframAlphaHandler.Attachment attachment : response.attachments()) {
82+
action = action.addFile(attachment.data(), attachment.name());
83+
}
84+
85+
action.queue();
86+
}
87+
}

0 commit comments

Comments
 (0)