Skip to content

Commit

Permalink
Add emoji counter
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Aug 26, 2024
1 parent b70f656 commit 42c8650
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions crates/chatbot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,30 @@ pub async fn gen_random_number() -> usize {

/// A chatbot that responds to inputs.
pub struct Chatbot {
emoji: String,
emojis: Vec<String>,
emoji_counter: usize,
}

impl Chatbot {
/// Creates a new chatbot that uses the provided emoji in its responses.
pub fn new(emoji: String) -> Self {
Chatbot { emoji }
pub fn new(emojis: Vec<String>) -> Self {
Chatbot {
emojis,
emoji_counter: 0,
}
}

/// Generates a list of possible responses given the current chat.
///
/// Warning: may take a few seconds!
pub async fn query_chat(&self, messages: &[String]) -> Vec<String> {
pub async fn query_chat(&mut self, messages: &[String]) -> Vec<String> {
std::thread::sleep(Duration::from_secs(2));
let most_recent = messages.last().unwrap();
let emoji = &self.emojis[self.emoji_counter];
self.emoji_counter = (self.emoji_counter + 1) % self.emojis.len();
vec![
format!(
"\"{most_recent}\"? And how does that make you feel? {}",
self.emoji
),
format!("\"{most_recent}\"! Interesting! Go on... {}", self.emoji),
format!("\"{most_recent}\"? And how does that make you feel? {emoji}",),
format!("\"{most_recent}\"! Interesting! Go on... {emoji}"),
]
}
}

0 comments on commit 42c8650

Please sign in to comment.