Skip to content

Commit 394f76f

Browse files
authored
Merge pull request #44 from Quantus-Network/feat/support-message-thread
feat: support message thread in telegram service
2 parents 22b6acf + c9a1f30 commit 394f76f

7 files changed

Lines changed: 33 additions & 25 deletions

File tree

config/default.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ whitelist = ["username"]
6262

6363
[tg_bot]
6464
base_url = "https://api.telegram.org"
65-
chat_id = '-1'
65+
chat_id = "-1"
66+
# This is optional, if you don't need message thread just remove it
67+
message_thread_id = "1"
6668
token = "token"
6769

6870
[raid_leaderboard]

config/example.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ whitelist = ["example-username"]
7373
[tg_bot]
7474
base_url = "https://api.telegram.org"
7575
chat_id = '-1'
76+
# This is optional, if you don't need message thread just remove it
77+
message_thread_id = '1'
7678
token = "token"
7779

7880
[raid_leaderboard]

config/test.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ whitelist = ["test-username"]
6363
[tg_bot]
6464
base_url = "https://api.telegram.org"
6565
chat_id = '-1'
66+
# This is optional, if you don't need message thread just remove it
67+
message_thread_id = '1'
6668
token = "token"
6769

6870
[raid_leaderboard]

src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub struct TelegramBotConfig {
8282
pub base_url: String,
8383
pub token: String,
8484
pub chat_id: String,
85+
pub message_thread_id: Option<String>,
8586
}
8687

8788
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -214,6 +215,7 @@ impl Default for Config {
214215
tg_bot: TelegramBotConfig {
215216
base_url: "https://api.telegram.org".to_string(),
216217
chat_id: "-0".to_string(),
218+
message_thread_id: Some("-0".to_string()),
217219
token: "token".to_string(),
218220
},
219221
raid_leaderboard: RaidLeaderboardConfig {

src/main.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,7 @@ async fn main() -> AppResult<()> {
270270
config.x_oauth.clone(),
271271
Some(config.tweet_sync.api_key.clone()),
272272
)?);
273-
let telegram_service = Arc::new(TelegramService::new(
274-
&config.tg_bot.base_url,
275-
&config.tg_bot.token,
276-
&config.tg_bot.chat_id,
277-
));
273+
let telegram_service = Arc::new(TelegramService::new(config.tg_bot.clone()));
278274
let server_db = db.clone();
279275
let graphql_client = Arc::new(graphql_client.clone());
280276
let server_addr_clone = server_address.clone();

src/services/telegram_service.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
use reqwest::{Client, StatusCode};
22
use serde::Serialize;
33

4-
use crate::{AppError, AppResult};
4+
use crate::{config::TelegramBotConfig, AppError, AppResult};
55

66
#[derive(Clone)]
77
pub struct TelegramService {
88
client: Client,
99
base_url: String,
1010
default_chat_id: String,
11+
default_message_thread_id: Option<String>,
1112
}
1213

1314
#[derive(Serialize)]
1415
struct MessagePayload<'a> {
1516
chat_id: &'a str,
17+
#[serde(skip_serializing_if = "Option::is_none")]
18+
message_thread_id: Option<&'a str>,
1619
text: &'a str,
1720
parse_mode: &'a str,
1821
disable_web_page_preview: bool,
@@ -40,23 +43,26 @@ impl TelegramService {
4043
.replace("!", "\\!")
4144
}
4245

43-
pub fn new(base_url: &str, token: &str, default_chat_id: &str) -> Self {
46+
pub fn new(config: TelegramBotConfig) -> Self {
4447
Self {
4548
client: Client::new(),
46-
base_url: format!("{base_url}/bot{token}"),
47-
default_chat_id: default_chat_id.to_string(),
49+
base_url: format!("{}/bot{}", config.base_url, config.token),
50+
default_chat_id: config.chat_id,
51+
default_message_thread_id: config.message_thread_id,
4852
}
4953
}
5054

5155
pub async fn send_message(&self, text: &str) -> AppResult<()> {
52-
self.send(&self.default_chat_id, text).await
56+
self.send(&self.default_chat_id, self.default_message_thread_id.as_deref(), text)
57+
.await
5358
}
5459

55-
async fn send(&self, chat_id: &str, text: &str) -> AppResult<()> {
60+
async fn send(&self, chat_id: &str, message_thread_id: Option<&str>, text: &str) -> AppResult<()> {
5661
let url = format!("{}/sendMessage", self.base_url);
5762

5863
let payload = MessagePayload {
5964
chat_id,
65+
message_thread_id,
6066
text,
6167
parse_mode: "MarkdownV2", // or "HTML"
6268
disable_web_page_preview: true,

src/services/tweet_synchronizer_service.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,10 @@ impl TweetSynchronizerService {
112112
let link = build_x_status_url(author_name, &tweet.id);
113113

114114
let tg_message = format!(
115-
"Raid Target Found!\n\n*Link*: {}\n*Author*: {}\n*Text*: {}\n*Impressions*: {}\n*Posted At*: {}",
116-
TelegramService::escape_markdown_v2(&link),
117-
TelegramService::escape_markdown_v2(author_name),
118-
TelegramService::escape_markdown_v2(&tweet.text),
119-
tweet.impression_count,
120-
tweet.created_at
115+
"**Raid Target Found!**\n\n**Link**: {}\n**Author**: {}\n**Text**: {}\n**Impressions**: {}\n**Posted At**: {}",
116+
&link, author_name, &tweet.text, tweet.impression_count, tweet.created_at
121117
);
122-
messages.push(tg_message);
118+
messages.push(TelegramService::escape_markdown_v2(&tg_message));
123119
}
124120

125121
tokio::spawn(async move {
@@ -222,7 +218,7 @@ impl TweetSynchronizerService {
222218
#[cfg(test)]
223219
mod tests {
224220
use super::*;
225-
use crate::config::Config;
221+
use crate::config::{Config, TelegramBotConfig};
226222
use crate::models::raid_quest::CreateRaidQuest;
227223
use crate::utils::test_db::reset_database;
228224
use mockall::predicate::*;
@@ -253,11 +249,13 @@ mod tests {
253249

254250
// B. Setup Telegram Mock Server
255251
let mock_server = MockServer::start().await;
256-
let telegram_service = Arc::new(TelegramService::new(
257-
&mock_server.uri(),
258-
"123456",
259-
&config.tg_bot.chat_id,
260-
));
252+
let telegram_config = TelegramBotConfig {
253+
base_url: mock_server.uri(),
254+
token: "123456".to_string(),
255+
chat_id: config.tg_bot.chat_id.clone(),
256+
message_thread_id: config.tg_bot.message_thread_id.clone(),
257+
};
258+
let telegram_service = Arc::new(TelegramService::new(telegram_config));
261259

262260
// C. Config
263261
let app_config = Arc::new(config);

0 commit comments

Comments
 (0)