-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor bot instantiation. Main cleanup
- Loading branch information
Charlie Bacon
committed
Jun 12, 2024
1 parent
e3bca00
commit 936d155
Showing
4 changed files
with
48 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,39 @@ | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
|
||
use teloxide::prelude::*; | ||
use teloxide::{dptree, Bot}; | ||
use tokio::sync::RwLock; | ||
pub mod bot; | ||
pub mod gpt; | ||
pub mod types; | ||
|
||
pub struct TelitairoBot {} | ||
|
||
pub mod types { | ||
use std::{ | ||
collections::{HashMap, VecDeque}, | ||
sync::Arc, | ||
}; | ||
use teloxide::types::{ChatId, Message}; | ||
use tokio::sync::RwLock; | ||
impl TelitairoBot { | ||
pub async fn dispatch() { | ||
let bot = Bot::from_env(); | ||
let buffer: types::Buffer = Arc::new(RwLock::new(HashMap::new())); | ||
|
||
pub const PERSONALITY: &str= "Eres un asistente andaluz con jerga informal y algo irónica. Ayudas a todo aquel que te necesite, no sin antes quejarte un poco, ya que eres algo vago."; | ||
pub const MEDIATE_QUERY: &str= "A partir de los siguientes mensajes, analiza una posible discusión y da la razón a alguno de los implicados, con una pequeña argumentación."; | ||
pub const STORE_CAPACITY: usize = 200; | ||
let handler = dptree::entry() | ||
.branch( | ||
Update::filter_message() | ||
.filter_command::<bot::Command>() | ||
.endpoint(bot::handle_commands), | ||
) | ||
.branch(Update::filter_message().endpoint(bot::handle_messages)); | ||
|
||
pub type Messages = Arc<RwLock<HashMap<ChatId, VecDeque<Message>>>>; | ||
Dispatcher::builder(bot, handler) | ||
.dependencies(dptree::deps![buffer]) | ||
.default_handler(|update| async move { | ||
log::warn!("Unhandled update: {:#?}", update); | ||
}) | ||
.error_handler(LoggingErrorHandler::with_custom_text( | ||
"An error occurred in the dispatcher", | ||
)) | ||
.enable_ctrlc_handler() | ||
.build() | ||
.dispatch() | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,9 @@ | ||
use std::{collections::HashMap, sync::Arc}; | ||
use telitairos_bot::{bot, types}; | ||
use teloxide::prelude::*; | ||
use tokio::sync::RwLock; | ||
use telitairos_bot::TelitairoBot; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
pretty_env_logger::init(); | ||
log::info!("Starting bot"); | ||
|
||
let bot = Bot::from_env(); | ||
|
||
let messages_store: types::Messages = Arc::new(RwLock::new(HashMap::new())); | ||
|
||
let handler = dptree::entry() | ||
.branch( | ||
Update::filter_message() | ||
.filter_command::<bot::Command>() | ||
.endpoint(bot::handle_commands), | ||
) | ||
.branch(Update::filter_message().endpoint(bot::handle_messages)); | ||
|
||
Dispatcher::builder(bot, handler) | ||
.dependencies(dptree::deps![messages_store]) | ||
.default_handler(|update| async move { | ||
log::warn!("Unhandled update: {:#?}", update); | ||
}) | ||
.error_handler(LoggingErrorHandler::with_custom_text( | ||
"An error occurred in the dispatcher", | ||
)) | ||
.enable_ctrlc_handler() | ||
.build() | ||
.dispatch() | ||
.await; | ||
TelitairoBot::dispatch().await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use std::{ | ||
collections::{HashMap, VecDeque}, | ||
sync::Arc, | ||
}; | ||
use teloxide::types::{ChatId, Message}; | ||
use tokio::sync::RwLock; | ||
|
||
pub const PERSONALITY: &str= "Eres un asistente andaluz con jerga informal y algo irónica. Ayudas a todo aquel que te necesite, no sin antes quejarte un poco, ya que eres algo vago."; | ||
pub const MEDIATE_QUERY: &str= "A partir de los siguientes mensajes, analiza una posible discusión y da la razón a alguno de los implicados, con una pequeña argumentación."; | ||
pub const BUFFER_CAPACITY: usize = 200; | ||
|
||
pub type Buffer = Arc<RwLock<HashMap<ChatId, VecDeque<Message>>>>; |