2020-06-01 09:05:10 +02:00
|
|
|
// The version of Heroku ping-pong-bot, which uses a webhook to receive updates
|
|
|
|
// from Telegram, instead of long polling.
|
2022-02-04 19:31:13 +01:00
|
|
|
//
|
|
|
|
// You will need to configure the buildpack for heroku. We will be using Heroku
|
|
|
|
// rust buildpack [1]. Configuration was done by using heroku CLI.
|
|
|
|
//
|
|
|
|
// If you're creating a new Heroku application, run this:
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// heroku create --buildpack emk/rust
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// To set buildpack for existing applicaton:
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// heroku buildpacks:set emk/rust
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// [1] https://github.com/emk/heroku-buildpack-rust
|
2020-04-17 04:31:02 +02:00
|
|
|
|
2021-12-26 15:55:24 +01:00
|
|
|
use teloxide::{
|
|
|
|
dispatching::{
|
|
|
|
stop_token::AsyncStopToken,
|
|
|
|
update_listeners::{self, StatefulListener},
|
|
|
|
},
|
2022-01-06 13:50:59 +01:00
|
|
|
prelude2::*,
|
2021-12-26 15:55:24 +01:00
|
|
|
types::Update,
|
|
|
|
};
|
2020-04-17 04:31:02 +02:00
|
|
|
|
2020-07-17 12:04:25 +02:00
|
|
|
use std::{convert::Infallible, env, net::SocketAddr};
|
2020-04-17 04:31:02 +02:00
|
|
|
use tokio::sync::mpsc;
|
2021-03-13 17:13:24 +01:00
|
|
|
use tokio_stream::wrappers::UnboundedReceiverStream;
|
2020-04-17 04:31:02 +02:00
|
|
|
use warp::Filter;
|
|
|
|
|
2021-07-06 14:10:22 +02:00
|
|
|
use reqwest::{StatusCode, Url};
|
2020-04-17 04:31:02 +02:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2021-12-27 16:13:46 +01:00
|
|
|
teloxide::enable_logging!();
|
|
|
|
log::info!("Starting heroku_ping_pong_bot...");
|
|
|
|
|
|
|
|
let bot = Bot::from_env().auto_send();
|
|
|
|
|
2022-01-06 13:50:59 +01:00
|
|
|
teloxide::repls2::repl_with_listener(
|
2022-01-05 19:19:39 +01:00
|
|
|
bot.clone(),
|
2022-02-02 10:40:22 +01:00
|
|
|
|msg: Message, bot: AutoSend<Bot>| async move {
|
|
|
|
bot.send_message(msg.chat.id, "pong").await?;
|
2021-12-27 16:13:46 +01:00
|
|
|
respond(())
|
|
|
|
},
|
2022-01-05 19:19:39 +01:00
|
|
|
webhook(bot).await,
|
2021-12-27 16:13:46 +01:00
|
|
|
)
|
|
|
|
.await;
|
2020-04-17 04:31:02 +02:00
|
|
|
}
|
|
|
|
|
2020-07-26 19:47:02 +02:00
|
|
|
async fn handle_rejection(error: warp::Rejection) -> Result<impl warp::Reply, Infallible> {
|
2020-04-17 04:31:02 +02:00
|
|
|
log::error!("Cannot process the request due to: {:?}", error);
|
|
|
|
Ok(StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
}
|
|
|
|
|
2021-05-22 00:15:32 +02:00
|
|
|
pub async fn webhook(bot: AutoSend<Bot>) -> impl update_listeners::UpdateListener<Infallible> {
|
|
|
|
// Heroku auto defines a port value
|
2020-07-26 19:47:02 +02:00
|
|
|
let teloxide_token = env::var("TELOXIDE_TOKEN").expect("TELOXIDE_TOKEN env variable missing");
|
2020-04-17 04:31:02 +02:00
|
|
|
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();
|
2020-04-17 04:31:02 +02:00
|
|
|
|
2021-03-05 22:24:10 +01:00
|
|
|
bot.set_webhook(url).await.expect("Cannot setup a webhook");
|
2020-04-17 04:31:02 +02:00
|
|
|
|
|
|
|
let (tx, rx) = mpsc::unbounded_channel();
|
|
|
|
|
|
|
|
let server = warp::post()
|
|
|
|
.and(warp::path(path))
|
|
|
|
.and(warp::body::json())
|
2022-01-12 12:03:10 +01:00
|
|
|
.map(move |update: Update| {
|
|
|
|
tx.send(Ok(update)).expect("Cannot send an incoming update from the webhook");
|
2020-04-17 04:31:02 +02:00
|
|
|
|
|
|
|
StatusCode::OK
|
|
|
|
})
|
|
|
|
.recover(handle_rejection);
|
|
|
|
|
2021-05-22 00:15:32 +02:00
|
|
|
let (stop_token, stop_flag) = AsyncStopToken::new_pair();
|
2020-04-17 04:31:02 +02:00
|
|
|
|
2021-05-22 00:15:32 +02:00
|
|
|
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);
|
|
|
|
|
2021-12-26 15:55:24 +01:00
|
|
|
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()
|
|
|
|
})
|
2020-04-17 04:31:02 +02:00
|
|
|
}
|