mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-08 19:33:53 +01:00
Update README.md
This commit is contained in:
parent
c4ccf76455
commit
8f1c3d4f4e
1 changed files with 17 additions and 35 deletions
52
README.md
52
README.md
|
@ -17,7 +17,7 @@
|
||||||
A full-featured framework that empowers you to easily build [Telegram bots](https://telegram.org/blog/bot-revolution) using the [`async`/`.await`](https://rust-lang.github.io/async-book/01_getting_started/01_chapter.html) syntax in [Rust](https://www.rust-lang.org/). It handles all the difficult stuff so you can focus only on your business logic.
|
A full-featured framework that empowers you to easily build [Telegram bots](https://telegram.org/blog/bot-revolution) using the [`async`/`.await`](https://rust-lang.github.io/async-book/01_getting_started/01_chapter.html) syntax in [Rust](https://www.rust-lang.org/). It handles all the difficult stuff so you can focus only on your business logic.
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
# Getting started
|
## Getting started
|
||||||
1. Be sure that you are up to date:
|
1. Be sure that you are up to date:
|
||||||
```bash
|
```bash
|
||||||
$ rustup update stable
|
$ rustup update stable
|
||||||
|
@ -29,44 +29,26 @@ $ rustup update stable
|
||||||
teloxide = "0.1.0"
|
teloxide = "0.1.0"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Writing first bot
|
## Writing your first bot
|
||||||
First, create bot with [@botfather](https://t.me/botfather). After creating, botfather give you
|
First, create a new bot using [@botfather](https://t.me/botfather), and after that you'll get a token in format `123456789:somemanyletters`.
|
||||||
token in format `123456789:somemanyletters`.
|
|
||||||
|
|
||||||
Next, open yout `main.rs` file. Let's create a simple echo bot:
|
Next, open `main.rs` file, because we're gonna write a ping-pong-bot!
|
||||||
```rust
|
```rust
|
||||||
use futures::stream::StreamExt;
|
use teloxide::prelude::*;
|
||||||
use teloxide::{
|
|
||||||
dispatching::{
|
|
||||||
chat::{ChatUpdate, ChatUpdateKind, Dispatcher},
|
|
||||||
update_listeners::polling_default,
|
|
||||||
SessionState,
|
|
||||||
},
|
|
||||||
requests::Request,
|
|
||||||
Bot,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let bot = &Bot::new("1061598315:AAErEDodTsrqD3UxA_EvFyEfXbKA6DT25G0");
|
std::env::set_var("RUST_LOG", "ping_pong_bot=trace");
|
||||||
let mut updater = Box::pin(polling_default(bot));
|
std::env::set_var("RUST_LOG", "teloxide=error");
|
||||||
let handler = |_, upd: ChatUpdate| async move {
|
pretty_env_logger::init();
|
||||||
if let ChatUpdateKind::Message(m) = upd.kind {
|
log::info!("Starting the ping-pong bot!");
|
||||||
let msg = bot.send_message(m.chat.id, m.text);
|
|
||||||
msg.send().await.unwrap();
|
Dispatcher::<RequestError>::new(Bot::new("MyAwesomeToken"))
|
||||||
}
|
.message_handler(&|ctx: DispatcherHandlerCtx<Message>| async move {
|
||||||
SessionState::Continue(())
|
ctx.answer("pong").send().await?;
|
||||||
};
|
Ok(())
|
||||||
let mut dp = Dispatcher::<'_, (), _>::new(handler);
|
})
|
||||||
println!("Starting the message handler.");
|
.dispatch()
|
||||||
loop {
|
.await;
|
||||||
let u = updater.next().await.unwrap();
|
|
||||||
match u {
|
|
||||||
Err(e) => eprintln!("Error: {}", e),
|
|
||||||
Ok(u) => {
|
|
||||||
let _ = dp.dispatch(u).await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in a new issue