From b4dc2ce5618c4477cfbc523da248a9d8e4b7fae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Sun, 27 Oct 2024 16:55:50 +0100 Subject: [PATCH] If a group migrates to a supergroup, update to the new id in the db and re-send the announcement --- src/bot.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 79a8050..6a7667c 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -10,6 +10,7 @@ use diesel::ExpressionMethods; use diesel::QueryDsl; use diesel::RunQueryDsl; use itertools::Itertools; +use log::info; use strum::Display; use strum::EnumIter; use strum::IntoEnumIterator; @@ -28,7 +29,7 @@ use teloxide::types::MessageId; use teloxide::types::{ChatId, ReplyParameters}; use teloxide::types::{Message, Update}; use teloxide::utils::command::BotCommands; -use teloxide::Bot; +use teloxide::{Bot, RequestError}; use crate::appointment::fetch_next_appointment; use crate::db::{ChatInfo, Reminder}; @@ -332,9 +333,9 @@ pub async fn fetch_and_announce_appointment( .to_string(), }; - let announcement = bot - .send_message( - ChatId(chat_info.id), + let send_announcement = |chat_id| { + bot.send_message( + ChatId(chat_id), t!( "messages.next_appointment", locale = &chat_info.locale, @@ -343,7 +344,25 @@ pub async fn fetch_and_announce_appointment( uk_time = &uk_time_str.to_string() ), ) - .await?; + }; + + let mut announcement = send_announcement(chat_info.id).await; + + if let Err(e) = &announcement { + if let RequestError::MigrateToChatId(new_id) = e { + db.lock().await.exclusive_transaction(|db| { + use schema::chats::dsl::*; + info!("Migrating chat {} to {}", chat_info.id, new_id.0); + diesel::update(chats) + .filter(telegram_id.eq(chat_info.id)) + .set(telegram_id.eq(new_id.0)) + .execute(db) + })?; + announcement = send_announcement(new_id.0).await; + } + } + + let announcement = announcement?; if let Some(pinned_message_id) = entry.pinned_message_id { let mut unpin_message = bot.unpin_chat_message(ChatId(chat_info.id));