From 525fec0702702e488ee047f63854fa575cd1bb53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Tue, 6 Jul 2021 21:03:30 +0200 Subject: [PATCH] Plugins/Wolfram: add `wolfram` command (#58) --- Plugins/Wolfram.pm | 56 ++++++++++++++++++++++++++++++++++++++++++++ README.md | 1 + data/plugin_autoload | 1 + doc/Admin.md | 2 +- 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 Plugins/Wolfram.pm diff --git a/Plugins/Wolfram.pm b/Plugins/Wolfram.pm new file mode 100644 index 00000000..bdce23a0 --- /dev/null +++ b/Plugins/Wolfram.pm @@ -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; diff --git a/README.md b/README.md index 64adb12c..c8d5d7ac 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/data/plugin_autoload b/data/plugin_autoload index 782ee46c..52986536 100644 --- a/data/plugin_autoload +++ b/data/plugin_autoload @@ -19,4 +19,5 @@ RestrictedMod TypoSub UrlTitles Weather +Wolfram Wttr diff --git a/doc/Admin.md b/doc/Admin.md index a64008ff..eea2c507 100644 --- a/doc/Admin.md +++ b/doc/Admin.md @@ -522,7 +522,7 @@ Usage: `pluglist` 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