line-bot-sdk-rust icon indicating copy to clipboard operation
line-bot-sdk-rust copied to clipboard

Failed to get_profile and then push_message

Open axblueblader opened this issue 4 years ago • 9 comments

First off, thanks for developing this library. When using it I encountered this problem:

I have some code that looks like this. All info are valid. The bot.push_message call will fail with 400. If I remove the bot.get_profile call then the bot.push_message call will work correctly.

    let line_id = "UXXXXXXXXXXXXXXXXXXXXX";
    let bot = LineBot::new(channel_secret.as_str(), access_token.as_str());
    let mut messages: Vec<SendMessageType> = Vec::new();
    let res = bot.get_profile(line_id);
    let _profile = res.unwrap();
    messages.push(SendMessageType::TextMessage(TextMessage {
        text: String::from("Pushed"),
        emojis: None,
    }));
    let result = bot.push_message(line_id, messages);

I'm new to rust so I haven't found out why this happens yet.

axblueblader avatar Apr 14 '21 09:04 axblueblader

thanks @axblueblader please show me the error message when it fails.

nanato12 avatar Apr 15 '21 04:04 nanato12

Hi @nanato12 , here is the response log I set log level to TRACE but that's all I got, sorry, couldn't find other useful error message. It just works properly when remove bot.get_profile call

Ok(Response { url: Url { scheme: "https", username: "", password: None, host: Some(Domain("api.line.me")), port: None, path: "/v2/bot/message/push", query: None, fragment: None
    }, status: 400, headers: {
        "server": "AkamaiGHost",
        "mime-version": "1.0",
        "content-type": "text/html",
        "content-length": "216",
        "expires": "Thu, 15 Apr 2021 06:36:28 GMT",
        "date": "Thu, 15 Apr 2021 06:36:28 GMT",
        "connection": "close"
    }
})

axblueblader avatar Apr 15 '21 06:04 axblueblader

add "println" and check the response message. and show me.

let result = bot.push_message(line_id, messages);
println!("{:?}", result.unwrap().text());
scchot

UserID length is 33.

if length is not 33 or "Uxxxxxx" (x is hex) , response is Ok("{\"message\":\"The property, \'to\', in the request body is invalid (line: -, column: -)\"}"). if UserID is invalid, response is Ok("{\"message\":\"Failed to send messages\"}")

nanato12 avatar Apr 15 '21 09:04 nanato12

    println!("user id length: {:?}", line_id.len());
    let _res = bot.push_message(line_id, messages);
    println!("bot.push_message: {:?}", _res.unwrap().text());

Length is 33 Here is response text

Ok("<HTML><HEAD>\n<TITLE>Bad Request</TITLE>\n</HEAD><BODY>\n<H1>Bad Request</H1>\nYour browser sent a request that this server could not understand.<P>\nReference&#32;&#35;7&#46;6fb42d17&#46;1618478740&#46;0\n</BODY>\n</HTML>\n")

axblueblader avatar Apr 15 '21 09:04 axblueblader

LINE_CHANNEL_SECRET and LINE_CHANNEL_ACCESS_TOKEN correct?

nanato12 avatar Apr 15 '21 10:04 nanato12

Yes all info are guaranteed to be correct. Because this will work:

    let bot = LineBot::new(channel_secret.as_str(), access_token.as_str());
    let mut messages: Vec<SendMessageType> = Vec::new();
    messages.push(SendMessageType::TextMessage(TextMessage {
        text: String::from("Pushed"),
        emojis: None,
    }));
    let result = bot.push_message(line_id, messages);

But this will fail with that error:

    let bot = LineBot::new(channel_secret.as_str(), access_token.as_str());
    let mut messages: Vec<SendMessageType> = Vec::new();
    
    let res = bot.get_profile(line_id);
    let _profile = res.unwrap();

    messages.push(SendMessageType::TextMessage(TextMessage {
        text: String::from("Pushed"),
        emojis: None,
    }));
    let result = bot.push_message(line_id, messages);

axblueblader avatar Apr 15 '21 10:04 axblueblader

I think it might a bug in the http client or reqwest http client lib (maybe a thread or socket race condition?). When debugging, if I step slowly through each function in the debugger, the push_message call will be successful. Running normally will always get 400. Hope this info is useful

axblueblader avatar Apr 15 '21 11:04 axblueblader

thanks @axblueblader It works without any trouble in my environment. I don't know the exact cause. Sorry....

nanato12 avatar Apr 15 '21 14:04 nanato12

ye, it could be the Rust version or something anyways thanks for looking into it

axblueblader avatar Apr 16 '21 02:04 axblueblader