From d6d12540a3bf365ff01802bb688a690694c6ed33 Mon Sep 17 00:00:00 2001 From: nanato12 Date: Tue, 28 Nov 2023 19:02:26 +0900 Subject: [PATCH] update sample --- sample/src/main.rs | 117 +++++++++++++++++++++++++++++++++++++-------- sample/src/sub.rs | 25 ++++++++++ 2 files changed, 123 insertions(+), 19 deletions(-) create mode 100644 sample/src/sub.rs diff --git a/sample/src/main.rs b/sample/src/main.rs index ea34a3a1..ae952214 100644 --- a/sample/src/main.rs +++ b/sample/src/main.rs @@ -1,25 +1,104 @@ -use line_bot_sdk_rust_openapi::messaging_api::{ - apis::{ - configuration::Configuration, - messaging_api_api::{reply_message, ReplyMessageParams}, +extern crate line_bot_sdk_rust_openapi as line; + +use actix_web::{ + error::ErrorBadRequest, middleware, post, web, App, Error, HttpRequest, HttpResponse, + HttpServer, +}; +use dotenv::dotenv; +use line::{ + parser::signature::validate_signature, + support::actix::Signature, + webhook::models::{ + CallbackRequest, DeliveryContext, Event, EventMode, GroupSource, MessageContent, + MessageEvent, Source, TextMessageContent, }, - models::{Message, ReplyMessageRequest, TextMessage}, }; +use std::env; + +#[post("/callback")] +async fn callback( + signature: Signature, + req: HttpRequest, + bytes: web::Bytes, +) -> Result { + // Get channel secret and access token by environment variable + let channel_secret: &str = + &env::var("LINE_CHANNEL_SECRET").expect("Failed getting LINE_CHANNEL_SECRET"); + let access_token: &str = + &env::var("LINE_CHANNEL_ACCESS_TOKEN").expect("Failed getting LINE_CHANNEL_ACCESS_TOKEN"); + + println!("channel_secret: {channel_secret:?}"); + println!("access_token: {access_token:?}"); + println!("signature: {signature:?}"); -#[tokio::main] -async fn main() { - let text_message = TextMessage::new("text".to_string(), "message".to_string()); - let params = ReplyMessageParams { - reply_message_request: ReplyMessageRequest { - reply_token: "a".to_string(), - messages: vec![Message::Text(text_message)], - notification_disabled: Some(false), - }, + let body: &str = &String::from_utf8(bytes.to_vec()).unwrap(); + println!("body: {body:?}"); + + let r = CallbackRequest { + destination: "U078cd692e67a90a66af06d18865830e3".to_string(), + events: vec![Event::MessageEvent(MessageEvent { + timestamp: 1, + mode: EventMode::Active, + webhook_event_id: "01HGACPYQE97R407J4AP7BZHB8".to_string(), + source: Some(Box::new( + Source::GroupSource( + GroupSource{ + group_id: "C4e256f4c52df27c374275bb35f4e8e38".to_string(), + user_id: Some("Ue10d267e7ad66d524781ccf16ca6ddbd".to_string()), + }) + )), + delivery_context: Box::new(DeliveryContext { + is_redelivery: false, + }), + reply_token: Some("3aa2f1b85ca8426c9c39f4f99f812329".to_string()), + message: Box::new(MessageContent::TextMessageContent(TextMessageContent { + id: "483728449715306614".to_string(), + text: "ら".to_string(), + emojis: None, + mention: None, + quote_token: "RBaLgQIeMAHBP-o96z6deIxus28rLKH7dvCl2xp2uFHB8-ubJNQmPwY3On5vfRs04V0yMituk5qDgcSkZMA24_em2Iu0mimIJgH0gOPkzxQ4Q-u0f06iYqUuFp5W-LkXvmvyZaE85dno-tOsKeWv-w".to_string(), + quoted_message_id: None, + })), + })], }; - let result = reply_message(&Configuration::default(), params).await; - match result { - Ok(r) => println!("{:#?}", r), - Err(e) => println!("{:#?}", e), + + let json_string = serde_json::to_string(&r)?; + println!("{json_string:#?}"); + + let deserialized_request: CallbackRequest = serde_json::from_str(&json_string)?; + println!("{deserialized_request:#?}"); + + if !validate_signature(channel_secret, &signature.key, body) { + return Err(ErrorBadRequest("x-line-signature is invalid.")); } - println!("Hello, world!"); + + let request: Result = serde_json::from_str(body); + match request { + Ok(req) => println!("REQ: {req:?}"), + Err(err) => return Err(ErrorBadRequest(err.to_string())), + } + + println!("REQ: {req:?}"); + Ok(HttpResponse::Ok().body("ok")) +} + +async fn index(req: HttpRequest) -> &'static str { + println!("REQ: {req:?}"); + "Hello world!" +} + +#[actix_web::main] +async fn main() -> std::io::Result<()> { + dotenv().ok(); + HttpServer::new(|| { + App::new() + // enable logger + .wrap(middleware::Logger::default()) + .service(web::resource("/index.html").to(|| async { "Hello world!" })) + .service(web::resource("/").to(index)) + .service(callback) + }) + .bind(("127.0.0.1", 8080))? + .run() + .await } diff --git a/sample/src/sub.rs b/sample/src/sub.rs new file mode 100644 index 00000000..ea34a3a1 --- /dev/null +++ b/sample/src/sub.rs @@ -0,0 +1,25 @@ +use line_bot_sdk_rust_openapi::messaging_api::{ + apis::{ + configuration::Configuration, + messaging_api_api::{reply_message, ReplyMessageParams}, + }, + models::{Message, ReplyMessageRequest, TextMessage}, +}; + +#[tokio::main] +async fn main() { + let text_message = TextMessage::new("text".to_string(), "message".to_string()); + let params = ReplyMessageParams { + reply_message_request: ReplyMessageRequest { + reply_token: "a".to_string(), + messages: vec![Message::Text(text_message)], + notification_disabled: Some(false), + }, + }; + let result = reply_message(&Configuration::default(), params).await; + match result { + Ok(r) => println!("{:#?}", r), + Err(e) => println!("{:#?}", e), + } + println!("Hello, world!"); +}