Add ping_bot example

This commit is contained in:
Sergey Levitin 2020-01-11 16:02:49 +03:00
parent 3351838a36
commit 45a032116d
4 changed files with 71 additions and 1 deletions

4
.gitignore vendored
View file

@ -1,4 +1,6 @@
/target
**/*.rs.bk
Cargo.lock
.idea/
.idea/
.vscode/
examples/target

16
examples/Cargo.toml Normal file
View 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
View 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
View 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;
}
}
}
}