mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Add ping_bot example
This commit is contained in:
parent
3351838a36
commit
45a032116d
4 changed files with 71 additions and 1 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -2,3 +2,5 @@
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
.idea/
|
.idea/
|
||||||
|
.vscode/
|
||||||
|
examples/target
|
||||||
|
|
16
examples/Cargo.toml
Normal file
16
examples/Cargo.toml
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
[package]
|
||||||
|
name = "examples"
|
||||||
|
version = "0.0.0"
|
||||||
|
publish = false
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
teloxide = { path = "../" }
|
||||||
|
tokio = "0.2.6"
|
||||||
|
pretty_env_logger = "0.3"
|
||||||
|
futures = "0.3.1"
|
||||||
|
log = "0.4.8"
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "ping_bot"
|
||||||
|
path = "ping_bot.rs"
|
9
examples/README.md
Normal file
9
examples/README.md
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# Examples of how to use Teloxide.
|
||||||
|
|
||||||
|
All examples can be executed with:
|
||||||
|
|
||||||
|
```
|
||||||
|
cargo run --example $name
|
||||||
|
```
|
||||||
|
|
||||||
|
If you've got an example you'd like to see here, please feel free to open an issue. Otherwise if you've got an example you'd like to add, please feel free to make a PR!
|
43
examples/ping_bot.rs
Normal file
43
examples/ping_bot.rs
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
use futures::stream::StreamExt;
|
||||||
|
use teloxide::{
|
||||||
|
dispatching::{
|
||||||
|
private::Dispatcher, update_listeners::polling_default, SessionState,
|
||||||
|
},
|
||||||
|
requests::Request,
|
||||||
|
types::{Message, Update, UpdateKind},
|
||||||
|
Bot,
|
||||||
|
};
|
||||||
|
|
||||||
|
extern crate pretty_env_logger;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate log;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
pretty_env_logger::init();
|
||||||
|
let bot = &Bot::new("1061598315:AAErEDodTsrqD3UxA_EvFyEfXbKA6DT25G0");
|
||||||
|
let mut updater = Box::pin(polling_default(bot));
|
||||||
|
let handler = |s, upd: Update| async move {
|
||||||
|
match upd.kind {
|
||||||
|
UpdateKind::Message(m) => {
|
||||||
|
if m.text() == "ping" {
|
||||||
|
let msg = bot.send_message(m.chat.id, "pong");
|
||||||
|
}
|
||||||
|
msg.send().await.unwrap();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
SessionState::Continue(s)
|
||||||
|
};
|
||||||
|
let mut dp = Dispatcher::<'_, (), _>::new(handler);
|
||||||
|
info!("Starting the message handler.");
|
||||||
|
loop {
|
||||||
|
let u = updater.next().await.unwrap();
|
||||||
|
match u {
|
||||||
|
Err(e) => error!("{}", e),
|
||||||
|
Ok(u) => {
|
||||||
|
let _ = dp.dispatch(u).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue