mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Use new built-in webhooks in ngrok example
I've also changed the port from 80 to 8443, since 80 usually requires root privileges.
This commit is contained in:
parent
d576d829f1
commit
37d1252bc9
1 changed files with 9 additions and 61 deletions
|
@ -1,21 +1,7 @@
|
|||
// The version of ngrok ping-pong-bot, which uses a webhook to receive updates
|
||||
// from Telegram, instead of long polling.
|
||||
|
||||
use teloxide::{
|
||||
dispatching::{
|
||||
stop_token::AsyncStopToken,
|
||||
update_listeners::{self, StatefulListener},
|
||||
},
|
||||
prelude::*,
|
||||
types::Update,
|
||||
};
|
||||
|
||||
use std::{convert::Infallible, net::SocketAddr};
|
||||
use tokio::sync::mpsc;
|
||||
use tokio_stream::wrappers::UnboundedReceiverStream;
|
||||
use warp::Filter;
|
||||
|
||||
use reqwest::{StatusCode, Url};
|
||||
use teloxide::{dispatching::update_listeners::webhooks, prelude::*};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
@ -24,57 +10,19 @@ async fn main() {
|
|||
|
||||
let bot = Bot::from_env().auto_send();
|
||||
|
||||
let addr = ([127, 0, 0, 1], 8443).into();
|
||||
let url = "Your HTTPS ngrok URL here. Get it by `ngrok http 8443`".parse().unwrap();
|
||||
let listener = webhooks::axum(bot.clone(), webhooks::Options::new(addr, url))
|
||||
.await
|
||||
.expect("Couldn't setup webhook");
|
||||
|
||||
teloxide::repl_with_listener(
|
||||
bot.clone(),
|
||||
bot,
|
||||
|msg: Message, bot: AutoSend<Bot>| async move {
|
||||
bot.send_message(msg.chat.id, "pong").await?;
|
||||
respond(())
|
||||
},
|
||||
webhook(bot).await,
|
||||
listener,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
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(bot: AutoSend<Bot>) -> impl update_listeners::UpdateListener<Infallible> {
|
||||
let url = Url::parse("Your HTTPS ngrok URL here. Get it by `ngrok http 80`").unwrap();
|
||||
|
||||
// You might want to specify a self-signed certificate via .certificate
|
||||
// method on SetWebhook.
|
||||
bot.set_webhook(url).await.expect("Cannot setup a webhook");
|
||||
|
||||
let (tx, rx) = mpsc::unbounded_channel();
|
||||
|
||||
let server = warp::post()
|
||||
.and(warp::body::json())
|
||||
.map(move |update: Update| {
|
||||
tx.send(Ok(update)).expect("Cannot send an incoming update from the webhook");
|
||||
|
||||
StatusCode::OK
|
||||
})
|
||||
.recover(handle_rejection);
|
||||
|
||||
let (stop_token, stop_flag) = AsyncStopToken::new_pair();
|
||||
|
||||
let addr = "127.0.0.1:80".parse::<SocketAddr>().unwrap();
|
||||
let server = warp::serve(server);
|
||||
let (_addr, fut) = server.bind_with_graceful_shutdown(addr, stop_flag);
|
||||
|
||||
// You might want to use serve.key_path/serve.cert_path methods here to
|
||||
// setup a self-signed TLS certificate.
|
||||
|
||||
tokio::spawn(fut);
|
||||
let stream = UnboundedReceiverStream::new(rx);
|
||||
|
||||
fn streamf<S, T>(state: &mut (S, T)) -> &mut S {
|
||||
&mut state.0
|
||||
}
|
||||
|
||||
StatefulListener::new((stream, stop_token), streamf, |state: &mut (_, AsyncStopToken)| {
|
||||
state.1.clone()
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue