|
| 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