mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
use built-in webhook support in heroku example
This commit is contained in:
parent
c4d456a432
commit
0b582882eb
2 changed files with 27 additions and 72 deletions
|
@ -99,7 +99,6 @@ once_cell = "1.9.0"
|
||||||
serde = "1"
|
serde = "1"
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
tokio = { version = "1.8", features = ["fs", "rt-multi-thread", "macros"] }
|
tokio = { version = "1.8", features = ["fs", "rt-multi-thread", "macros"] }
|
||||||
warp = "0.3.0"
|
|
||||||
reqwest = "0.10.4"
|
reqwest = "0.10.4"
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
tokio-stream = "0.1"
|
tokio-stream = "0.1"
|
||||||
|
@ -153,6 +152,10 @@ required-features = ["macros"]
|
||||||
name = "ngrok_ping_pong"
|
name = "ngrok_ping_pong"
|
||||||
required-features = ["webhooks-axum"]
|
required-features = ["webhooks-axum"]
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "heroku_ping_pong"
|
||||||
|
required-features = ["webhooks-axum"]
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "purchase"
|
name = "purchase"
|
||||||
required-features = ["macros"]
|
required-features = ["macros"]
|
||||||
|
|
|
@ -16,25 +16,12 @@
|
||||||
// heroku buildpacks:set emk/rust
|
// heroku buildpacks:set emk/rust
|
||||||
// ```
|
// ```
|
||||||
//
|
//
|
||||||
// [1] https://github.com/emk/heroku-buildpack-rust
|
// [1]: https://github.com/emk/heroku-buildpack-rust
|
||||||
|
|
||||||
// TODO: use built-in webhook support
|
use std::env;
|
||||||
|
|
||||||
use teloxide::{
|
use teloxide::{dispatching::update_listeners::webhooks, prelude::*};
|
||||||
dispatching::{
|
use url::Url;
|
||||||
stop_token::AsyncStopToken,
|
|
||||||
update_listeners::{self, StatefulListener},
|
|
||||||
},
|
|
||||||
prelude::*,
|
|
||||||
types::Update,
|
|
||||||
};
|
|
||||||
|
|
||||||
use std::{convert::Infallible, env, net::SocketAddr};
|
|
||||||
use tokio::sync::mpsc;
|
|
||||||
use tokio_stream::wrappers::UnboundedReceiverStream;
|
|
||||||
use warp::Filter;
|
|
||||||
|
|
||||||
use reqwest::{StatusCode, Url};
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
@ -42,66 +29,31 @@ async fn main() {
|
||||||
log::info!("Starting Heroku ping-pong bot...");
|
log::info!("Starting Heroku ping-pong bot...");
|
||||||
|
|
||||||
let bot = Bot::from_env().auto_send();
|
let bot = Bot::from_env().auto_send();
|
||||||
|
let token = bot.inner().token();
|
||||||
|
|
||||||
|
// Heroku auto defines a port value
|
||||||
|
let port: u16 = env::var("PORT")
|
||||||
|
.expect("PORT env variable is not set")
|
||||||
|
.parse()
|
||||||
|
.expect("PORT env variable value is not an integer");
|
||||||
|
|
||||||
|
let addr = ([127, 0, 0, 1], port).into();
|
||||||
|
|
||||||
|
// Heroku host example: "heroku-ping-pong-bot.herokuapp.com"
|
||||||
|
let host = env::var("HOST").expect("HOST env variable is not set");
|
||||||
|
let url = Url::parse(&format!("https://{host}/webhooks/{token}")).unwrap();
|
||||||
|
|
||||||
|
let listener = webhooks::axum(bot.clone(), webhooks::Options::new(addr, url))
|
||||||
|
.await
|
||||||
|
.expect("Couldn't setup webhook");
|
||||||
|
|
||||||
teloxide::repl_with_listener(
|
teloxide::repl_with_listener(
|
||||||
bot.clone(),
|
bot,
|
||||||
|msg: Message, bot: AutoSend<Bot>| async move {
|
|msg: Message, bot: AutoSend<Bot>| async move {
|
||||||
bot.send_message(msg.chat.id, "pong").await?;
|
bot.send_message(msg.chat.id, "pong").await?;
|
||||||
respond(())
|
respond(())
|
||||||
},
|
},
|
||||||
webhook(bot).await,
|
listener,
|
||||||
)
|
)
|
||||||
.await;
|
.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> {
|
|
||||||
// Heroku auto defines a port value
|
|
||||||
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()
|
|
||||||
.expect("PORT value to be integer");
|
|
||||||
// Heroku host example .: "heroku-ping-pong-bot.herokuapp.com"
|
|
||||||
let host = env::var("HOST").expect("have HOST env variable");
|
|
||||||
let path = format!("bot{teloxide_token}");
|
|
||||||
let url = Url::parse(&format!("https://{host}/{path}")).unwrap();
|
|
||||||
|
|
||||||
bot.set_webhook(url).await.expect("Cannot setup a webhook");
|
|
||||||
|
|
||||||
let (tx, rx) = mpsc::unbounded_channel();
|
|
||||||
|
|
||||||
let server = warp::post()
|
|
||||||
.and(warp::path(path))
|
|
||||||
.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 = format!("0.0.0.0:{port}").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