#http-request #http-post #post-requests #web #http-parser #http

traq-bot-http

Library to parse POST requests of traQ BOT event

18 releases

0.10.1 Nov 9, 2024
0.9.1 Sep 14, 2024
0.9.0 Jul 9, 2024
0.8.2 Feb 11, 2024
0.4.0 Mar 11, 2023

#364 in HTTP server

Download history 42/week @ 2024-07-29 5/week @ 2024-08-19 1/week @ 2024-08-26 148/week @ 2024-09-09 67/week @ 2024-09-16 84/week @ 2024-09-23 38/week @ 2024-09-30 122/week @ 2024-10-07 50/week @ 2024-10-14 12/week @ 2024-10-21 24/week @ 2024-10-28 91/week @ 2024-11-04 33/week @ 2024-11-11

162 downloads per month

MIT license

155KB
2.5K SLoC

traq-bot-http-rs

Rust codecov Release docs.rs

GitHub Crates.io GitHub release (with filter) Crates.io

traQ BOTのPOSTリクエストをパースするライブラリです。

example

Cargo.toml

# ...

[dependencies]
http = "1"
tower = "0.4"
axum = "0.7"
tokio = { version = "1", features = ["full"] }
traq-bot-http = { version = "0.10.1", features = ["tower"] }

main.rs

use std::{env, net::SocketAddr};

use axum::{routing::post_service, Router};
use http::StatusCode;
use tokio::net::TcpListener;
use tower::service_fn;

use traq_bot_http::{payloads, RequestParser};

#[tokio::main]
async fn main() {
    let verification_token = env::var("VERIFICATION_TOKEN").unwrap();
    let parser = RequestParser::new(&verification_token);
    let handler = parser
        .into_handler()
        .on_message_created(service_fn(on_message_created));
    let app = Router::new().route(
        "/",
        post_service(handler).handle_error(|_| async { StatusCode::INTERNAL_SERVER_ERROR }),
    );
    let addr = SocketAddr::from(([127, 0, 0, 1], 8080));
    let server = TcpListener::bind(addr).await.unwrap();
    axum::serve(server, app).await.unwrap();
}

async fn on_message_created(
    payload: payloads::MessageCreatedPayload,
) -> Result<(), std::convert::Infallible> {
    print!(
        "{}さんがメッセージを投稿しました。\n内容: {}\n",
        payload.message.user.display_name, payload.message.text
    );
    Ok(())
}

Features

feature 機能 バージョン
uuid ペイロードのUUID値がuuid::Uuid型に v0.4.0から
time ペイロードのタイムスタンプ値(RFC3339 format)がtime::OffsetDateTime型に v0.5.0から
chrono ペイロードのタイムスタンプ値がchrono::DateTime<chrono::Utc>型に v0.6.0から
http http::Request型のサポート v0.10.0

timeよりもchronoの方が優先されます

Contributing

Issue, Pull Requestは大歓迎です。

Dependencies

~0.7–2.7MB
~53K SLoC