3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-02 18:19:33 +01:00

Plugins/Wolfram: rename api_key to appid

This commit is contained in:
Pragmatic Software 2021-07-06 12:33:29 -07:00
parent 868350c67b
commit c3e1c60a37

View File

@ -14,37 +14,45 @@ use PBot::Imports;
use LWP::UserAgent::Paranoid;
use URI::Escape qw/uri_escape_utf8/;
# called when plugin is loaded
sub initialize {
my ($self, %conf) = @_;
$self->{pbot}->{registry}->add_default('text', 'wolfram', 'api_key', '');
# add default registry entry
$self->{pbot}->{registry}->add_default('text', 'wolfram', 'appid', '');
$self->{pbot}->{registry}->set_default('wolfram', 'api_key', 'private', 1);
# make registry entry private by default
$self->{pbot}->{registry}->set_default('wolfram', 'appid', 'private', 1);
# registry `wolfram` bot command
$self->{pbot}->{commands}->register(sub { $self->cmd_wolfram(@_) }, 'wolfram', 0);
}
# called when plugin is unloaded
sub unload {
my ($self) = @_;
# unregister `wolfram` bot command
$self->{pbot}->{commands}->unregister('wolfram');
}
# `wolfram` bot command
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');
my $appid = $self->{pbot}->{registry}->get_value('wolfram', 'appid');
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.";
if (not length $appid) {
return "$context->{nick}: Registry item wolfram.appid is not set. See https://developer.wolframalpha.com/portal/myapps to get an appid.";
}
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");
my $response = $ua->get("https://api.wolframalpha.com/v1/result?appid=$appid&i=$question&units=$units&timeout=10");
if ($response->is_success) {
return "$context->{nick}: " . $response->decoded_content;