add bovine

This commit is contained in:
Eric S. Londres 2021-08-21 22:05:29 -04:00
parent 788dca687f
commit d348b548c1
Signed by: slondr
GPG Key ID: A2D25B4D5CB970E4
1 changed files with 158 additions and 141 deletions

View File

@ -1,27 +1,25 @@
// BeerHolderBot // BeerHolderBot
// Copyright (C) 2021 Eric S. Londres // Copyright (C) 2021 Eric S. Londres
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published // it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or // by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details. // GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
#![allow(non_snake_case)] #![allow(non_snake_case)]
use teloxide::{prelude::*, utils::command::BotCommand, requests::ResponseResult}; use teloxide::{prelude::*, utils::command::BotCommand, requests::ResponseResult};
use tokio::sync::Mutex; use tokio::sync::Mutex;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::error::Error; use std::{error::Error, fs::File, io::prelude::*};
use std::fs::File;
use std::io::prelude::*;
use rand::prelude::*; use rand::prelude::*;
type AsyncResult<T> = Result<T, Box<dyn Error + Send + Sync>>; type AsyncResult<T> = Result<T, Box<dyn Error + Send + Sync>>;
@ -115,10 +113,10 @@ async fn quaff(id: i64) -> AsyncResult<String> {
} }
} }
async fn harvest_corn() -> AsyncResult<String> { async fn grab_photo(query: &'static str) -> AsyncResult<String> {
if let Some(access) = std::env::var_os("UNSPLASH_ACCESS") { if let Some(access) = std::env::var_os("UNSPLASH_ACCESS") {
// call API to get a random picture of corn // call API to get a random picture of corn
let auth_uri = format!("https://api.unsplash.com/photos/random/?client_id={}&query={}", access.into_string().unwrap(), "corn"); let auth_uri = format!("https://api.unsplash.com/photos/random/?client_id={}&query={}", access.into_string().unwrap(), query);
let response = reqwest::get(&auth_uri) let response = reqwest::get(&auth_uri)
.await.unwrap().text().await.unwrap(); .await.unwrap().text().await.unwrap();
// response format is some pretty nested json // response format is some pretty nested json
@ -134,6 +132,14 @@ async fn harvest_corn() -> AsyncResult<String> {
} }
} }
async fn harvest_corn() -> AsyncResult<String> {
grab_photo("corn").await
}
async fn bovine() -> AsyncResult<String> {
grab_photo("cow").await
}
//// TO IMPLEMENT A NEW COMMAND //// TO IMPLEMENT A NEW COMMAND
// Add the command name to the Command enum with a description // Add the command name to the Command enum with a description
// Implement the logic of the command in the match statement in answer() // Implement the logic of the command in the match statement in answer()
@ -156,7 +162,9 @@ enum Command {
#[command(description = "Get the number of beers on tap")] #[command(description = "Get the number of beers on tap")]
Count, Count,
#[command(description = "Die stupidly")] #[command(description = "Die stupidly")]
Yasd Yasd,
#[command(description = "Take a tripe to the pasture")]
Bovine
} }
async fn answer(cx: UpdateWithCx<AutoSend<Bot>, Message>, command: Command) -> ResponseResult<()> { async fn answer(cx: UpdateWithCx<AutoSend<Bot>, Message>, command: Command) -> ResponseResult<()> {
@ -228,6 +236,15 @@ async fn answer(cx: UpdateWithCx<AutoSend<Bot>, Message>, command: Command) -> R
cx.reply_to("You don't have a farm.").await? cx.reply_to("You don't have a farm.").await?
} }
}, },
Command::Bovine => {
log::info!("cow time");
if let Ok(bovine) = bovine().await {
cx.answer_photo(teloxide::types::InputFile::url(bovine)).await?
} else {
log::error!("An error occurred within bovine()");
cx.reply_to("No bovine").await?
}
},
Command::Post => { Command::Post => {
log::info!("Generating new message"); log::info!("Generating new message");
let new_msg = telegram_markov_chain::chain(); let new_msg = telegram_markov_chain::chain();