This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
forked from teloxide/teloxide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heroku_ping_pong.rs
57 lines (48 loc) · 1.56 KB
/
heroku_ping_pong.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// The version of Heroku ping-pong-bot, which uses a webhook to receive updates
// from Telegram, instead of long polling.
//
// 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 application:
//
// ```
// heroku buildpacks:set emk/rust
// ```
//
// [1]: https://github.com/emk/heroku-buildpack-rust
use std::env;
use teloxide::{dispatching::update_listeners::webhooks, prelude::*};
#[tokio::main]
async fn main() {
pretty_env_logger::init();
log::info!("Starting Heroku ping-pong bot...");
let bot = Bot::from_env();
// 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 = ([0, 0, 0, 0], 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 = format!("https://{host}/webhook").parse().unwrap();
let listener = webhooks::axum(bot.clone(), webhooks::Options::new(addr, url))
.await
.expect("Couldn't setup webhook");
teloxide::repl_with_listener(
bot,
|bot: Bot, msg: Message| async move {
bot.send_message(msg.chat.id, "pong").await?;
Ok(())
},
listener,
)
.await;
}