Compare commits
No commits in common. "3d9168e46f7b732968ad0ca8b96cdb7205a1a734" and "5fc84c54e747220494951c36ac9e3758074b41aa" have entirely different histories.
3d9168e46f
...
5fc84c54e7
6 changed files with 4 additions and 57 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -2169,7 +2169,6 @@ dependencies = [
|
||||||
name = "telepingbot"
|
name = "telepingbot"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64 0.22.1",
|
|
||||||
"chrono",
|
"chrono",
|
||||||
"grammers-client",
|
"grammers-client",
|
||||||
"grammers-session",
|
"grammers-session",
|
||||||
|
|
|
@ -12,7 +12,6 @@ version = "0.1.0"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
base64 = "0.22.1"
|
|
||||||
chrono = "0.4.38"
|
chrono = "0.4.38"
|
||||||
grammers-client = "0.6.0"
|
grammers-client = "0.6.0"
|
||||||
grammers-session = "0.5.2"
|
grammers-session = "0.5.2"
|
||||||
|
|
|
@ -42,11 +42,6 @@ Or just run the binary file in `target/release/telepingbot`
|
||||||
### `/ping/@<bot_username>`
|
### `/ping/@<bot_username>`
|
||||||
This endpoint is to ping the bot and returns if it's online or not.
|
This endpoint is to ping the bot and returns if it's online or not.
|
||||||
|
|
||||||
#### Url querys
|
|
||||||
- "cmd": One word base64Url encoded, will use it as a command, default is `start`
|
|
||||||
- "args": Base64Url encoded, will use it as arguments for the command, default is `""`
|
|
||||||
|
|
||||||
|
|
||||||
#### Headers
|
#### Headers
|
||||||
- `Authorization`: The API access token. e.g: `Authorization: FirstToken`
|
- `Authorization`: The API access token. e.g: `Authorization: FirstToken`
|
||||||
|
|
||||||
|
|
45
src/api.rs
45
src/api.rs
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use base64::Engine;
|
|
||||||
use salvo::{catcher::Catcher, http::HeaderValue, hyper::header, logging::Logger, prelude::*};
|
use salvo::{catcher::Catcher, http::HeaderValue, hyper::header, logging::Logger, prelude::*};
|
||||||
|
|
||||||
use crate::{superbot, PingList};
|
use crate::{superbot, PingList};
|
||||||
|
@ -83,46 +82,12 @@ fn write_json_body(res: &mut Response, json_body: impl serde::Serialize) {
|
||||||
.ok();
|
.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decode_base64url_or(
|
|
||||||
or: impl FnOnce() -> String,
|
|
||||||
res: &mut Response,
|
|
||||||
query: Option<String>,
|
|
||||||
) -> Option<String> {
|
|
||||||
log::debug!("Decode {query:?}");
|
|
||||||
query.map_or_else(
|
|
||||||
|| Some(or()),
|
|
||||||
|query| {
|
|
||||||
crate::Base64Url.decode(query).map_or_else(
|
|
||||||
|_| {
|
|
||||||
log::error!("Is invalid base64Url");
|
|
||||||
res.status_code(StatusCode::BAD_REQUEST);
|
|
||||||
write_json_body(
|
|
||||||
res,
|
|
||||||
MessageSchema::new("Invalid base64Url args").code(StatusCode::BAD_REQUEST),
|
|
||||||
);
|
|
||||||
None
|
|
||||||
},
|
|
||||||
|decoded_query| Some(String::from_utf8_lossy(&decoded_query).into_owned()),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[handler]
|
#[handler]
|
||||||
async fn ping(req: &Request, res: &mut Response, depot: &mut Depot) {
|
async fn ping(req: &Request, res: &mut Response, depot: &mut Depot) {
|
||||||
let bot_username = req
|
let bot_username = req
|
||||||
.param::<String>("bot_username")
|
.param::<String>("bot_username")
|
||||||
.expect("The path param is required")
|
.expect("The path param is required")
|
||||||
.to_lowercase();
|
.to_lowercase();
|
||||||
|
|
||||||
let Some(bot_cmd) = decode_base64url_or(|| "start".to_owned(), res, req.query("cmd")) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Some(cmd_args) = decode_base64url_or(String::new, res, req.query("args")) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
let app_state = depot
|
let app_state = depot
|
||||||
.obtain::<Arc<AppState>>()
|
.obtain::<Arc<AppState>>()
|
||||||
.expect("The app state is injected");
|
.expect("The app state is injected");
|
||||||
|
@ -134,14 +99,8 @@ async fn ping(req: &Request, res: &mut Response, depot: &mut Depot) {
|
||||||
let msg = if !app_state.bots.contains(&bot_username) {
|
let msg = if !app_state.bots.contains(&bot_username) {
|
||||||
MessageSchema::new("Is not authorized to check the status of this bot")
|
MessageSchema::new("Is not authorized to check the status of this bot")
|
||||||
.code(StatusCode::BAD_REQUEST)
|
.code(StatusCode::BAD_REQUEST)
|
||||||
} else if let Ok(telegram_id) = superbot::send_command(
|
} else if let Ok(telegram_id) =
|
||||||
&app_state.tg_client,
|
superbot::send_start(&app_state.tg_client, &bot_username, bots).await
|
||||||
&bot_username,
|
|
||||||
bots,
|
|
||||||
&bot_cmd,
|
|
||||||
&cmd_args,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
{
|
||||||
if bots.check(telegram_id) {
|
if bots.check(telegram_id) {
|
||||||
MessageSchema::new("Alive")
|
MessageSchema::new("Alive")
|
||||||
|
|
|
@ -30,7 +30,6 @@ mod errors;
|
||||||
mod superbot;
|
mod superbot;
|
||||||
mod traits;
|
mod traits;
|
||||||
|
|
||||||
pub(crate) use base64::engine::general_purpose::URL_SAFE_NO_PAD as Base64Url;
|
|
||||||
pub(crate) use errors::{Error as ServerError, Result as ServerResult};
|
pub(crate) use errors::{Error as ServerError, Result as ServerResult};
|
||||||
use tokio::signal;
|
use tokio::signal;
|
||||||
pub(crate) use traits::PingList;
|
pub(crate) use traits::PingList;
|
||||||
|
|
|
@ -101,20 +101,16 @@ pub(crate) async fn handler(client: Client, bots: Arc<Mutex<Vec<crate::PingedBot
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn send_command(
|
pub(crate) async fn send_start(
|
||||||
client: &Client,
|
client: &Client,
|
||||||
bot_username: &str,
|
bot_username: &str,
|
||||||
bots: &Arc<Mutex<Vec<crate::PingedBot>>>,
|
bots: &Arc<Mutex<Vec<crate::PingedBot>>>,
|
||||||
command: &str,
|
|
||||||
args: &str,
|
|
||||||
) -> ServerResult<u64> {
|
) -> ServerResult<u64> {
|
||||||
if let Some(chat) = client.resolve_username(bot_username).await? {
|
if let Some(chat) = client.resolve_username(bot_username).await? {
|
||||||
log::debug!("Bots: {bots:?}");
|
log::debug!("Bots: {bots:?}");
|
||||||
let telegram_id = chat.id() as u64;
|
let telegram_id = chat.id() as u64;
|
||||||
bots.add_new(telegram_id);
|
bots.add_new(telegram_id);
|
||||||
client
|
client.send_message(chat, "/start").await?;
|
||||||
.send_message(chat, format!("/{command} {args}"))
|
|
||||||
.await?;
|
|
||||||
// Sleep, wating the response
|
// Sleep, wating the response
|
||||||
time::sleep(time::Duration::from_secs(2)).await;
|
time::sleep(time::Duration::from_secs(2)).await;
|
||||||
Ok(telegram_id)
|
Ok(telegram_id)
|
||||||
|
|
Loading…
Reference in a new issue