Lines of
src/main.rs
from check-in 61df933942
that are changed by the sequence of edits moving toward
check-in 0191d490fe:
1: use config;
2:
3: use tokio;
4: use rss;
5: use chrono::DateTime;
6:
7: use regex::Regex;
8:
61df933942 2020-11-18 9: //use tbot;
61df933942 2020-11-18 10: //use tbot::prelude::*;
61df933942 2020-11-18 11:
61df933942 2020-11-18 12: use futures::StreamExt;
61df933942 2020-11-18 13: use futures::TryStreamExt;
14: use telegram_bot::*;
15:
16: use sqlx::postgres::PgPoolOptions;
17: use sqlx::Row;
18:
19: type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
20:
21: struct Core {
22: owner: i64,
23: owner_chat: UserId,
24: tg: telegram_bot::Api,
25: my: User,
26: pool: sqlx::Pool<sqlx::Postgres>,
27: }
28:
29: impl Core {
30: async fn new(settings: config::Config) -> Result<Core> {
31: let owner = settings.get_int("owner")?;
61df933942 2020-11-18 32: let tg = Api::new(settings.get_str("api_key")?);
33: let core = Core {
34: owner: owner,
35: my: tg.send(telegram_bot::GetMe).await?,
36: tg: tg,
37: owner_chat: UserId::new(owner),
38: pool: PgPoolOptions::new().max_connections(5).connect(&settings.get_str("pg")?).await?,
39: };
40: tokio::spawn(async move {
61df933942 2020-11-18 41: if let Err(err) = &core.autofetch().await {
42: eprintln!("connection error: {}", err);
43: }
44: });
61df933942 2020-11-18 45:
61df933942 2020-11-18 46: let tg = Api::new(settings.get_str("api_key")?);
61df933942 2020-11-18 47: Ok(Core {
61df933942 2020-11-18 48: owner: owner,
61df933942 2020-11-18 49: my: tg.send(telegram_bot::GetMe).await?,
61df933942 2020-11-18 50: tg: tg,
61df933942 2020-11-18 51: owner_chat: UserId::new(owner),
61df933942 2020-11-18 52: pool: PgPoolOptions::new().max_connections(5).connect(&settings.get_str("pg")?).await?,
61df933942 2020-11-18 53: })
54: }
55:
56: fn stream(&self) -> telegram_bot::UpdatesStream {
57: self.tg.stream()
58: }
59:
60: fn debug(&self, msg: &str) -> Result<()> {
61: self.tg.spawn(SendMessage::new(self.owner_chat, msg));
62: Ok(())
63: }
64:
61df933942 2020-11-18 65: async fn check(&self, id: i32, real: Option<bool>) -> Result<()> {
61df933942 2020-11-18 66: match sqlx::query("select channel_id, url, last_fetch, iv_hash from rsstg_source where source_id = $1")
61df933942 2020-11-18 67: .bind(id)
68: .fetch_one(&self.pool).await {
69: Ok(row) => {
70: let channel_id: i64 = row.try_get("channel_id")?;
71: let destination = match real {
72: Some(true) => UserId::new(channel_id),
61df933942 2020-11-18 73: Some(false) | None => self.owner_chat,
74: };
75: let url: &str = row.try_get("url")?;
76: let last_fetch: Option<DateTime<chrono::FixedOffset>> = row.try_get("last_fetch")?;
77: let mut this_fetch: Option<DateTime<chrono::FixedOffset>> = None;
78: let iv_hash: Option<&str> = row.try_get("iv_hash")?;
79: match rss::Channel::from_url(url) {
80: Ok(feed) => {
81: self.debug(&format!("# title:{:?} ttl:{:?} hours:{:?} days:{:?}", feed.title(), feed.ttl(), feed.skip_hours(), feed.skip_days()))?;
82: for item in feed.items() {
61df933942 2020-11-18 83: let date = DateTime::parse_from_rfc2822(item.pub_date().unwrap()).unwrap();
61df933942 2020-11-18 84: let url = item.link().unwrap().to_string();
61df933942 2020-11-18 85: if last_fetch == None || date > last_fetch.unwrap() {
61df933942 2020-11-18 86: if this_fetch == None || date > this_fetch.unwrap() {
61df933942 2020-11-18 87: this_fetch = Some(date);
61df933942 2020-11-18 88: }
61df933942 2020-11-18 89: match self.tg.send( match iv_hash {
61df933942 2020-11-18 90: Some(x) => SendMessage::new(destination, format!("<a href=\"https://t.me/iv?url={}&rhash={}\"> </a>{0}", url, x)),
61df933942 2020-11-18 91: None => SendMessage::new(destination, format!("{}", url)),
61df933942 2020-11-18 92: }.parse_mode(types::ParseMode::Html)).await {
61df933942 2020-11-18 93: Ok(_) => {
61df933942 2020-11-18 94: match sqlx::query("insert into rsstg_post (source_id, posted, url) values ($1, $2, $3);")
61df933942 2020-11-18 95: .bind(id)
61df933942 2020-11-18 96: .bind(date)
61df933942 2020-11-18 97: .bind(url)
61df933942 2020-11-18 98: .execute(&self.pool).await {
61df933942 2020-11-18 99: Ok(_) => {},
61df933942 2020-11-18 100: Err(err) => {
61df933942 2020-11-18 101: self.debug(&err.to_string())?;
61df933942 2020-11-18 102: },
61df933942 2020-11-18 103: };
61df933942 2020-11-18 104: },
61df933942 2020-11-18 105: Err(err) => {
61df933942 2020-11-18 106: self.debug(&err.to_string())?;
61df933942 2020-11-18 107: },
61df933942 2020-11-18 108: }
61df933942 2020-11-18 109: };
61df933942 2020-11-18 110: tokio::time::delay_for(std::time::Duration::new(4, 0)).await;
61df933942 2020-11-18 111: };
61df933942 2020-11-18 112: // update last_fetch
61df933942 2020-11-18 113: if this_fetch != None && (last_fetch == None || this_fetch.unwrap() > last_fetch.unwrap()) {
61df933942 2020-11-18 114: match sqlx::query("update rsstg_source set last_fetch = $1 where source_id = $2;")
115: .bind(this_fetch.unwrap())
116: .bind(id)
117: .execute(&self.pool).await {
118: Ok(_) => {},
119: Err(err) => {
120: self.debug(&err.to_string())?;
121: },
122: };
123: }
124: },
125: Err(err) => {
126: self.debug(&err.to_string())?;
127: },
128: };
129: match sqlx::query("update rsstg_source set last_scrape = now() where source_id = $1;")
130: .bind(id)
131: .execute(&self.pool).await {
132: Ok(_) => {},
133: Err(err) => {
134: self.debug(&err.to_string())?;
135: },
136: };
137: },
138: Err(err) => {
139: self.debug(&err.to_string())?;
140: },
141: };
142: Ok(())
143: }
144:
145: async fn clean(&self, source_id: i32) -> Result<()> {
146: for query in vec!["delete from rsstg_post where source_id = $1;", "update rsstg_source set last_fetch = NULL where source_id = $1;"] {
147: match sqlx::query(query)
148: .bind(source_id)
149: .execute(&self.pool).await {
150: Ok(_) => {},
151: Err(err) => {
152: self.debug(&err.to_string())?;
153: },
154: }
155: }
156: Ok(())
157: }
158:
159: async fn enable(&self, user: UserId, channel: &str) -> Result<()> {
160: match sqlx::query("update rsstg_source set enabled = true from rsstg_channel where rsstg_channel.channel_id = rsstg_source.channel_id and rsstg_channel.username = $1 and owner = $2")
161: .bind(channel)
162: .bind(i64::from(user))
163: .execute(&self.pool).await {
164: Ok(_) => {},
165: Err(err) => {
166: self.debug(&err.to_string())?;
167: },
168: }
169: Ok(())
170: }
171:
172: async fn disable(&self, user: UserId, channel: &str) -> Result<()> {
173: match sqlx::query("update rsstg_source set enabled = false from rsstg_channel where rsstg_channel.channel_id = rsstg_source.channel_id and rsstg_channel.username = $1 and owner = $2")
174: .bind(channel)
175: .bind(i64::from(user))
176: .execute(&self.pool).await {
177: Ok(_) => {},
178: Err(err) => {
179: self.debug(&err.to_string())?;
180: },
181: }
182: Ok(())
183: }
184:
185: async fn autofetch(&self) -> Result<()> {
186: let mut delay = chrono::Duration::minutes(5);
61df933942 2020-11-18 187: let mut source_id;
188: let mut next_fetch: DateTime<chrono::Local>;
189: let mut now;
190: loop {
61df933942 2020-11-18 191: let mut rows = sqlx::query("select source_id, next_fetch from rsstg_order limit 1;")
192: .fetch(&self.pool);
193: while let Some(row) = rows.try_next().await.unwrap() {
194: now = chrono::Local::now();
61df933942 2020-11-18 195: source_id = row.try_get("source_id")?;
196: next_fetch = row.try_get("next_fetch")?;
197: if next_fetch < now {
61df933942 2020-11-18 198: &self.check(source_id, Some(true)).await?;
199: } else {
61df933942 2020-11-18 200: delay = next_fetch - now;
61df933942 2020-11-18 201: if delay > chrono::Duration::minutes(5) {
61df933942 2020-11-18 202: delay = chrono::Duration::minutes(5);
203: }
204: }
205: };
206: tokio::time::delay_for(delay.to_std()?).await;
207: }
208: //Ok(())
209: }
210:
211: }
212:
61df933942 2020-11-18 213: #[tokio::main]
214: async fn main() -> Result<()> {
215: let mut settings = config::Config::default();
216: settings.merge(config::File::with_name("rsstg"))?;
217:
61df933942 2020-11-18 218: let re_username = Regex::new(r"^@[a-z][a-z0-9_]+$")?;
61df933942 2020-11-18 219: let re_link = Regex::new(r"^https?://[a-z.0-9]+/[-_a-z.0-9/]+$")?;
220: let re_iv_hash = Regex::new(r"^[a-f0-9]{14}$")?;
221:
61df933942 2020-11-18 222: /*
61df933942 2020-11-18 223: tokio::spawn(async move {
61df933942 2020-11-18 224: if let Err(e) = connection.await {
61df933942 2020-11-18 225: eprintln!("connection error: {}", e);
61df933942 2020-11-18 226: }
61df933942 2020-11-18 227: }); */
61df933942 2020-11-18 228:
229: let core = Core::new(settings).await?;
61df933942 2020-11-18 230:
61df933942 2020-11-18 231: /*
61df933942 2020-11-18 232: let mut bot = tbot::Bot::new(settings.get_str("api_key")?).event_loop();
61df933942 2020-11-18 233:
61df933942 2020-11-18 234: bot.command("start", //"Start working.",
61df933942 2020-11-18 235: |context| async move {
61df933942 2020-11-18 236: context.send_message_in_reply("Not in service yet. Try later.").call().await.unwrap();
61df933942 2020-11-18 237: },
61df933942 2020-11-18 238: );
61df933942 2020-11-18 239:
61df933942 2020-11-18 240: bot.command("list", //"List channels.",
61df933942 2020-11-18 241: |context| async move {
61df933942 2020-11-18 242: dbg!(&context.chat);
61df933942 2020-11-18 243: let mut res = "Channels:\n".to_owned();
61df933942 2020-11-18 244: let mut rows = sqlx::query("select username, channel_id, url, iv_hash from rsstg_source left join rsstg_channel using (channel_id) where owner = $1")
61df933942 2020-11-18 245: .bind(context.chat.id.0)
61df933942 2020-11-18 246: .fetch(&pool);
61df933942 2020-11-18 247: while let Some(row) = rows.try_next().await.unwrap() {
61df933942 2020-11-18 248: let username: &str = row.try_get("username").unwrap();
61df933942 2020-11-18 249: let channel_id: &str = row.try_get("channel_id").unwrap();
61df933942 2020-11-18 250: let url: &str = row.try_get("url").unwrap();
61df933942 2020-11-18 251: let iv_hash: &str = row.try_get("iv_hash").unwrap();
61df933942 2020-11-18 252: res.push_str(&format!("`{}`: `{}` iv:`{}`\n", username, url, iv_hash));
61df933942 2020-11-18 253: //match row.get(3) as str {
61df933942 2020-11-18 254: //Some(x) => x,
61df933942 2020-11-18 255: //_ => "None"
61df933942 2020-11-18 256: //}));
61df933942 2020-11-18 257: }
61df933942 2020-11-18 258: context.send_message_in_reply(&res).call().await.unwrap();
61df933942 2020-11-18 259: },
61df933942 2020-11-18 260: );
61df933942 2020-11-18 261: */
262:
263: let mut stream = core.stream();
264:
265: while let Some(update) = stream.next().await {
266: let update = update?;
267: match update.kind {
268: UpdateKind::Message(message) => {
269: let mut reply: Vec<String> = vec![];
270: match message.kind {
271: MessageKind::Text { ref data, .. } => {
272: let mut words = data.split_whitespace();
273: let cmd = words.next().unwrap();
274: match cmd {
275:
276: // start
277:
278: "/start" => {
279: reply.push("Not in service yet. Try later.".to_string());
280: },
281:
282: // list
283:
284: "/list" => {
285: reply.push("Channels:".to_string());
286: let mut rows = sqlx::query("select username, enabled, url, iv_hash from rsstg_source left join rsstg_channel using (channel_id) where owner = $1")
287: .bind(i64::from(message.from.id))
288: .fetch(&core.pool);
289: while let Some(row) = rows.try_next().await? {
290: let username: &str = row.try_get("username")?;
291: let enabled: bool = row.try_get("enabled")?;
292: let url: &str = row.try_get("url")?;
61df933942 2020-11-18 293: let iv_hash: &str = row.try_get("iv_hash")?;
61df933942 2020-11-18 294: reply.push(format!("\n\\*️⃣ `{}` {}\n🔗 `{}`\nIV `{}`", username,
295: match enabled {
296: true => "🔄 enabled",
297: false => "⛔ disabled",
61df933942 2020-11-18 298: }, url, iv_hash));
61df933942 2020-11-18 299: //match row.get(3) as str {
61df933942 2020-11-18 300: //Some(x) => x,
61df933942 2020-11-18 301: //_ => "None"
61df933942 2020-11-18 302: //}));
303: }
304: },
305:
306: // add
307:
308: "/add" => {
309: let (channel, url, iv_hash) = (words.next().unwrap(), words.next().unwrap(), words.next());
310: let ok_link = re_link.is_match(&url);
311: let ok_hash = match iv_hash {
312: Some(hash) => re_iv_hash.is_match(&hash),
313: None => true,
314: };
315: if ! ok_link {
61df933942 2020-11-18 316: reply.push("Link should be link to atom/rss feed, something like \"https://domain/path\".".to_string());
317: core.debug(&format!("Url: {:?}", &url))?;
318: }
319: if ! ok_hash {
320: reply.push("IV hash should be 14 hex digits.".to_string());
321: core.debug(&format!("IV: {:?}", &iv_hash))?;
322: }
323: if ok_link && ok_hash {
324: let chan: Option<i64> = match sqlx::query("select channel_id from rsstg_channel where username = $1")
325: .bind(channel)
326: .fetch_one(&core.pool).await {
327: Ok(chan) => Some(chan.try_get("channel_id")?),
328: Err(sqlx::Error::RowNotFound) => {
329: reply.push("Sorry, I don't know about that channel. Please, add a channel with /addchan.".to_string());
330: None
331: },
332: Err(err) => {
333: reply.push("Sorry, unknown error\\.".to_string());
334: core.debug(&format!("Sorry, unknown error: {:#?}\n", err))?;
335: None
336: },
337: };
338: match chan {
339: Some(chan) => {
340: match sqlx::query("insert into rsstg_source (channel_id, url, iv_hash, owner) values ($1, $2, $3, $4) on conflict (channel_id, owner) do update set url = excluded.url, iv_hash = excluded.iv_hash;")
341: .bind(chan)
342: .bind(url)
343: .bind(iv_hash)
344: .bind(i64::from(message.from.id))
345: .execute(&core.pool).await {
346: Ok(_) => reply.push("Channel added\\.".to_string()),
347: Err(sqlx::Error::Database(err)) => {
348: match err.downcast::<sqlx::postgres::PgDatabaseError>().routine() {
349: Some("_bt_check_unique", ) => {
350: reply.push("Duplicate key\\.".to_string());
351: },
352: Some(_) => {
353: reply.push("Database error\\.".to_string());
354: },
355: None => {
356: reply.push("No database error extracted\\.".to_string());
357: },
358: };
359: },
360: Err(err) => {
361: reply.push("Sorry, unknown error\\.".to_string());
362: core.debug(&format!("Sorry, unknown error: {:#?}\n", err))?;
363: },
364: };
365: },
366: None => {},
367: };
368: };
369: },
370:
371: // addchan
372:
373: "/addchan" => {
374: let channel = words.next().unwrap();
375: if ! re_username.is_match(&channel) {
61df933942 2020-11-18 376: reply.push("Usernames should be something like \"@\\[a-z]\\[a-z0-9_]+\", aren't they?".to_string());
377: } else {
378: let chan: Option<i64> = match sqlx::query("select channel_id from rsstg_channel where username = $1")
379: .bind(channel)
380: .fetch_one(&core.pool).await {
381: Ok(chan) => Some(chan.try_get("channel_id")?),
382: Err(sqlx::Error::RowNotFound) => None,
383: Err(err) => {
384: reply.push("Sorry, unknown error\\.".to_string());
385: core.debug(&format!("Sorry, unknown error: {:#?}\n", err))?;
386: None
387: },
388: };
389: match chan {
390: Some(chan) => {
391: let new_chan = core.tg.send(telegram_bot::GetChat::new(telegram_bot::types::ChatId::new(chan))).await?;
392: if i64::from(new_chan.id()) == chan {
393: reply.push("I already know that channel\\.".to_string());
394: } else {
395: reply.push("Hmm, channel has changed… I'll fix it later\\.".to_string());
396: };
397: },
398: None => {
399: match core.tg.send(telegram_bot::GetChatAdministrators::new(telegram_bot::types::ChatRef::ChannelUsername(channel.to_string()))).await {
400: Ok(chan_adm) => {
401: let (mut me, mut user) = (false, false);
402: for admin in &chan_adm {
403: if admin.user.id == core.my.id {
404: me = true;
405: };
406: if admin.user.id == message.from.id {
407: user = true;
408: };
409: };
410: if ! me { reply.push("I need to be admin on that channel\\.".to_string()); };
411: if ! user { reply.push("You should be admin on that channel\\.".to_string()); };
412: if me && user {
413: let chan_id = core.tg.send(telegram_bot::GetChat::new(telegram_bot::types::ChatRef::ChannelUsername(channel.to_string()))).await?;
414: sqlx::query("insert into rsstg_channel (channel_id, username) values ($1, $2);")
415: .bind(i64::from(chan_id.id()))
416: .bind(channel)
417: .execute(&core.pool).await?;
418: reply.push("Good, I know that channel now\\.\n".to_string());
419: };
420: },
421: Err(_) => {
422: reply.push("Sorry, I have no access to that chat\\.".to_string());
423: },
424: };
425: },
426: };
427: };
428: },
429:
430: // check
431:
432: "/check" => {
61df933942 2020-11-18 433: if core.owner != i64::from(message.from.id) {
61df933942 2020-11-18 434: reply.push("Reserved for testing\\.".to_string());
435: } else {
61df933942 2020-11-18 436: let source_id = words.next().unwrap().parse::<i32>().unwrap_or(0);
61df933942 2020-11-18 437: &core.check(source_id, None).await?;
438: }
439: },
440:
441: // clear
442:
443: "/clean" => {
444: if core.owner != i64::from(message.from.id) {
445: reply.push("Reserved for testing\\.".to_string());
446: } else {
447: let source_id = words.next().unwrap().parse::<i32>().unwrap_or(0);
448: &core.clean(source_id).await?;
449: }
450: },
451:
452: // enable
453:
454: "/enable" => {
455: let channel = words.next().unwrap();
456: if ! re_username.is_match(&channel) {
457: reply.push("Usernames should be something like \"@\\[a-z]\\[a-z0-9_]+\", aren't they?".to_string());
458: } else {
459: match core.enable(message.from.id, channel).await {
460: Ok(_) => {
461: reply.push("Channel enabled\\.".to_string());
462: }
463: Err(err) => {
464: core.debug(&err.to_string())?;
465: },
466: }
467: }
468: },
469:
470: // disable
471:
472: "/disable" => {
473: let channel = words.next().unwrap();
474: if ! re_username.is_match(&channel) {
475: reply.push("Usernames should be something like \"@\\[a-z]\\[a-z0-9_]+\", aren't they?".to_string());
476: } else {
477: match core.disable(message.from.id, channel).await {
478: Ok(_) => {
479: reply.push("Channel disabled\\.".to_string());
480: }
481: Err(err) => {
482: core.debug(&err.to_string())?;
483: },
484: }
485: }
486: },
487:
488: _ => {
489: },
490: };
491: },
492: _ => {
493: },
494: };
495: if reply.len() > 0 {
496: match core.tg.send(message.text_reply(reply.join("\n")).parse_mode(types::ParseMode::MarkdownV2)).await {
497: Ok(_) => {},
498: Err(err) => {
499: dbg!(reply.join("\n"));
500: println!("{}", err);
501: },
502: }
503: }
504: },
505: _ => {},
506: };
507: }
61df933942 2020-11-18 508: /*
61df933942 2020-11-18 509: loop {
61df933942 2020-11-18 510: println!("cycle");
61df933942 2020-11-18 511: for _ in botdb.query("select owner from rsstg_updates where owner is NULL limit 1;", &[])? {
61df933942 2020-11-18 512: for row in botdb.query("update rsstg_updates set owner = $1 where update->>'update_id' = ( select update->>'update_id' from rsstg_updates where owner is NULL limit 1 for update skip locked ) returning update;", &[owner])? {
61df933942 2020-11-18 513: let u :types::Update = serde_json::from_value(row.get(0))?;
61df933942 2020-11-18 514: println!("update: {:?}", &u);
61df933942 2020-11-18 515: /*
61df933942 2020-11-18 516: if let Some(message) = &u.message {
61df933942 2020-11-18 517: //if u["message"] != None {
61df933942 2020-11-18 518: if let (Some(entities), Some(text)) = (&message.entities, &message.text) {
61df933942 2020-11-18 519: //if u["message"]["entities"] {
61df933942 2020-11-18 520: for entry in entities {
61df933942 2020-11-18 521: if &entry.type_ == "bot_command" {
61df933942 2020-11-18 522: println!("command: {:?}", &text.chars().skip(entry.offset as usize).take(entry.length as usize).collect::<String>());
61df933942 2020-11-18 523: }
61df933942 2020-11-18 524: println!("entity: {:?}", &entry);
61df933942 2020-11-18 525: }
61df933942 2020-11-18 526: }
61df933942 2020-11-18 527: }
61df933942 2020-11-18 528: */
61df933942 2020-11-18 529: }
61df933942 2020-11-18 530: }
61df933942 2020-11-18 531: std::process::exit(0);
61df933942 2020-11-18 532: }
61df933942 2020-11-18 533: */
61df933942 2020-11-18 534:
61df933942 2020-11-18 535: //bot.polling().start().await.unwrap();
536:
537: Ok(())
538: }