Plugins/Wolfram: add `wolfram` command (#58)

This commit is contained in:
Naïm Favier 2021-07-06 21:03:30 +02:00 committed by GitHub
parent 17b2eb5a3a
commit 525fec0702
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 1 deletions

56
Plugins/Wolfram.pm Normal file
View File

@ -0,0 +1,56 @@
# File: Wolfram.pm
#
# Purpose: Query Wolfram|Alpha's Short Answers API.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
package Plugins::Wolfram;
use parent 'Plugins::Plugin';
use PBot::Imports;
use LWP::UserAgent::Paranoid;
use URI::Escape qw/uri_escape_utf8/;
sub initialize {
my ($self, %conf) = @_;
$self->{pbot}->{registry}->add_default('text', 'wolfram', 'api_key', '');
$self->{pbot}->{registry}->set_default('wolfram', 'api_key', 'private', 1);
$self->{pbot}->{commands}->register(sub { $self->cmd_wolfram(@_) }, 'wolfram', 0);
}
sub unload {
my $self = shift;
$self->{pbot}->{commands}->unregister('wolfram');
}
sub cmd_wolfram {
my ($self, $context) = @_;
return "Usage: wolfram QUERY\n" if not length $context->{arguments};
my $api_key = $self->{pbot}->{registry}->get_value('wolfram', 'api_key');
if (not length $api_key) {
return "$context->{nick}: Registry item wolfram.api_key is not set. See https://developer.wolframalpha.com/portal/myapps to get an API key.";
}
my $ua = LWP::UserAgent::Paranoid->new(agent => 'Mozilla/5.0', request_timeout => 10);
my $question = uri_escape_utf8 $context->{arguments};
my $units = uri_escape_utf8($self->{pbot}->{users}->get_user_metadata($context->{from}, $context->{hostmask}, 'units') // 'metric');
my $response = $ua->get("https://api.wolframalpha.com/v1/result?appid=$api_key&i=$question&units=$units&timeout=10");
if ($response->is_success) {
return "$context->{nick}: " . $response->decoded_content;
} else {
return "Failed to query Wolfram|Alpha: " . $response->status_line;
}
}
1;

View File

@ -218,6 +218,7 @@ Plugin | Description
[UrlTitles](Plugins/UrlTitles.pm) | When a URL is seen in a channel, intelligently display its title. It will not display titles that are textually similiar to the URL, in order to maintain the channel signal-noise ratio.
[Quotegrabs](Plugins/Quotegrabs.pm) | Grabs channel messages as quotes for posterity. Can grab messages from anywhere in the channel history. Can grab multiple messages at once!
[Weather](Plugins/Weather.pm) | Fetches and shows weather data for a location.
[Wolfram](Plugins/Wolfram.pm) | Queries Wolfram|Alpha for answers.
[Wttr](Plugins/Wttr.pm) | Advanced weather Plugin with tons of options. Uses wttr.in.
[RemindMe](Plugins/RemindMe.pm) | Lets people set up reminders. Lots of options.
[ActionTrigger](Plugins/ActionTrigger.pm) | Lets admins set regular expression triggers to execute PBot commands or factoids.

View File

@ -19,4 +19,5 @@ RestrictedMod
TypoSub
UrlTitles
Weather
Wolfram
Wttr

View File

@ -522,7 +522,7 @@ Usage: `pluglist`
<PBot> Loaded plugins: ActionTrigger, AntiAway, AntiKickAutoRejoin, AntiNickSpam,
AntiRepeat, AntiTwitter, AutoRejoin, Battleship, Connect4, Counter, Date,
GoogleSearch, Quotegrabs, RemindMe, RestrictedMod, Spinach, TypoSub, UrlTitles,
Weather, Wttr
Weather, Wolfram, Wttr
## Command metadata commands
### cmdset