diff options
| author | Max Audron <audron@cocaine.farm> | 2021-10-16 13:49:23 +0200 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2021-10-16 13:53:22 +0200 |
| commit | 7cbe1e70e440c29375425182e661ea9ff7bd6300 (patch) | |
| tree | de36d71d9167eed333f9a216f2d1588941b528c0 /src/hooks/wolfram_alpha.rs | |
| parent | move bot initialization out of macro (diff) | |
rework configuration to allow extensibility by hooksconfig-rework
Diffstat (limited to 'src/hooks/wolfram_alpha.rs')
| -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 { |
