1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
use crate::{
Arc,
Mutex,
};
use std::{
borrow::Cow,
collections::HashMap,
fmt,
};
use serde::{
Deserialize,
Serialize,
};
use stacked_errors::{
bail,
Result,
StackableErr,
};
use tgbot::{
api::Client,
types::{
Bot,
ChatPeerId,
GetBot,
InlineKeyboardButton,
InlineKeyboardMarkup,
Message,
ParseMode,
SendMessage,
},
};
const CB_VERSION: u8 = 0;
#[derive(Serialize, Deserialize, Debug)]
pub enum Callback {
// List all feeds (version, name to show, page number)
List(u8, String, u8),
}
impl Callback {
pub fn list (text: &str, page: u8) -> Callback {
Callback::List(CB_VERSION, text.to_owned(), page)
}
fn version (&self) -> u8 {
match self {
Callback::List(version, .. ) => *version,
}
}
}
impl fmt::Display for Callback {
fn fmt (&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&toml::to_string(self).map_err(|_| fmt::Error)?)
}
}
/// Produce new Keyboard Markup from current Callback
pub async fn get_kb (cb: &Callback, feeds: Arc<Mutex<HashMap<i32, String>>>) -> Result<InlineKeyboardMarkup> {
if cb.version() != CB_VERSION {
bail!("Wrong callback version.");
}
let mark = match cb {
Callback::List(_, name, page) => {
let mut kb = vec![];
let feeds = feeds.lock_arc().await;
let long = feeds.len() > 6;
let (start, end) = if long {
(page * 5, 5 + page * 5)
} else {
(0, 6)
};
let mut i = 0;
if name.is_empty() {
for (id, name) in feeds.iter() {
if i < start { continue }
if i > end { break }
i += 1;
kb.push(vec![
InlineKeyboardButton::for_callback_data(
format!("{}. {}", id, name),
Callback::list("xxx", *page).to_string()), // XXX edit
]);
}
} else {
let mut found = false;
let mut first_page = None;
for (id, feed_name) in feeds.iter() {
if name == feed_name {
found = true;
|
>
<
>
>
>
>
>
>
>
>
>
>
|
>
|
>
>
>
>
>
>
|
>
>
>
>
|
<
<
>
|
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
use crate::{
Arc,
Mutex,
core::FeedList,
};
use std::{
borrow::Cow,
fmt,
};
use serde::{
Deserialize,
Serialize,
};
use stacked_errors::{
bail,
Result,
StackableErr,
};
use tgbot::{
api::Client,
types::{
AnswerCallbackQuery,
Bot,
ChatPeerId,
GetBot,
InlineKeyboardButton,
InlineKeyboardMarkup,
Message,
ParseMode,
SendMessage,
},
};
const CB_VERSION: u8 = 0;
#[derive(Serialize, Deserialize, Debug)]
pub enum Callback {
// Edit one feed (version, name)
Edit(u8, String),
// List all feeds (version, name to show, page number)
List(u8, String, u8),
// Show root menu (version)
Menu(u8),
}
impl Callback {
pub fn edit <S>(text: S) -> Callback
where S: Into<String> {
Callback::Edit(CB_VERSION, text.into())
}
pub fn list <S>(text: S, page: u8) -> Callback
where S: Into<String> {
Callback::List(CB_VERSION, text.into(), page)
}
pub fn menu () -> Callback {
Callback::Menu(CB_VERSION)
}
fn version (&self) -> u8 {
match self {
Callback::Edit(version, .. ) => *version,
Callback::List(version, .. ) => *version,
Callback::Menu(version) => *version,
}
}
}
impl fmt::Display for Callback {
fn fmt (&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&toml::to_string(self).map_err(|_| fmt::Error)?)
}
}
/// Produce new Keyboard Markup from current Callback
pub async fn get_kb (cb: &Callback, feeds: Arc<Mutex<FeedList>>) -> Result<InlineKeyboardMarkup> {
if cb.version() != CB_VERSION {
bail!("Wrong callback version.");
}
let mark = match cb {
Callback::Edit(_, _name) => { // XXX edit missing
let kb: Vec<Vec<InlineKeyboardButton>> = vec![];
InlineKeyboardMarkup::from(kb)
},
Callback::List(_, name, page) => {
let mut kb = vec![];
let feeds = feeds.lock_arc().await;
let long = feeds.len() > 6;
let (start, end) = if long {
(page * 5 + 1, 5 + page * 5)
} else {
(0, 6)
};
let mut i = 0;
if name.is_empty() {
for (id, name) in feeds.iter() {
i += 1;
if i < start { continue }
kb.push(vec![
InlineKeyboardButton::for_callback_data(
format!("{}. {}", id, name),
Callback::edit(name).to_string()),
]);
if i > end { break }
}
} else {
let mut found = false;
let mut first_page = None;
for (id, feed_name) in feeds.iter() {
if name == feed_name {
found = true;
|
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
Callback::list("", if *page == 0 { *page } else { page - 1 } ).to_string()),
InlineKeyboardButton::for_callback_data(">>",
Callback::list("", page + 1).to_string()),
]);
}
InlineKeyboardMarkup::from(kb)
},
};
Ok(mark)
}
pub enum MyMessage <'a> {
Html { text: Cow<'a, str> },
HtmlTo { text: Cow<'a, str>, to: ChatPeerId },
HtmlToKb { text: Cow<'a, str>, to: ChatPeerId, kb: InlineKeyboardMarkup },
Text { text: Cow<'a, str> },
TextTo { text: Cow<'a, str>, to: ChatPeerId },
}
impl MyMessage <'_> {
pub fn html <'a, S> (text: S) -> MyMessage<'a>
where S: Into<Cow<'a, str>> {
let text = text.into();
MyMessage::Html { text }
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
<
<
|
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
Callback::list("", if *page == 0 { *page } else { page - 1 } ).to_string()),
InlineKeyboardButton::for_callback_data(">>",
Callback::list("", page + 1).to_string()),
]);
}
InlineKeyboardMarkup::from(kb)
},
Callback::Menu(_) => {
let kb = vec![
vec![
InlineKeyboardButton::for_callback_data(
"Add new channel",
Callback::menu().to_string()), // new XXX
],
vec![
InlineKeyboardButton::for_callback_data(
"List channels",
Callback::list("", 0).to_string()),
],
];
InlineKeyboardMarkup::from(kb)
},
};
Ok(mark)
}
pub enum MyMessage <'a> {
Html { text: Cow<'a, str> },
HtmlTo { text: Cow<'a, str>, to: ChatPeerId },
HtmlToKb { text: Cow<'a, str>, to: ChatPeerId, kb: InlineKeyboardMarkup },
}
impl MyMessage <'_> {
pub fn html <'a, S> (text: S) -> MyMessage<'a>
where S: Into<Cow<'a, str>> {
let text = text.into();
MyMessage::Html { text }
|
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
pub fn html_to_kb <'a, S> (text: S, to: ChatPeerId, kb: InlineKeyboardMarkup) -> MyMessage<'a>
where S: Into<Cow<'a, str>> {
let text = text.into();
MyMessage::HtmlToKb { text, to, kb }
}
pub fn text <'a, S> (text: S) -> MyMessage<'a>
where S: Into<Cow<'a, str>> {
let text = text.into();
MyMessage::Text { text }
}
pub fn text_to <'a, S> (text: S, to: ChatPeerId) -> MyMessage<'a>
where S: Into<Cow<'a, str>> {
let text = text.into();
MyMessage::TextTo { text, to }
}
fn req (&self, tg: &Tg) -> Result<SendMessage> {
Ok(match self {
MyMessage::Html { text } =>
SendMessage::new(tg.owner, text.as_ref())
.with_parse_mode(ParseMode::Html),
MyMessage::HtmlTo { text, to } =>
SendMessage::new(*to, text.as_ref())
.with_parse_mode(ParseMode::Html),
MyMessage::HtmlToKb { text, to, kb } =>
SendMessage::new(*to, text.as_ref())
.with_parse_mode(ParseMode::Html)
.with_reply_markup(kb.clone()),
MyMessage::Text { text } =>
SendMessage::new(tg.owner, text.as_ref())
.with_parse_mode(ParseMode::MarkdownV2),
MyMessage::TextTo { text, to } =>
SendMessage::new(*to, text.as_ref())
.with_parse_mode(ParseMode::MarkdownV2),
})
}
}
#[derive(Clone)]
pub struct Tg {
pub me: Bot,
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
pub fn html_to_kb <'a, S> (text: S, to: ChatPeerId, kb: InlineKeyboardMarkup) -> MyMessage<'a>
where S: Into<Cow<'a, str>> {
let text = text.into();
MyMessage::HtmlToKb { text, to, kb }
}
fn req (&self, tg: &Tg) -> Result<SendMessage> {
Ok(match self {
MyMessage::Html { text } =>
SendMessage::new(tg.owner, text.as_ref())
.with_parse_mode(ParseMode::Html),
MyMessage::HtmlTo { text, to } =>
SendMessage::new(*to, text.as_ref())
.with_parse_mode(ParseMode::Html),
MyMessage::HtmlToKb { text, to, kb } =>
SendMessage::new(*to, text.as_ref())
.with_parse_mode(ParseMode::Html)
.with_reply_markup(kb.clone()),
})
}
}
#[derive(Clone)]
pub struct Tg {
pub me: Bot,
|