Diff
Logged in as anonymous

Differences From Artifact [caeb3fb50c]:

To Artifact [540899dcce]:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use crate::{
	command,
	sql::Db,
};

use std::{
	borrow::Cow,
	collections::{
		BTreeMap,
		HashSet,
	},
	num::TryFromIntError,
	sync::{
		Arc,
		Mutex
	},
};

use anyhow::{











<







1
2
3
4
5
6
7
8
9
10
11

12
13
14
15
16
17
18
use crate::{
	command,
	sql::Db,
};

use std::{
	borrow::Cow,
	collections::{
		BTreeMap,
		HashSet,
	},

	sync::{
		Arc,
		Mutex
	},
};

use anyhow::{
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
		ParseMode,
		SendMessage,
		Update,
		UpdateType,
		UserPeerId,
	},
};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum RssError {
	// #[error(transparent)]
	// Tg(#[from] TgError),
	#[error(transparent)]
	Int(#[from] TryFromIntError),
}

#[derive(Clone)]
pub struct Core {
	owner_chat: ChatPeerId,
	// max_delay: u16,
	pub tg: Client,
	pub me: Bot,







<
<
<
<
<
<
<
<
<







34
35
36
37
38
39
40









41
42
43
44
45
46
47
		ParseMode,
		SendMessage,
		Update,
		UpdateType,
		UserPeerId,
	},
};










#[derive(Clone)]
pub struct Core {
	owner_chat: ChatPeerId,
	// max_delay: u16,
	pub tg: Client,
	pub me: Bot,
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
				};
				task::sleep(delay).await;
			}
		});
		Ok(core)
	}

	/*
	pub async fn stream(&self) -> Result<()> {
		let mut offset: i64 = 0;
		let mut params = GetUpdatesParams {
			offset: None,
			limit: Some(100),
			timeout: Some(300),
			allowed_updates: Some(vec![AllowedUpdate::Message]),
		};
		loop {
			let updates = self.tg.get_updates(&params).await?.result;
			if updates.is_empty() {
				offset = 0;
				params.offset = None;
				continue;
			}
			for update in updates {
				if i64::from(update.update_id) >= offset {
					offset = i64::from(update.update_id) + 1;
					params.offset = Some(offset);
				}
				if let UpdateContent::Message(msg) = update.content {
					if let Some(text) = msg.text {
						if let Some(entities) = msg.entities {
							let chars: Vec<u16> = text.encode_utf16().collect();
							for entity in entities {
								if entity.type_field == MessageEntityType::BotCommand && entity.offset != 0 {
									bail!("commands should be at message start");
								};
								let cmd = String::from_utf16_lossy(&chars[entity.offset as usize..entity.length as usize]);
								let words: Vec<&str> = text.split_whitespace().collect();
								let res = match cmd.as_ref() {
									"/check" | "/clean" | "/enable" | "/delete" | "/disable" => command::command(self, msg.chat.id, words).await,
									"/start" => command::start(self, msg.chat.id).await,
									"/list" => command::list(self, msg.chat.id).await,
									"/add" | "/update" => command::update(self, msg.chat.id, words).await,
									any => Err(anyhow!("Unknown command: {any}")),
								};
								if let Err(err) = res {
									if let Err(err2) = self.send(format!("\\#error\n```\n{err:?}\n```"),
										Some(msg.chat.id),
										Some(ParseMode::MarkdownV2)
									).await{
										dbg!(err2);
									};
								}
							};
						};
					};
				};
			}
		}
	}
	*/

	pub async fn send <S>(&self, msg: S, target: Option<ChatPeerId>, mode: Option<ParseMode>) -> Result<Message>
	where S: Into<String> {
		let msg = msg.into();

		let mode = mode.unwrap_or(ParseMode::Html);
		let target = target.unwrap_or(self.owner_chat);
		Ok(self.tg.execute(







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







86
87
88
89
90
91
92























































93
94
95
96
97
98
99
				};
				task::sleep(delay).await;
			}
		});
		Ok(core)
	}
























































	pub async fn send <S>(&self, msg: S, target: Option<ChatPeerId>, mode: Option<ParseMode>) -> Result<Message>
	where S: Into<String> {
		let msg = msg.into();

		let mode = mode.unwrap_or(ParseMode::Html);
		let target = target.unwrap_or(self.owner_chat);
		Ok(self.tg.execute(
307
308
309
310
311
312
313

314
315
316
317
318
319
320
321
322
323
324
325

impl UpdateHandler for Core {
	async fn handle (&self, update: Update) {
		if let UpdateType::Message(msg) = update.update_type {
			if let Ok(cmd) = Command::try_from(msg) {
				let msg = cmd.get_message();
				let words = cmd.get_args();

				let res = match cmd.get_name() {
					"/check" | "/clean" | "/enable" | "/delete" | "/disable" => command::command(self, msg, words).await,
					"/start" => command::start(self, msg).await,
					"/list" => command::list(self, msg).await,
					"/add" | "/update" => command::update(self, msg, words).await,
					any => Err(anyhow!("Unknown command: {any}")),
				};
				if let Err(err) = res {
					if let Err(err2) = self.send(format!("\\#error\n```\n{err:?}\n```"),
						Some(msg.chat.get_id()),
						Some(ParseMode::MarkdownV2)
					).await{







>
|
|


|







242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261

impl UpdateHandler for Core {
	async fn handle (&self, update: Update) {
		if let UpdateType::Message(msg) = update.update_type {
			if let Ok(cmd) = Command::try_from(msg) {
				let msg = cmd.get_message();
				let words = cmd.get_args();
				let command = cmd.get_name();
				let res = match command {
					"/check" | "/clean" | "/enable" | "/delete" | "/disable" => command::command(self, command, msg, words).await,
					"/start" => command::start(self, msg).await,
					"/list" => command::list(self, msg).await,
					"/add" | "/update" => command::update(self, command, msg, words).await,
					any => Err(anyhow!("Unknown command: {any}")),
				};
				if let Err(err) = res {
					if let Err(err2) = self.send(format!("\\#error\n```\n{err:?}\n```"),
						Some(msg.chat.get_id()),
						Some(ParseMode::MarkdownV2)
					).await{