-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.rs
72 lines (63 loc) · 2.51 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use dotenv::dotenv;
use line_bot_sdk_rust::{
client::LINE,
line_messaging_api::{
apis::MessagingApiApi,
models::{Message, ReplyMessageRequest, TextMessage},
},
parser::signature::validate_signature,
support::rocket::Signature,
line_webhook::models::{CallbackRequest, Event, MessageContent},
};
use rocket::{http::Status, launch, post, routes};
use std::env;
#[post("/callback", data = "<body>")]
async fn world(signature: Signature, body: String) -> (Status, &'static str) {
// Get channel secret and access token by environment variable
let channel_secret: &str =
&env::var("LINE_CHANNEL_SECRET").expect("Failed to get LINE_CHANNEL_SECRET");
let access_token: &str =
&env::var("LINE_CHANNEL_ACCESS_TOKEN").expect("Failed to get LINE_CHANNEL_ACCESS_TOKEN");
let _line = LINE::new(access_token.to_string());
println!("{signature:#?}");
println!("{body:#?}");
if !validate_signature(channel_secret, &signature.key, &body) {
return (Status::BadRequest, "x-line-signature is invalid.");
}
let request: Result<CallbackRequest, serde_json::Error> = serde_json::from_str(&body);
match request {
Err(_) => {}
Ok(req) => {
println!("req: {req:#?}");
for e in req.events {
if let Event::MessageEvent(message_event) = e {
if let MessageContent::TextMessageContent(text_message) = *message_event.message
{
let _reply_message_request = ReplyMessageRequest {
reply_token: message_event.reply_token.unwrap(),
messages: vec![Message::Text(TextMessage::new(text_message.text))],
notification_disabled: Some(false),
};
// TODO: reply_message sample
// let result = line
// .messaging_api_client
// .reply_message(reply_message_request)
// .await;
// match result {
// Ok(r) => println!("{:#?}", r),
// Err(e) => println!("{:#?}", e),
// }
};
};
}
}
}
(Status::Ok, "OK")
}
#[launch]
async fn rocket() -> _ {
dotenv().ok();
rocket::build()
.mount("/", routes![world])
.configure(rocket::Config::figment().merge(("port", 8080)))
}