From 7cbe1e70e440c29375425182e661ea9ff7bd6300 Mon Sep 17 00:00:00 2001 From: Max Audron Date: Sat, 16 Oct 2021 13:49:23 +0200 Subject: rework configuration to allow extensibility by hooks --- src/hooks/wolfram_alpha.rs | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'src/hooks/wolfram_alpha.rs') diff --git a/src/hooks/wolfram_alpha.rs b/src/hooks/wolfram_alpha.rs index 3faddbf..0fceb0b 100644 --- a/src/hooks/wolfram_alpha.rs +++ b/src/hooks/wolfram_alpha.rs @@ -3,12 +3,39 @@ use crate::util::{ web::{quote_plus, IsgdUrlShortener, UrlShortener}, }; use anyhow::{bail, Context, Error, Result}; +use figment::providers::Env; use futures::join; use irc::client::prelude::*; use macros::privmsg; use reqwest::{get, Url}; use serde::{Deserialize, Serialize}; +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct WolframAlpha { + wa_api_key: String, +} + +impl WolframAlpha { + pub fn new(bot: &crate::Bot) -> Result { + bot.figment + .clone() + .merge(Env::prefixed("CATINATOR_")) + .extract() + .context("failed to extract wolfram alpha config") + } + + pub async fn wa(&self, bot: &crate::Bot, msg: Message) -> Result<()> { + privmsg!(msg, { + let content = get_input_query(text)?; + bot.send_privmsg( + msg.response_target() + .context("failed to get response target")?, + &wa_query(&content, Some(&self.wa_api_key), None).await?, + )?; + }) + } +} + #[derive(Serialize, Deserialize, Debug)] struct WaResponse { queryresult: QueryResult, @@ -147,17 +174,6 @@ fn get_input_query(text: &str) -> Result { Ok(content.to_string()) } -pub async fn wa(bot: &crate::Bot, msg: Message) -> Result<()> { - privmsg!(msg, { - let content = get_input_query(text)?; - bot.send_privmsg( - msg.response_target() - .context("failed to get response target")?, - &wa_query(&content, Some(&bot.config.settings.wa_api_key), None).await?, - )?; - }) -} - #[cfg(test)] mod tests { -- cgit v1.2.3