mirror of
https://github.com/pragma-/pbot.git
synced 2024-11-05 19:49:32 +01:00
Plugins/Wolfram: add wolfram
command (#58)
This commit is contained in:
parent
17b2eb5a3a
commit
525fec0702
56
Plugins/Wolfram.pm
Normal file
56
Plugins/Wolfram.pm
Normal 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;
|
@ -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.
|
||||
|
1
data/plugin_autoload
vendored
1
data/plugin_autoload
vendored
@ -19,4 +19,5 @@ RestrictedMod
|
||||
TypoSub
|
||||
UrlTitles
|
||||
Weather
|
||||
Wolfram
|
||||
Wttr
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user