teloxide/examples/heroku_ping_pong_bot/src/main.rs

96 lines
2.8 KiB
Rust
Raw Normal View History

// The version of Heroku 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,
};
2020-07-17 12:04:25 +02:00
use std::{convert::Infallible, env, net::SocketAddr};
use tokio::sync::mpsc;
2021-03-13 17:13:24 +01:00
use tokio_stream::wrappers::UnboundedReceiverStream;
use warp::Filter;
2021-07-06 14:10:22 +02:00
use reqwest::{StatusCode, Url};
#[tokio::main]
async fn main() {
run().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);
2021-07-06 14:10:22 +02:00
let url = Url::parse(&format!("https://{}/{}", host, path)).unwrap();
2021-03-05 22:24:10 +01:00
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 |json: serde_json::Value| {
2021-03-13 17:13:24 +01:00
if let Ok(update) = Update::try_parse(&json) {
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()
})
}
async fn run() {
teloxide::enable_logging!();
log::info!("Starting heroku_ping_pong_bot...");
2021-03-05 22:24:10 +01:00
let bot = Bot::from_env().auto_send();
let cloned_bot = bot.clone();
teloxide::repl_with_listener(
bot,
|message| async move {
2021-03-05 22:24:10 +01:00
message.answer("pong").await?;
2021-03-13 17:13:24 +01:00
respond(())
},
webhook(cloned_bot).await,
)
.await;
}