-
-
Notifications
You must be signed in to change notification settings - Fork 100
Migrate WolframAlpha command #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
913da39
Added WolframAlpha command
java-coding-prodigy 9ed826f
Fixed most issues after rebase, code style, package refactoring
Zabuzard 486f8d2
Some adjustments, inlined "Constants", removed utility
Zabuzard c1da29b
Reworked /wolfram-alpha and its code
Zabuzard fbfaf8f
removed obsolete full package name
Zabuzard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...n/java/org/togetherjava/tjbot/commands/mathcommands/wolframalpha/WolframAlphaCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package org.togetherjava.tjbot.commands.mathcommands.wolframalpha; | ||
|
||
import io.mikael.urlbuilder.UrlBuilder; | ||
import net.dv8tion.jda.api.entities.Message; | ||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; | ||
import net.dv8tion.jda.api.interactions.callbacks.IDeferrableCallback; | ||
import net.dv8tion.jda.api.interactions.commands.OptionType; | ||
import net.dv8tion.jda.api.requests.restaction.WebhookMessageUpdateAction; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.togetherjava.tjbot.commands.SlashCommandAdapter; | ||
import org.togetherjava.tjbot.commands.SlashCommandVisibility; | ||
import org.togetherjava.tjbot.config.Config; | ||
|
||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* Command to send a query to the <a href="https://www.wolframalpha.com/">Wolfram Alpha</a> API. | ||
* Renders its response as images. | ||
*/ | ||
public final class WolframAlphaCommand extends SlashCommandAdapter { | ||
private static final String QUERY_OPTION = "query"; | ||
/** | ||
* WolframAlpha API endpoint to connect to. | ||
* | ||
* @see <a href= | ||
* "https://products.wolframalpha.com/docs/WolframAlpha-API-Reference.pdf">WolframAlpha API | ||
* Reference</a>. | ||
*/ | ||
private static final String API_ENDPOINT = "http://api.wolframalpha.com/v2/query"; | ||
private static final HttpClient CLIENT = HttpClient.newHttpClient(); | ||
|
||
private final String appId; | ||
|
||
/** | ||
* Creates a new instance. | ||
* | ||
* @param config the config to use | ||
*/ | ||
public WolframAlphaCommand(@NotNull Config config) { | ||
super("wolfram-alpha", "Renders mathematical queries using WolframAlpha", | ||
SlashCommandVisibility.GUILD); | ||
getData().addOption(OptionType.STRING, QUERY_OPTION, "the query to send to WolframAlpha", | ||
true); | ||
appId = config.getWolframAlphaAppId(); | ||
} | ||
|
||
@Override | ||
public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { | ||
String query = event.getOption(QUERY_OPTION).getAsString(); | ||
WolframAlphaHandler handler = new WolframAlphaHandler(query); | ||
|
||
// The API call takes a bit | ||
event.deferReply().queue(); | ||
|
||
// Send query | ||
Zabuzard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
HttpRequest request = HttpRequest | ||
.newBuilder(UrlBuilder.fromString(API_ENDPOINT) | ||
.addParameter("appid", appId) | ||
.addParameter("format", "image,plaintext") | ||
.addParameter("input", query) | ||
.toUri()) | ||
.GET() | ||
.build(); | ||
|
||
CompletableFuture<HttpResponse<String>> apiResponse = | ||
CLIENT.sendAsync(request, HttpResponse.BodyHandlers.ofString()); | ||
|
||
// Parse and respond | ||
apiResponse.thenApply(handler::handleApiResponse) | ||
.thenAccept(response -> sendResponse(response, event)); | ||
} | ||
|
||
private static void sendResponse(@NotNull WolframAlphaHandler.HandlerResponse response, | ||
@NotNull IDeferrableCallback event) { | ||
WebhookMessageUpdateAction<Message> action = | ||
event.getHook().editOriginalEmbeds(response.embeds()); | ||
|
||
for (WolframAlphaHandler.Attachment attachment : response.attachments()) { | ||
action = action.addFile(attachment.data(), attachment.name()); | ||
} | ||
|
||
action.queue(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.