teloxide/README.md

61 lines
1.9 KiB
Markdown
Raw Normal View History

2019-09-02 09:28:56 +02:00
<div align="center">
2020-01-06 11:25:37 +01:00
<img src="ICON.png" width="250"/>
2019-12-07 20:30:15 +01:00
<h1>teloxide</h1>
2019-12-07 20:30:15 +01:00
<a href="https://docs.rs/teloxide/">
2020-01-06 09:27:05 +01:00
<img src="https://img.shields.io/badge/docs.rs-v0.1.0-blue.svg">
2019-10-14 19:07:58 +02:00
</a>
2019-12-08 07:57:53 +01:00
<a href="https://github.com/teloxide/teloxide/actions">
<img src="https://github.com/teloxide/teloxide/workflows/Continuous%20integration/badge.svg">
2019-09-02 09:28:56 +02:00
</a>
2019-12-07 20:30:15 +01:00
<a href="https://crates.io/crates/teloxide">
2019-10-14 19:04:55 +02:00
<img src="https://img.shields.io/badge/crates.io-v0.1.0-orange.svg">
</a>
2019-10-14 19:10:41 +02:00
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.
2019-09-02 09:28:56 +02:00
</div>
2020-01-26 22:36:36 +01:00
2020-02-12 11:17:20 +01:00
## Getting started
2020-02-12 11:20:15 +01:00
1. Create a new bot using [@Botfather](https://t.me/botfather) to get a token in the format `123456789:blablabla`.
2020-02-12 11:47:51 +01:00
2. Initialise the `TELOXIDE_TOKEN` environmental variable to your token:
```bash
2020-02-12 11:49:59 +01:00
$ export TELOXIDE_TOKEN=MyAwesomeToken
2020-02-12 11:47:51 +01:00
```
2020-02-12 16:37:57 +01:00
3. Be sure that you are up to date:
2020-02-12 11:09:53 +01:00
```bash
$ rustup update stable
2020-01-26 22:36:36 +01:00
```
2020-02-12 11:09:53 +01:00
2020-02-12 16:37:57 +01:00
4. Execute `cargo new my_bot`, enter the directory and put these lines into your `Cargo.toml`:
2020-01-26 22:36:36 +01:00
```toml
2020-02-12 11:09:53 +01:00
[dependencies]
2020-01-26 22:36:36 +01:00
teloxide = "0.1.0"
2020-02-12 17:00:54 +01:00
log = "0.4.8"
2020-02-12 18:21:29 +01:00
tokio = "0.2.11"
2020-01-26 22:36:36 +01:00
```
2020-02-12 11:35:51 +01:00
## The ping-pong bot
2020-01-26 22:36:36 +01:00
```rust
2020-02-12 11:17:20 +01:00
use teloxide::prelude::*;
2020-01-26 22:36:36 +01:00
#[tokio::main]
async fn main() {
2020-02-13 09:50:01 +01:00
run().await;
}
async fn run() {
2020-02-13 15:03:23 +01:00
let bot = Bot::from_env().enable_logging(crate_name!()).build();
2020-02-13 09:50:01 +01:00
log::info!("Starting ping_pong_bot!");
2020-02-12 11:17:20 +01:00
2020-02-12 11:47:51 +01:00
// Create a dispatcher with a single message handler that answers "pong" to
// each incoming message.
Dispatcher::<RequestError>::new(bot)
2020-02-12 11:17:20 +01:00
.message_handler(&|ctx: DispatcherHandlerCtx<Message>| async move {
ctx.answer("pong").send().await?;
Ok(())
})
.dispatch()
.await;
2020-01-26 22:36:36 +01:00
}
```