2021-07-06 21:03:30 +02:00
|
|
|
# File: Wolfram.pm
|
|
|
|
#
|
|
|
|
# Purpose: Query Wolfram|Alpha's Short Answers API.
|
|
|
|
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2021-07-06 21:03:30 +02:00
|
|
|
|
2021-07-14 04:45:56 +02:00
|
|
|
package PBot::Plugin::Wolfram;
|
|
|
|
use parent 'PBot::Plugin::Base';
|
2021-07-06 21:03:30 +02:00
|
|
|
|
|
|
|
use PBot::Imports;
|
|
|
|
|
|
|
|
use LWP::UserAgent::Paranoid;
|
|
|
|
use URI::Escape qw/uri_escape_utf8/;
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
2021-07-06 21:46:51 +02:00
|
|
|
# add default registry entry for `wolfram.appid`
|
2021-07-06 21:33:29 +02:00
|
|
|
$self->{pbot}->{registry}->add_default('text', 'wolfram', 'appid', '');
|
2021-07-06 21:03:30 +02:00
|
|
|
|
2021-07-06 21:46:51 +02:00
|
|
|
# make `wolfram.appid` registry entry private by default
|
2021-07-06 21:33:29 +02:00
|
|
|
$self->{pbot}->{registry}->set_default('wolfram', 'appid', 'private', 1);
|
2021-07-06 21:03:30 +02:00
|
|
|
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_wolfram(@_) }, 'wolfram', 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub unload {
|
2021-07-06 21:22:52 +02:00
|
|
|
my ($self) = @_;
|
2021-07-06 21:03:30 +02:00
|
|
|
$self->{pbot}->{commands}->unregister('wolfram');
|
|
|
|
}
|
|
|
|
|
|
|
|
sub cmd_wolfram {
|
|
|
|
my ($self, $context) = @_;
|
|
|
|
|
2021-07-06 21:22:52 +02:00
|
|
|
return "Usage: wolfram <query>\n" if not length $context->{arguments};
|
2021-07-06 21:03:30 +02:00
|
|
|
|
2021-07-06 21:33:29 +02:00
|
|
|
my $appid = $self->{pbot}->{registry}->get_value('wolfram', 'appid');
|
2021-07-06 21:03:30 +02:00
|
|
|
|
2021-07-06 21:33:29 +02:00
|
|
|
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.";
|
2021-07-06 21:03:30 +02:00
|
|
|
}
|
|
|
|
|
2021-07-06 21:37:50 +02:00
|
|
|
my $question = uri_escape_utf8 $context->{arguments};
|
|
|
|
my $units = uri_escape_utf8 ($self->{pbot}->{users}->get_user_metadata($context->{from}, $context->{hostmask}, 'units') // 'metric');
|
|
|
|
|
2021-07-06 21:03:30 +02:00
|
|
|
my $ua = LWP::UserAgent::Paranoid->new(agent => 'Mozilla/5.0', request_timeout => 10);
|
|
|
|
|
2021-07-06 21:33:29 +02:00
|
|
|
my $response = $ua->get("https://api.wolframalpha.com/v1/result?appid=$appid&i=$question&units=$units&timeout=10");
|
2021-07-06 21:03:30 +02:00
|
|
|
|
|
|
|
if ($response->is_success) {
|
|
|
|
return "$context->{nick}: " . $response->decoded_content;
|
2021-07-06 21:22:52 +02:00
|
|
|
}
|
2021-07-08 05:25:16 +02:00
|
|
|
elsif ($response->code == 501) {
|
2021-07-07 04:20:07 +02:00
|
|
|
return "$context->{nick}: I don't know what that means.";
|
2021-07-06 21:22:52 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-07-07 04:20:07 +02:00
|
|
|
return "$context->{nick}: Failed to query Wolfram|Alpha: " . $response->status_line;
|
2021-07-06 21:03:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|