Migrate group id on all messages sent
This commit is contained in:
31
src/bot.rs
31
src/bot.rs
@@ -10,7 +10,6 @@ use diesel::ExpressionMethods;
|
|||||||
use diesel::QueryDsl;
|
use diesel::QueryDsl;
|
||||||
use diesel::RunQueryDsl;
|
use diesel::RunQueryDsl;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use log::info;
|
|
||||||
use strum::Display;
|
use strum::Display;
|
||||||
use strum::EnumIter;
|
use strum::EnumIter;
|
||||||
use strum::IntoEnumIterator;
|
use strum::IntoEnumIterator;
|
||||||
@@ -29,11 +28,12 @@ use teloxide::types::MessageId;
|
|||||||
use teloxide::types::{ChatId, ReplyParameters};
|
use teloxide::types::{ChatId, ReplyParameters};
|
||||||
use teloxide::types::{Message, Update};
|
use teloxide::types::{Message, Update};
|
||||||
use teloxide::utils::command::BotCommands;
|
use teloxide::utils::command::BotCommands;
|
||||||
use teloxide::{Bot, RequestError};
|
use teloxide::Bot;
|
||||||
|
|
||||||
use crate::appointment::fetch_next_appointment;
|
use crate::appointment::fetch_next_appointment;
|
||||||
use crate::db::{ChatInfo, Reminder};
|
use crate::db::{ChatInfo, Reminder};
|
||||||
use crate::db::{DbChat, ParseReminderError};
|
use crate::db::{DbChat, ParseReminderError};
|
||||||
|
use crate::migrating_send::migrating_send;
|
||||||
use crate::{schema, Database};
|
use crate::{schema, Database};
|
||||||
|
|
||||||
#[derive(BotCommands, Clone, EnumIter, Display)]
|
#[derive(BotCommands, Clone, EnumIter, Display)]
|
||||||
@@ -333,9 +333,10 @@ pub async fn fetch_and_announce_appointment(
|
|||||||
.to_string(),
|
.to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let send_announcement = |chat_id| {
|
let announcement = migrating_send(
|
||||||
bot.send_message(
|
bot,
|
||||||
ChatId(chat_id),
|
db,
|
||||||
|
ChatId(chat_info.id),
|
||||||
t!(
|
t!(
|
||||||
"messages.next_appointment",
|
"messages.next_appointment",
|
||||||
locale = &chat_info.locale,
|
locale = &chat_info.locale,
|
||||||
@@ -344,25 +345,7 @@ pub async fn fetch_and_announce_appointment(
|
|||||||
uk_time = &uk_time_str.to_string()
|
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 {
|
if let Some(pinned_message_id) = entry.pinned_message_id {
|
||||||
let mut unpin_message = bot.unpin_chat_message(ChatId(chat_info.id));
|
let mut unpin_message = bot.unpin_chat_message(ChatId(chat_info.id));
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ mod appointment;
|
|||||||
mod bot;
|
mod bot;
|
||||||
mod db;
|
mod db;
|
||||||
mod error;
|
mod error;
|
||||||
|
mod migrating_send;
|
||||||
mod schema;
|
mod schema;
|
||||||
|
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
@@ -21,10 +22,10 @@ use diesel::{ExpressionMethods, QueryDsl};
|
|||||||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||||
use error::ConfigLoadError;
|
use error::ConfigLoadError;
|
||||||
use log::*;
|
use log::*;
|
||||||
|
use migrating_send::migrating_send;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use teloxide::adaptors::Throttle;
|
use teloxide::adaptors::Throttle;
|
||||||
use teloxide::prelude::RequesterExt;
|
use teloxide::prelude::RequesterExt;
|
||||||
use teloxide::requests::Requester;
|
|
||||||
use teloxide::types::ChatId;
|
use teloxide::types::ChatId;
|
||||||
use teloxide::{adaptors::throttle::Limits, Bot};
|
use teloxide::{adaptors::throttle::Limits, Bot};
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
@@ -238,8 +239,7 @@ async fn check_task(bot: &Throttle<Bot>, db: &Database) -> Result<()> {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
bot.send_message(ChatId(chat_info.id), reminder.text)
|
migrating_send(bot, db, ChatId(chat_info.id), reminder.text).await?;
|
||||||
.await?;
|
|
||||||
|
|
||||||
db.lock().await.transaction(|db| {
|
db.lock().await.transaction(|db| {
|
||||||
use schema::chats::dsl::*;
|
use schema::chats::dsl::*;
|
||||||
|
|||||||
35
src/migrating_send.rs
Normal file
35
src/migrating_send.rs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
use anyhow::Result;
|
||||||
|
use diesel::{ExpressionMethods, RunQueryDsl};
|
||||||
|
use log::info;
|
||||||
|
use teloxide::{
|
||||||
|
adaptors::Throttle,
|
||||||
|
prelude::Requester,
|
||||||
|
types::{ChatId, Message},
|
||||||
|
Bot, RequestError,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{schema, Database};
|
||||||
|
|
||||||
|
pub async fn migrating_send<T: Into<String>>(
|
||||||
|
bot: &Throttle<Bot>,
|
||||||
|
db: &Database,
|
||||||
|
chat_id: ChatId,
|
||||||
|
text: T,
|
||||||
|
) -> Result<Message> {
|
||||||
|
let text = text.into();
|
||||||
|
let mut result = bot.send_message(chat_id, &text).await;
|
||||||
|
if let Err(e) = &result {
|
||||||
|
if let RequestError::MigrateToChatId(new_id) = e {
|
||||||
|
db.lock().await.exclusive_transaction(|db| {
|
||||||
|
use schema::chats::dsl::*;
|
||||||
|
info!("Migrating chat {} to {}", chat_id.0, new_id.0);
|
||||||
|
diesel::update(chats)
|
||||||
|
.filter(telegram_id.eq(chat_id.0))
|
||||||
|
.set(telegram_id.eq(new_id.0))
|
||||||
|
.execute(db)
|
||||||
|
})?;
|
||||||
|
result = bot.send_message(*new_id, text).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(result?)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user