-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "serialize" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
telers = { path = "../../telers", features = ["default"] } | ||
tokio = { version = "1.36", features = ["macros"] } | ||
tracing = "0.1" | ||
tracing-subscriber = { version = "0.3", features = ["env-filter"] } | ||
serde_json = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//! This example shows how to serialize Telegram types. | ||
//! | ||
//! You can run this example by setting `BOT_TOKEN` and optional `RUST_LOG` environment variable and running: | ||
//! ```bash | ||
//! RUST_LOG={log_level} BOT_TOKEN={your_bot_token} cargo run --package serialize | ||
//! ``` | ||
use telers::{ | ||
enums::{ParseMode, UpdateType}, | ||
errors::HandlerError, | ||
event::{telegram::HandlerResult, EventReturn, ToServiceProvider as _}, | ||
methods::SendMessage, | ||
types::Update, | ||
utils::text::{html_pre_language, html_quote}, | ||
Bot, Dispatcher, Router, | ||
}; | ||
|
||
use tracing::{event, Level}; | ||
use tracing_subscriber::{fmt, layer::SubscriberExt as _, util::SubscriberInitExt as _, EnvFilter}; | ||
|
||
async fn serialize_handler(bot: Bot, update: Update) -> HandlerResult { | ||
if let Some(chat_id) = update.chat_id() { | ||
match serde_json::to_string_pretty(&update) { | ||
Ok(text) => { | ||
bot.send( | ||
SendMessage::new(chat_id, html_pre_language(html_quote(text), "json")) | ||
.parse_mode(ParseMode::HTML), | ||
) | ||
.await?; | ||
} | ||
Err(err) => { | ||
bot.send(SendMessage::new( | ||
chat_id, | ||
format!("Serialize error :(\n\n{err:?}"), | ||
)) | ||
.await?; | ||
|
||
return Err(HandlerError::new(err)); | ||
} | ||
} | ||
} | ||
|
||
Ok(EventReturn::Finish) | ||
} | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() { | ||
tracing_subscriber::registry() | ||
.with(fmt::layer()) | ||
.with(EnvFilter::from_env("RUST_LOG")) | ||
.init(); | ||
|
||
let bot = Bot::from_env_by_key("BOT_TOKEN"); | ||
|
||
let mut router = Router::new("main"); | ||
router.update.register(serialize_handler); | ||
|
||
let dispatcher = Dispatcher::builder() | ||
.main_router(router) | ||
.bot(bot) | ||
.allowed_updates(UpdateType::all()) | ||
.build(); | ||
|
||
match dispatcher | ||
.to_service_provider_default() | ||
.unwrap() | ||
.run_polling() | ||
.await | ||
{ | ||
Ok(()) => event!(Level::INFO, "Bot stopped"), | ||
Err(err) => event!(Level::ERROR, error = %err, "Bot stopped"), | ||
} | ||
} |