Diff
Logged in as anonymous

Differences From Artifact [b857ac5a83]:

To Artifact [ff793eb38f]:


18
19
20
21
22
23
24

25
26
27
28

29
30
31
32
33
34
35
36
37
38
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

33
34
35
36
37
38
39







+




+


-







use tgbot::{
	api::Client,
	types::{
		ChatPeerId,
		InputFile,
		InputFileReader,
		InputMediaDocument,
		InputText,
		MediaGroup,
		MediaGroupItem,
		Message,
		ParseMode::Html,
		SendDocument,
		SendMediaGroup,
		SendMessage,
		SendDocument,
	},
};

#[derive(Debug)]
pub struct TelegramTransport {
	tg: Client,
	recipients: HashMap<String, ChatPeerId>,
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
115
116
117
118
119
120
121
122
123
124
125
126


127
128
129
130
131
132

133
134
135
136
137
138
139
140
141
142



143
144
145
146






147
148
149
150
151
152
153
154
155
156
157
158
159



160
161
162
163
164
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
115
116
117
118
119
120
121
122
123
124

125
126
127
128
129
130
131

132










133
134
135


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153



154
155
156
157
158
159
160
161







-
+














-
+










-
+
-

-
+
-











-
+
+





-
+
-
-
-
-
-
-
-
-
-
-
+
+
+
-
-


+
+
+
+
+
+










-
-
-
+
+
+





	///
	/// # Returns
	/// * `Result<Message>` - Telegram API response.
	///
	/// # Errors
	/// Returns an error if `msg` contains a closing Telegram tag or sending fails.
	pub async fn debug (&self, msg: &str) -> Result<Message> {
		self.send(&self.default, format!("<pre>{}</pre>", validate(msg).stack()?)).await
		self.send(&self.default, &format!("<pre>{}</pre>", validate(msg).stack()?)).await
	}

	/// Retrieves a chat ID by name.
	///
	/// # Arguments
	/// * `name` - Name or email to look up.
	///
	/// # Returns
	/// * `Result<&ChatPeerId>` - Chat ID if found.
	///
	/// # Errors
	/// Returns an error if `name` is not configured.
	pub fn get (&self, name: &str) -> Result<&ChatPeerId> {
		self.recipients.get(&name.to_lowercase())
			.with_context(|| format!("Recipient \"{name}\" not found in configuration"))
			.with_context(|| format!("Recipient {name:?} not found in configuration"))
	}

	/// Sends a text message to a specified chat.
	///
	/// # Arguments
	/// * `to` - Target chat ID.
	/// * `msg` - Message text (supports HTML formatting).
	///
	/// # Returns
	/// * `Result<Message>` - Telegram API response.
	pub async fn send <S> (&self, to: &ChatPeerId, msg: S) -> Result<Message>
	pub async fn send (&self, to: &ChatPeerId, msg: &str) -> Result<Message> {
	where S: Into<String> + Debug{
		self.tg.execute(
			SendMessage::new(*to, msg)
			SendMessage::new(*to, InputText::from(msg).with_format(Html))
			.with_parse_mode(Html)
		).await.stack()
	}

	/// Sends a message with attachments to a specified chat.
	///
	/// # Arguments
	/// * `to` - Target chat ID.
	/// * `media` - List of attachments, non-empty.
	/// * `msg` - Message text (supports HTML formatting).
	///
	/// # Returns
	/// * `Result<()>` - Success or error.
	/// * `Result<()>` - fails if `media` is empty, problems forming media
	///   group, request fails.
	pub async fn sendgroup (&self, to: &ChatPeerId, media: Vec<Attachment>, msg: &str) -> Result<()> {
		if media.len() > 1 {
			let mut attach = vec![];
			let mut pos = media.len();
			for file in media {
				let mut caption = InputMediaDocument::default();
				let mut doc = InputMediaDocument::from(
				if pos == 1 {
					caption = caption.with_caption(msg)
						.with_caption_parse_mode(Html);
				}
				pos -= 1;
				attach.push(
					MediaGroupItem::for_document(
						InputFile::from(
							InputFileReader::from(file.data)
								.with_file_name(file.name)
					InputFile::from(
						InputFileReader::from(file.data)
							.with_file_name(file.name)
						),
						caption
					)
				);
				if pos == 1 {
					doc = doc.with_caption(InputText::from(msg)
						.with_format(Html));
				}
				pos -= 1;
				attach.push(MediaGroupItem::from(doc));
			}
			self.tg.execute(SendMediaGroup::new(*to, MediaGroup::new(attach).stack()?)).await.stack()?;
		} else {
			if media.is_empty() {
				bail!("At least one attachment is required.");
			}
			self.tg.execute(
				SendDocument::new(
					*to,
					InputFileReader::from(media[0].data.clone())
					.with_file_name(media[0].name.clone())
				).with_caption(msg)
				.with_caption_parse_mode(Html)
						.with_file_name(media[0].name.clone())
				).with_caption(InputText::from(msg)
					.with_format(Html))
			).await.stack()?;
		}
		Ok(())
	}
}