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