Update all the examples

This commit is contained in:
Temirkhan Myrzamadi 2020-05-26 16:42:04 +06:00
parent da1762039d
commit 75baa88c7e
6 changed files with 18 additions and 15 deletions

View file

@ -11,7 +11,7 @@ log = "0.4.8"
futures = "0.3.4"
tokio = "0.2.9"
pretty_env_logger = "0.4.0"
teloxide = "0.2.0"
teloxide = { path = "../../" }
# Used to setup a webhook
warp = "0.2.2"

View file

@ -14,14 +14,19 @@ async fn main() {
run().await;
}
async fn handle_rejection(error: warp::Rejection) -> Result<impl warp::Reply, Infallible> {
async fn handle_rejection(
error: warp::Rejection,
) -> Result<impl warp::Reply, Infallible> {
log::error!("Cannot process the request due to: {:?}", error);
Ok(StatusCode::INTERNAL_SERVER_ERROR)
}
pub async fn webhook<'a>(bot: Arc<Bot>) -> impl update_listeners::UpdateListener<Infallible> {
pub async fn webhook<'a>(
bot: Arc<Bot>,
) -> impl update_listeners::UpdateListener<Infallible> {
// Heroku defines auto defines a port value
let teloxide_token = env::var("TELOXIDE_TOKEN").expect("TELOXIDE_TOKEN env variable missing");
let teloxide_token = env::var("TELOXIDE_TOKEN")
.expect("TELOXIDE_TOKEN env variable missing");
let port: u16 = env::var("PORT")
.expect("PORT env variable missing")
.parse()
@ -31,10 +36,7 @@ pub async fn webhook<'a>(bot: Arc<Bot>) -> impl update_listeners::UpdateListener
let path = format!("bot{}", teloxide_token);
let url = format!("https://{}/{}", host, path);
bot.set_webhook(url)
.send()
.await
.expect("Cannot setup a webhook");
bot.set_webhook(url).send().await.expect("Cannot setup a webhook");
let (tx, rx) = mpsc::unbounded_channel();
@ -80,12 +82,14 @@ async fn run() {
Dispatcher::new(Arc::clone(&bot))
.messages_handler(|rx: DispatcherHandlerRx<Message>| {
rx.for_each(|message| async move {
message.answer("pong").send().await.log_on_error().await;
message.answer_str("pong").await.log_on_error().await;
})
})
.dispatch_with_listener(
webhook(bot).await,
LoggingErrorHandler::with_custom_text("An error from the update listener"),
LoggingErrorHandler::with_custom_text(
"An error from the update listener",
),
)
.await;
}

View file

@ -16,7 +16,7 @@ async fn run() {
Dispatcher::new(bot)
.messages_handler(|rx: DispatcherHandlerRx<Message>| {
rx.for_each(|message| async move {
message.answer("pong").send().await.log_on_error().await;
message.answer_str("pong").await.log_on_error().await;
})
})
.dispatch()

View file

@ -26,11 +26,10 @@ async fn run() {
let previous = MESSAGES_TOTAL.fetch_add(1, Ordering::Relaxed);
message
.answer(format!(
.answer_str(format!(
"I received {} messages in total.",
previous
))
.send()
.await
.log_on_error()
.await;

View file

@ -18,7 +18,7 @@ fn generate() -> String {
}
async fn answer(
cx: DispatcherHandlerCx<Message>,
cx: UpdateWithCx<Message>,
command: Command,
) -> ResponseResult<()> {
match command {

View file

@ -63,7 +63,7 @@ async fn run() {
Dispatcher::new(Arc::clone(&bot))
.messages_handler(|rx: DispatcherHandlerRx<Message>| {
rx.for_each(|message| async move {
message.answer("pong").send().await.log_on_error().await;
message.answer_str("pong").await.log_on_error().await;
})
})
.dispatch_with_listener(