-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
171 lines (152 loc) ยท 6.15 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::{
fs::{self, OpenOptions},
io::{self, Write},
time::{SystemTime, UNIX_EPOCH},
};
use rand::Rng;
use chrono::Local;
use glob::glob;
use lazy_static::lazy_static;
use teloxide::{dispatching::dialogue::InMemStorage, prelude::*};
type MyDialogue = Dialogue<State, InMemStorage<State>>;
type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
#[derive(Clone, Default)]
pub enum State {
#[default]
Inbox,
}
// pub enum State {
// #[default]
// Start,
// ReceiveFullName,
// ReceiveAge {
// full_name: String,
// },
// ReceiveLocation {
// full_name: String,
// age: u8,
// },
// }
//
// specify the username when compiling the binary
lazy_static! {
static ref USERNAME: String = std::env::var("INBOXBOT_USERNAME").unwrap();
}
const SAME_FILE_THRESHOLD: u64 = 1800;
#[tokio::main]
async fn main() {
pretty_env_logger::init();
log::info!("Starting dialogue bot...");
let bot = Bot::from_env();
Dispatcher::builder(
bot,
Update::filter_message()
.enter_dialogue::<Message, InMemStorage<State>, State>()
.branch(dptree::case![State::Inbox].endpoint(inbox)),
// .branch(dptree::case![State::ReceiveFullName].endpoint(receive_full_name))
// .branch(dptree::case![State::ReceiveAge { full_name }].endpoint(receive_age))
// .branch(
// dptree::case![State::ReceiveLocation { full_name, age }].endpoint(receive_location),
// )
)
.dependencies(dptree::deps![InMemStorage::<State>::new()])
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}
async fn inbox(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
let username = msg.from().unwrap().username.clone();
if username.unwrap() != USERNAME.to_owned() {
bot.send_message(msg.chat.id, "You are not authorized to use this bot")
.await?;
return Ok(());
}
let mut found = false;
for entry in glob("*-tg.md").expect("Failed to read glob pattern") {
match entry {
Ok(path) => {
if was_file_modified_in_last_n_seconds(&path.to_string_lossy(), SAME_FILE_THRESHOLD)
{
write_message_to_file(msg.clone(), Some(path.to_string_lossy().to_string()))?;
found = true;
break;
}
}
Err(e) => println!("{:?}", e),
}
}
if !found {
write_message_to_file(msg.clone(), None)?;
}
bot.send_message(msg.chat.id, random_emoji()).await?;
dialogue.update(State::Inbox).await?;
Ok(())
}
fn write_message_to_file(msg: Message, path: Option<String>) -> io::Result<()> {
let filename = match path {
Some(p) => p,
None => {
let timestamp = Local::now().format("%Y%m%d%H%M%S").to_string();
format!("{}-tg.md", timestamp)
}
};
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(filename)?;
if let Some(entities) = msg.parse_entities() {
for entity in entities {
match entity.kind() {
teloxide::types::MessageEntityKind::Url => {
let link = format!("- []({})\n", entity.text());
file.write_all(link.as_bytes())?;
}
teloxide::types::MessageEntityKind::TextLink { url } => {
let link = format!("- [{}]({})\n", entity.text(), url);
file.write_all(link.as_bytes())?;
}
_ => {}
}
}
}
msg.text().map(|t| file.write_all(t.as_bytes()));
file.write_all(b"\n").unwrap();
Ok(())
}
fn was_file_modified_in_last_n_seconds(file_path: &str, n: u64) -> bool {
let metadata = fs::metadata(file_path).unwrap();
let modified_time = metadata
.modified()
.unwrap()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
let current_time = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
current_time - modified_time < n
}
fn random_emoji() -> &'static str {
let emojis = [
"๐", "๐", "๐", "๐", "๐บ๏ธ", "๐พ", "๐งญ", "๐๏ธ", "โฐ๏ธ", "๐", "๐ป", "๐๏ธ", "๐๏ธ", "๐๏ธ", "๐๏ธ", "๐๏ธ",
"๐๏ธ", "๐๏ธ", "๐๏ธ", "๐งฑ", "๐ชจ", "๐ชต", "๐", "๐๏ธ", "๐๏ธ", "๐ ", "๐ก", "๐ข", "๐ฃ", "๐ค", "๐ฅ", "๐ฆ",
"๐จ", "๐ฉ", "๐ช", "๐ซ", "๐ฌ", "๐ญ", "๐ฏ", "๐ฐ", "๐", "๐ผ", "๐ฝ", "โช", "๐", "๐", "๐",
"โฉ๏ธ", "๐", "โฒ", "โบ", "๐", "๐", "๐๏ธ", "๐", "๐
", "๐", "๐", "๐", "โจ๏ธ", "๐ ", "๐ก",
"๐ข", "๐", "๐ช", "๐", "๐", "๐", "๐
", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐",
"๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ป", "๐",
"๐", "๐", "๐๏ธ", "๐๏ธ", "๐ต", "๐ฆฝ", "๐ฆผ", "๐บ", "๐ถ", "๐ฑ", "๐ญ", "๐น", "๐ฐ", "๐ฆ", "๐ป",
"๐ผ", "๐จ", "๐ฏ", "๐ฆ", "๐ฎ", "๐ธ", "๐ต", "๐", "๐ง", "๐ฆ", "๐ค", "๐ฆ", "๐ฆ
", "๐ฆ", "๐ฆ",
"๐บ", "๐", "๐ด", "๐ฆ", "๐", "๐", "๐ฆ", "๐", "๐", "๐", "๐ฆ", "๐ฆ", "๐ท", "๐ธ", "๐ข",
"๐", "๐ฆ", "๐ฆ", "๐ฆ", "๐ฆ", "๐ฆ", "๐ฆ", "๐", "๐ฆ", "๐ฆ", "๐ณ", "๐", "๐ฌ", "๐", "๐ ",
"๐ก", "๐ฆ", "๐", "๐
", "๐", "๐ฆ", "๐ฆ", "๐", "๐ฆ", "๐ฆ", "๐ช", "๐ซ", "๐ฆ", "๐ฆ", "๐ฆฌ",
"๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ฆ", "๐", "๐ฉ", "๐ฆฎ", "๐", "๐", "๐", "๐ฆ",
"๐ฆ", "๐ฆ", "๐ฆข", "๐ฆฉ", "๐", "๐", "๐ฑ", "๐ฒ", "๐ณ", "๐ด", "๐ต", "๐พ", "๐ฟ", "โ๏ธ", "๐",
"๐", "๐", "๐", "๐ชด", "๐", "๐", "๐บ", "๐ป", "๐ผ", "๐ท", "๐น", "๐ฅ", "๐ธ", "๐", "๐",
"๐ฐ", "๐", "๐ผ", "๐ป", "๐", "๐",
];
let mut rng = rand::thread_rng();
let index = rng.gen_range(0..emojis.len());
emojis[index]
}