diff options
Diffstat (limited to '')
| -rw-r--r-- | src/hooks/wolfram_alpha.rs | 38 |
1 files changed, 27 insertions, 11 deletions
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<WolframAlpha> { + 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<String, Error> { 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 { |
