Skip to content

Commit 3233dfd

Browse files
committed
[Telegram] Allow disabling buttons
1 parent 6954407 commit 3233dfd

File tree

3 files changed

+70
-30
lines changed

3 files changed

+70
-30
lines changed

src/config/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,9 @@ notify = ["meow", "woof", { ref = "woof", id = 123 }]
481481
},
482482
option: NotificationOption {
483483
author_name: false,
484-
ext: ()
484+
ext: telegram::notify::OptionExt {
485+
no_button: false,
486+
}
485487
}
486488
},
487489
chat: telegram::ConfigChat::Id(5678),

src/platform/telegram/notify/mod.rs

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,39 @@ use crate::{
2929
},
3030
};
3131

32+
#[derive(Clone, Debug, PartialEq, Deserialize)]
33+
pub struct OptionExt {
34+
// For channels with comments enabled, messages sent with buttons will hide the comment
35+
// entrance, this option disables sending messages with buttons.
36+
#[serde(default = "helper::refl_bool::<false>")]
37+
pub no_button: bool,
38+
}
39+
40+
impl Default for OptionExt {
41+
fn default() -> Self {
42+
helper::serde_default()
43+
}
44+
}
45+
46+
#[derive(Clone, Debug, PartialEq, Deserialize)]
47+
pub struct OptionExtOverride {
48+
pub no_button: Option<bool>,
49+
}
50+
51+
impl Overridable for OptionExt {
52+
type Override = OptionExtOverride;
53+
54+
fn override_into(self, new: Self::Override) -> Self {
55+
Self {
56+
no_button: new.no_button.unwrap_or(self.no_button),
57+
}
58+
}
59+
}
60+
3261
#[derive(Clone, Debug, PartialEq, Deserialize)]
3362
pub struct ConfigParams {
3463
#[serde(default, flatten)]
35-
pub base: config::NotifierBase,
64+
pub base: config::NotifierBase<OptionExt>,
3665
#[serde(flatten)]
3766
pub chat: ConfigChat,
3867
pub thread_id: Option<i64>,
@@ -90,7 +119,7 @@ impl Overridable for ConfigParams {
90119
#[serde(deny_unknown_fields)]
91120
pub struct ConfigOverride {
92121
#[serde(flatten)]
93-
pub base: Option<config::NotifierBaseOverride>,
122+
pub base: Option<config::NotifierBaseOverride<OptionExtOverride>>,
94123
#[serde(flatten)]
95124
pub chat: Option<ConfigChat>,
96125
pub thread_id: Option<i64>,
@@ -427,22 +456,40 @@ impl Notifier {
427456
let attachments = post.attachments_recursive(true);
428457
let num_attachments = attachments.len();
429458

459+
// Jump buttons
460+
let buttons = if !self.params.base.option.ext.no_button
461+
&& (num_attachments == 0 || num_attachments == 1)
462+
{
463+
Some(Markup::InlineKeyboard(vec![post
464+
.urls_recursive()
465+
.into_iter()
466+
.filter_map(|url| url.as_clickable())
467+
.map(|url| Button::new_url(&url.display, &url.url))
468+
.collect::<Vec<_>>()]))
469+
} else {
470+
text.push_plain("\n\n");
471+
let mut iter = post
472+
.urls_recursive()
473+
.into_iter()
474+
.filter_map(|url| url.as_clickable())
475+
.peekable();
476+
while let Some(url) = iter.next() {
477+
text.push_link(format!(">> {} <<", url.display), &url.url);
478+
if iter.peek().is_some() {
479+
text.push_plain(" | ");
480+
}
481+
}
482+
None
483+
};
484+
430485
let resp = match num_attachments {
431486
0 | 1 => {
432-
// Jump buttons
433-
let buttons = vec![post
434-
.urls_recursive()
435-
.into_iter()
436-
.filter_map(|url| url.as_clickable())
437-
.map(|url| Button::new_url(&url.display, &url.url))
438-
.collect::<Vec<_>>()];
439-
440487
if num_attachments == 0 {
441488
Request::new(token)
442489
.send_message(&self.params.chat, text)
443490
.thread_id_opt(self.params.thread_id)
444491
.disable_notification_bool(DISABLE_NOTIFICATION)
445-
.markup(Markup::InlineKeyboard(buttons))
492+
.markup_opt(buttons)
446493
.send()
447494
.await
448495
.map(|resp| resp.discard_result())
@@ -461,30 +508,13 @@ impl Notifier {
461508
.text(text)
462509
.thread_id_opt(self.params.thread_id)
463510
.disable_notification_bool(DISABLE_NOTIFICATION)
464-
.markup(Markup::InlineKeyboard(buttons))
511+
.markup_opt(buttons)
465512
.send()
466513
.await
467514
.map(|resp| resp.discard_result())
468515
}
469516
}
470517
_ => {
471-
text.push_plain("\n\n");
472-
473-
// Jump buttons
474-
{
475-
let mut iter = post
476-
.urls_recursive()
477-
.into_iter()
478-
.filter_map(|url| url.as_clickable())
479-
.peekable();
480-
while let Some(url) = iter.next() {
481-
text.push_link(format!(">> {} <<", url.display), &url.url);
482-
if iter.peek().is_some() {
483-
text.push_plain(" | ");
484-
}
485-
}
486-
}
487-
488518
let medias = attachments.iter().map(|attachment| match attachment {
489519
// TODO: Mixing GIF in media group to send is not yet supported in Telegram, add
490520
// an overlay like video? (see comment in twitter.com implementation)

src/platform/telegram/notify/request.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ impl<'a> SendMessage<'a> {
422422
}
423423
}
424424

425+
pub fn markup_opt(self, markup: Option<Markup<'a>>) -> Self {
426+
Self { markup, ..self }
427+
}
428+
425429
pub async fn send(self) -> anyhow::Result<Response<ResultMessage>> {
426430
let mut body = json!(
427431
{
@@ -598,6 +602,10 @@ impl<'a> SendMedia<'a> {
598602
}
599603
}
600604

605+
pub fn markup_opt(self, markup: Option<Markup<'a>>) -> Self {
606+
Self { markup, ..self }
607+
}
608+
601609
pub fn prefer_self_host(self) -> Self {
602610
Self {
603611
prefer_self_host: true,

0 commit comments

Comments
 (0)