Compare commits
	
		
			3 Commits
		
	
	
		
			6a93251d81
			...
			ac039233ea
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| ac039233ea | |||
| 95bd2077ce | |||
| 59f91e72e9 | 
							
								
								
									
										17
									
								
								Dockerfile
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								Dockerfile
									
									
									
									
									
								
							| @@ -1,10 +1,21 @@ | ||||
| FROM rust:1.43 as builder | ||||
| FROM rust:1.47 as builder | ||||
|  | ||||
| WORKDIR /usr/src/sarcastifybot | ||||
|  | ||||
| # Avoid having to install/build all dependencies by copying | ||||
| # the Cargo files and making a dummy src/main.rs | ||||
| COPY Cargo.toml . | ||||
| COPY Cargo.lock . | ||||
| RUN mkdir -p src && echo "fn main() {}" > src/main.rs | ||||
| RUN cargo build --release | ||||
|  | ||||
| # We need to touch our real main.rs file or else docker will use | ||||
| # the cached one. | ||||
| COPY . . | ||||
| RUN cargo install --path . | ||||
| RUN touch src/main.rs | ||||
| RUN cargo build --release | ||||
|  | ||||
| FROM debian:buster | ||||
| RUN apt-get update && apt-get install -y openssl ca-certificates | ||||
| COPY --from=builder /usr/local/cargo/bin/sarcastifybot /usr/local/bin/sarcastifybot | ||||
| COPY --from=builder /usr/src/sarcastifybot/target/release/sarcastifybot /usr/local/bin/sarcastifybot | ||||
| CMD ["sarcastifybot"] | ||||
|   | ||||
							
								
								
									
										23
									
								
								deployment.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								deployment.yml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| apiVersion: apps/v1 | ||||
| kind: Deployment | ||||
| metadata: | ||||
|   name: sarcastifybot | ||||
|   labels: | ||||
|     app: sarcastifybot | ||||
| spec: | ||||
|   replicas: 1 | ||||
|   selector: | ||||
|     matchLabels: | ||||
|       app: sarcastifybot | ||||
|   template: | ||||
|     metadata: | ||||
|       labels: | ||||
|         app: sarcastifybot | ||||
|     spec: | ||||
|       containers: | ||||
|       - name: main | ||||
|         image: harbor.blacknova.io/nvls/sarcastifybot:latest | ||||
|         imagePullPolicy: Always | ||||
|         env: | ||||
|         - name: TELEGRAM_BOT_TOKEN | ||||
|           value: "924754197:AAGvpdqUY-2pOy1wnQIW2zeS-oA7-lxwfqU" | ||||
							
								
								
									
										137
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										137
									
								
								src/main.rs
									
									
									
									
									
								
							| @@ -4,10 +4,89 @@ use dotenv::dotenv; | ||||
