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
|
|
|
|
// ```
|
|
|
|
//
|
2022-06-09 16:00:15 +02:00
|
|
|
// [1]: https://github.com/emk/heroku-buildpack-rust
|
2020-04-17 04:31:02 +02:00
|
|
|
|
2022-06-09 16:00:15 +02:00
|
|
|
use std::env;
|
2022-04-01 12:26:58 +02:00
|
|
|
|
2022-06-09 16:00:15 +02:00
|
|
|
use teloxide::{dispatching::update_listeners::webhooks, prelude::*};
|
2020-04-17 04:31:02 +02:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2022-03-21 15:53:59 +01:00
|
|
|
pretty_env_logger::init();
|
2022-04-26 21:00:08 +02:00
|
|
|
log::info!("Starting Heroku ping-pong bot...");
|
2021-12-27 16:13:46 +01:00
|
|
|
|
2022-09-29 05:37:20 +02:00
|
|
|
let bot = Bot::from_env();
|
2021-12-27 16:13:46 +01:00
|
|
|
|
2021-05-22 00:15:32 +02:00
|
|
|
// Heroku auto defines a port value
|
2020-04-17 04:31:02 +02:00
|
|
|
let port: u16 = env::var("PORT")
|
2022-06-09 16:00:15 +02:00
|
|
|
.expect("PORT env variable is not set")
|
2020-04-17 04:31:02 +02:00
|
|
|
.parse()
|
2022-06-09 16:00:15 +02:00
|
|
|
.expect("PORT env variable value is not an integer");
|
2020-04-17 04:31:02 +02:00
|
|
|
|
2022-06-29 15:10:45 +02:00
|
|
|
let addr = ([0, 0, 0, 0], port).into();
|
2020-04-17 04:31:02 +02:00
|
|
|
|
2022-06-09 16:00:15 +02:00
|
|
|
// Heroku host example: "heroku-ping-pong-bot.herokuapp.com"
|
|
|
|
let host = env::var("HOST").expect("HOST env variable is not set");
|
2022-09-24 23:06:11 +02:00
|
|
|
let url = format!("https://{host}/webhook").parse().unwrap();
|
2020-04-17 04:31:02 +02:00
|
|
|
|
2022-06-09 16:00:15 +02:00
|
|
|
let listener = webhooks::axum(bot.clone(), webhooks::Options::new(addr, url))
|
|
|
|
.await
|
|
|
|
.expect("Couldn't setup webhook");
|
2020-04-17 04:31:02 +02:00
|
|
|
|
2022-06-09 16:00:15 +02:00
|
|
|
teloxide::repl_with_listener(
|
|
|
|
bot,
|
2022-10-03 13:54:06 +02:00
|
|
|
|bot: Bot, msg: Message| async move {
|
2022-06-09 16:00:15 +02:00
|
|
|
bot.send_message(msg.chat.id, "pong").await?;
|
2022-10-02 17:44:04 +02:00
|
|
|
Ok(())
|
2022-06-09 16:00:15 +02:00
|
|
|
},
|
|
|
|
listener,
|
|
|
|
)
|
|
|
|
.await;
|
2020-04-17 04:31:02 +02:00
|
|
|
}
|