|
1 | 1 | # LINE Messaging API SDK for Rust |
| 2 | + |
2 | 3 | ## Introduction |
| 4 | + |
3 | 5 | The LINE Messaging API SDK for Rust makes it easy to develop bots using LINE Messaging API, and you can create a sample bot within minutes. |
| 6 | + |
4 | 7 | ## Documentation |
| 8 | + |
5 | 9 | See the official API documentation for more information. |
| 10 | + |
6 | 11 | - English: <https://developers.line.biz/en/docs/messaging-api/overview/> |
7 | 12 | - Japanese: <https://developers.line.biz/ja/docs/messaging-api/overview/> |
| 13 | + |
8 | 14 | ## Requirements |
9 | | -This library requires Rust nightly. |
| 15 | + |
| 16 | +This library requires stable/beta Rust. |
| 17 | + |
10 | 18 | ## Installation |
11 | | -``` |
| 19 | + |
| 20 | +```toml |
12 | 21 | [dependencies] |
13 | | -line-bot-sdk-rust = "0.1" |
| 22 | +line-bot-sdk-rust = "1.0.0" |
14 | 23 | ``` |
| 24 | + |
15 | 25 | If you use `rocket support`. |
| 26 | + |
| 27 | +```toml |
| 28 | +[dependencies.line-bot-sdk-rust] |
| 29 | +version = "1.0.0" |
| 30 | +features = ["rocket_support"] |
16 | 31 | ``` |
17 | | -[dependencies] |
18 | | -line-bot-sdk-rust = { version = "0.1", features = ["rocket_support"] } |
19 | | -``` |
| 32 | + |
20 | 33 | If you use `actix_web support`. |
| 34 | + |
| 35 | +```toml |
| 36 | +[dependencies.line-bot-sdk-rust] |
| 37 | +version = "1.0.0" |
| 38 | +features = ["actix_support"] |
21 | 39 | ``` |
22 | | -[dependencies] |
23 | | -line-bot-sdk-rust = { version = "0.1", features = ["actix_support"] } |
24 | | -``` |
| 40 | + |
25 | 41 | ## Configuration |
26 | | -``` |
| 42 | + |
| 43 | +```rust |
27 | 44 | extern crate line_bot_sdk_rust as line; |
28 | | -use line::bot::LineBot; |
| 45 | +use line::messaging_api::apis::configuration::Configuration; |
| 46 | +use std::env; |
29 | 47 |
|
30 | 48 | fn main() { |
31 | | - let bot = LineBot::new("<channel secret>", "<channel access token>"); |
| 49 | + let access_token: &str = |
| 50 | + &env::var("LINE_CHANNEL_ACCESS_TOKEN").expect("Failed getting LINE_CHANNEL_ACCESS_TOKEN"); |
| 51 | + |
| 52 | + let mut conf = Configuration::default(); |
| 53 | + conf.bearer_access_token = Some(access_token.to_string()); |
32 | 54 | } |
33 | 55 | ``` |
| 56 | + |
34 | 57 | ## How to use |
| 58 | + |
35 | 59 | The LINE Messaging API uses the JSON data format. |
36 | | -parse_event_request() will help you to parse the HttpRequest content and return a Result<[Events](`events::Events`) , &'static str> Object. |
37 | | -``` |
38 | | - let result: Result<Events, &'static str> = |
39 | | - bot.parse_event_request(signature, body); |
40 | | -``` |
41 | 60 |
|
| 61 | +Parse body (`&str`) into Result<CallbackRequest, serde_json::Error>. |
| 62 | + |
| 63 | +```rust |
| 64 | +let request: Result<CallbackRequest, serde_json::Error> = serde_json::from_str(body); |
42 | 65 | ``` |
43 | | -match result { |
44 | | - Ok(events) => { |
45 | | - for event in events.events { |
46 | | - ... |
| 66 | + |
| 67 | +```rust |
| 68 | +match request { |
| 69 | + Err(err) => { |
| 70 | + // error handling |
| 71 | + }, |
| 72 | + Ok(req) => { |
| 73 | + for e in req.events { |
| 74 | + // Processing for various events |
47 | 75 | } |
48 | 76 | } |
49 | | - Err(msg) => {} |
50 | 77 | } |
51 | 78 | ``` |
52 | 79 |
|
53 | 80 | ## EchoBot examples |
54 | 81 |
|
55 | | -**with Rocket framework** |
| 82 | +### with Rocket framework |
56 | 83 |
|
57 | 84 | ```bash |
58 | | -cargo run --example echobot_rocket --features=rocket_support |
| 85 | +$ cd examples |
| 86 | +$ cargo run --bin rocket |
59 | 87 | ``` |
60 | 88 |
|
61 | | -source: [rocket example](./examples/echobot_rocket.rs) |
| 89 | +source: [rocket example](./examples/rocket/src/main.rs) |
62 | 90 |
|
63 | | -**with Actix_web framework** |
| 91 | +### with actix_web framework |
64 | 92 |
|
65 | 93 | ```bash |
66 | | -cargo run --example echobot_actix_web --features=actix_support |
| 94 | +$ cd examples |
| 95 | +$ cargo run --bin actix_web |
67 | 96 | ``` |
68 | 97 |
|
69 | | -source: [actix_web example](./examples/echobot_actix_web.rs) |
| 98 | +source: [actix_web example](./examples/actix_web/src/main.rs) |
70 | 99 |
|
71 | 100 | ## Contributing |
| 101 | + |
72 | 102 | Please make a contribution 😆 |
73 | 103 |
|
74 | 104 | ## License |
75 | | -``` |
76 | | -Copyright 2021 nanato12 |
| 105 | + |
| 106 | +```plain |
| 107 | +Copyright 2023 nanato12 |
77 | 108 |
|
78 | 109 | Licensed under the Apache License, Version 2.0 (the "License"); |
79 | 110 | you may not use this file except in compliance with the License. |
|
0 commit comments