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.
|
2020-04-17 04:31:02 +02:00
|
|
|
|
|
|
|
use teloxide::{dispatching::update_listeners, prelude::*};
|
|
|
|
|
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;
|
|
|
|
use warp::Filter;
|
|
|
|
|
|
|
|
use reqwest::StatusCode;
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
run().await;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-07-26 19:47:02 +02:00
|
|
|
pub async fn webhook<'a>(bot: Bot) -> impl update_listeners::UpdateListener<Infallible> {
|
2020-04-17 04:31:02 +02:00
|
|
|
// Heroku defines 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);
|
|
|
|
let url = format!("https://{}/{}", host, path);
|
|
|
|
|
2020-05-26 12:42:04 +02:00
|
|
|
bot.set_webhook(url).send().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())
|
|
|
|
.map(move |json: serde_json::Value| {
|
|
|
|
let try_parse = match serde_json::from_str(&json.to_string()) {
|
|
|
|
Ok(update) => Ok(update),
|
|
|
|
Err(error) => {
|
|
|
|
log::error!(
|
|
|
|
"Cannot parse an update.\nError: {:?}\nValue: {}\n\
|
|
|
|
This is a bug in teloxide, please open an issue here: \
|
|
|
|
https://github.com/teloxide/teloxide/issues.",
|
|
|
|
error,
|
|
|
|
json
|
|
|
|
);
|
|
|
|
Err(error)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Ok(update) = try_parse {
|
2020-07-26 19:47:02 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
let serve = warp::serve(server);
|
|
|
|
|
|
|
|
let address = format!("0.0.0.0:{}", port);
|
|
|
|
tokio::spawn(serve.run(address.parse::<SocketAddr>().unwrap()));
|
|
|
|
rx
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run() {
|
|
|
|
teloxide::enable_logging!();
|
2020-07-25 15:45:57 +02:00
|
|
|
log::info!("Starting heroku_ping_pong_bot...");
|
2020-04-17 04:31:02 +02:00
|
|
|
|
|
|
|
let bot = Bot::from_env();
|
|
|
|
|
2020-07-17 12:04:25 +02:00
|
|
|
Dispatcher::new(bot.clone())
|
2020-04-17 04:31:02 +02:00
|
|
|
.messages_handler(|rx: DispatcherHandlerRx<Message>| {
|
|
|
|
rx.for_each(|message| async move {
|
2020-05-26 12:42:04 +02:00
|
|
|
message.answer_str("pong").await.log_on_error().await;
|
2020-04-17 04:31:02 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.dispatch_with_listener(
|
|
|
|
webhook(bot).await,
|
2020-07-26 19:47:02 +02:00
|
|
|
LoggingErrorHandler::with_custom_text("An error from the update listener"),
|
2020-04-17 04:31:02 +02:00
|
|
|
)
|
|
|
|
.await;
|
|
|
|
}
|