Skip to content

Commit

Permalink
update sample
Browse files Browse the repository at this point in the history
  • Loading branch information
nanato12 committed Nov 28, 2023
1 parent 9375fe8 commit d6d1254
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 19 deletions.
117 changes: 98 additions & 19 deletions sample/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<HttpResponse, Error> {
// 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<CallbackRequest, serde_json::Error> = 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
}
25 changes: 25 additions & 0 deletions sample/src/sub.rs
Original file line number Diff line number Diff line change
@@ -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!");
}

0 comments on commit d6d1254

Please sign in to comment.