Diff
Logged in as anonymous

Differences From Artifact [bf2d049c4a]:

To Artifact [5f32e40450]:


222
223
224
225
226
227
228
229

230
231
232
233
234
235
236

237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
222
223
224
225
226
227
228

229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256

257
258
259
260
261

262
263
264
265
266

267
268
269
270
271
272
273







-
+







+



















-





-





-







}

fn my_prudence() -> Prudence {
	Prudence::default().with_read_timeout(Duration::from_secs(60)).with_banner_delay(Duration::from_secs(1))
}

pub struct TelegramTransport {
	tg: teloxide::adaptors::DefaultParseMode<Bot>,
	tg: teloxide::adaptors::DefaultParseMode<teloxide::adaptors::Throttle<Bot>>,
	recipients: HashMap<String, ChatId>,
}

impl TelegramTransport {
	pub fn new(settings: config::Config) -> TelegramTransport {
		let tg = Bot::new(settings.get_string("api_key")
			.expect("[smtp2tg.toml] missing \"api_key\" parameter.\n"))
			.throttle(teloxide::adaptors::throttle::Limits::default())
			.parse_mode(MarkdownV2);
		let recipients: HashMap<String, ChatId> = settings.get_table("recipients")
			.expect("[smtp2tg.toml] missing table \"recipients\".\n")
			.into_iter().map(|(a, b)| (a, ChatId (b.into_int()
				.expect("[smtp2tg.toml] \"recipient\" table values should be integers.\n")
				))).collect();
		if !recipients.contains_key("_") {
			eprintln!("[smtp2tg.toml] \"recipient\" table misses \"default_recipient\".\n");
			panic!("no default recipient");
		}

		TelegramTransport {
			tg,
			recipients,
		}
	}

	pub async fn debug<'b, S>(&self, msg: S) -> Result<teloxide::types::Message>
	where S: Into<String> {
		task::sleep(Duration::from_secs(5)).await;
		Ok(self.tg.send_message(*self.recipients.get("_").unwrap(), msg).await?)
	}

	pub async fn send<'b, S>(&self, to: &ChatId, msg: S) -> Result<teloxide::types::Message>
	where S: Into<String> {
		task::sleep(Duration::from_secs(5)).await;
		Ok(self.tg.send_message(*to, msg).await?)
	}

	pub async fn sendgroup<M>(&self, to: &ChatId, media: M) -> Result<Vec<teloxide::types::Message>>
	where M: IntoIterator<Item = teloxide::types::InputMedia> {
		task::sleep(Duration::from_secs(5)).await;
		Ok(self.tg.send_media_group(*to, media).await?)
	}
}

#[async_std::main]
async fn main() {
	let settings: config::Config = config::Config::builder()