Skip to content

Commit

Permalink
Refactor bot instantiation. Main cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Bacon committed Jun 12, 2024
1 parent e3bca00 commit 936d155
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub async fn handle_messages(messages: types::Messages, msg: Message) -> Respons
let mut messages_lock = messages.write().await;
match messages_lock.get_mut(&msg.chat.id) {
Some(buffer) => {
if buffer.len() == types::STORE_CAPACITY {
if buffer.len() == types::BUFFER_CAPACITY {
buffer.pop_front();
}
buffer.push_back(msg.clone());
Expand Down
44 changes: 33 additions & 11 deletions src/lib.rs
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
}
}
30 changes: 2 additions & 28 deletions src/main.rs
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;
}
12 changes: 12 additions & 0 deletions src/types.rs
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>>>>;

0 comments on commit 936d155

Please sign in to comment.