| use futures::StreamExt; | ||||
| use telegram_bot::*; | ||||
|  | ||||
| /// Returns two versions of AlTeRnAtInG case equivalents of the given message | ||||
| /// The first result contains the a version beginning with an uppercase letter, | ||||
| /// the second result begins with a lowercase letter. | ||||
| /// | ||||
| /// # Arguments | ||||
| /// | ||||
| /// * `message` - A string slice that contains the message to convert | ||||
| fn generate_alternate_case(message: &str) -> (String, String) { | ||||
|     let mut option1 = String::new(); | ||||
|     let mut option2 = String::new(); | ||||
|  | ||||
|     let mut stream = message.chars().enumerate(); | ||||
|     while let Some((idx, letter)) = stream.next() { | ||||
|         if idx % 2 == 0 { | ||||
|             option1.push(letter.to_uppercase().next().unwrap()); | ||||
|             option2.push(letter.to_lowercase().next().unwrap()); | ||||
|         } else { | ||||
|             option1.push(letter.to_lowercase().next().unwrap()); | ||||
|             option2.push(letter.to_uppercase().next().unwrap()); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     (option1, option2) | ||||
| } | ||||
|  | ||||
| /// Generate a spaced-out version of the message. | ||||
| /// | ||||
| /// # Arguments | ||||
| /// | ||||
| /// * `message` - A string slice that contains the message to convert | ||||
| fn generate_spaced(message: &str) -> String | ||||
| { | ||||
|     let mut result = String::new(); | ||||
|  | ||||
|     let mut stream = message.chars().enumerate(); | ||||
|     while let Some((_, letter)) = stream.next() { | ||||
|         result.push(letter); | ||||
|         result.push(' '); | ||||
|     } | ||||
|  | ||||
|     // Trim trailing space | ||||
|     result.pop(); | ||||
|     result | ||||
| } | ||||
|  | ||||
|  | ||||
| fn generate_responses(message: &str) -> Vec<InlineQueryResult> { | ||||
|     if message == "" { | ||||
|         return vec![]; | ||||
|     } | ||||
|  | ||||
|     let (option1, option2) = generate_alternate_case(message); | ||||
|     let option3 = generate_spaced(message); | ||||
|  | ||||
|     vec![ | ||||
|         InlineQueryResultArticle::new( | ||||
|             "altcase_1", option1.clone(), InputTextMessageContent { | ||||
|                 message_text: option1, | ||||
|                 parse_mode: None, | ||||
|                 disable_web_page_preview: false, | ||||
|             } | ||||
|         ).into(), | ||||
|         InlineQueryResultArticle::new( | ||||
|             "altcase_2", option2.clone(), InputTextMessageContent { | ||||
|                 message_text: option2, | ||||
|                 parse_mode: None, | ||||
|                 disable_web_page_preview: false, | ||||
|             } | ||||
|         ).into(), | ||||
|         InlineQueryResultArticle::new( | ||||
|             "spaced", option3.clone(), InputTextMessageContent { | ||||
|                 message_text: option3, | ||||
|                 parse_mode: None, | ||||
|                 disable_web_page_preview: false, | ||||
|             } | ||||
|         ).into(), | ||||
|     ] | ||||
| } | ||||
|  | ||||
| #[tokio::main] | ||||
| async fn main() { | ||||
|     dotenv().ok(); | ||||
|      | ||||
|  | ||||
|     let token = env::var("TELEGRAM_BOT_TOKEN").expect("TELEGRAM_BOT_TOKEN not set"); | ||||
|     let api = Api::new(token); | ||||
|  | ||||
| @@ -20,42 +99,26 @@ async fn main() { | ||||
|         }; | ||||
|  | ||||
|         if let UpdateKind::InlineQuery(query) = update.kind { | ||||
|             let mut option1 = String::new(); | ||||
|             let mut option2 = String::new(); | ||||
|             if query.query == "" { | ||||
|                 api.send(query.answer(vec![])).await.ok(); | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             { | ||||
|                 let mut stream = query.query.chars().enumerate(); | ||||
|                 while let Some((idx, letter)) = stream.next() { | ||||
|                     if idx % 2 == 0 { | ||||
|                         option1.push(letter.to_uppercase().next().unwrap()); | ||||
|                         option2.push(letter.to_lowercase().next().unwrap()); | ||||
|                     } else { | ||||
|                         option1.push(letter.to_lowercase().next().unwrap()); | ||||
|                         option2.push(letter.to_uppercase().next().unwrap()); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             api.send(query.answer(vec![ | ||||
|                 InlineQueryResultArticle::new( | ||||
|                     "1", option1.clone(), InputTextMessageContent { | ||||
|                         message_text: option1, | ||||
|                         parse_mode: None, | ||||
|                         disable_web_page_preview: false, | ||||
|                     } | ||||
|                 ).into(), | ||||
|                 InlineQueryResultArticle::new( | ||||
|                     "2", option2.clone(), InputTextMessageContent { | ||||
|                         message_text: option2, | ||||
|                         parse_mode: None, | ||||
|                         disable_web_page_preview: false, | ||||
|                     } | ||||
|                 ).into(), | ||||
|             ])).await.ok(); | ||||
|             let responses = generate_responses(&query.query); | ||||
|             api.send(query.answer(responses)).await.ok(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| #[cfg(test)] | ||||
| mod tests { | ||||
|     use super::*; | ||||
|  | ||||
|     #[test] | ||||
|     fn test_alternating() { | ||||
|         let (option1, option2) = generate_alternate_case("This is a test"); | ||||
|         assert_eq!(option1, "ThIs iS A TeSt"); | ||||
|         assert_eq!(option2, "tHiS Is a tEsT"); | ||||
|     } | ||||
|  | ||||
|     #[test] | ||||
|     fn test_spaced() { | ||||
|         let option = generate_spaced("This is a test"); | ||||
|         assert_eq!(option, "T h i s   i s   a   t e s t") | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user