slack-rs / slack-rs-api

Rust interface for the Slack Web API
Apache License 2.0
109 stars 66 forks source link

Can post message to channel, but can't get channel history #80

Closed sts10 closed 4 years ago

sts10 commented 4 years ago

Running into something strange, hoping y'all can help (though it may not be an issue with this crate — if that's uncalled for, feel free to close/delete, my bad):

I'm trying to make a Slack bot that needs to read a channel's history and send messages to that channel. I've got it sending messages just fine, using this function:

fn send_message(text: &str, channel: &str, client: slack::requests::Client, token: &str) {
    let msg_request = slack::chat::PostMessageRequest {
        channel: channel,
        text: text,
        parse: None,
        link_names: None,
        attachments: None,
        unfurl_links: Some(true),
        unfurl_media: None,
        username: Some("My Bot"),
        as_user: Some(false),
        icon_url: None,
        icon_emoji: None,
        thread_ts: None,
        reply_broadcast: None,
    };
    slack::chat::post_message(&client, token, &msg_request).unwrap();
}

But when I try to read the same channel's history, I keep getting the same error: "ChannelNotFound". My read_channel function (below) is very similar to the channel_history.rs example given in this repo.

// https://github.com/slack-rs/slack-rs-api/blob/master/examples/channel_history.rs
fn read_channel(channel: &str, client: slack::requests::Client, token: &str) {
    let response = slack::channels::history(
        &client,
        &token,
        &slack::channels::HistoryRequest {
            channel: channel,
            ..slack::channels::HistoryRequest::default()
        },
    );

    if let Ok(response) = response {
        if let Some(messages) = response.messages {
            println!("Got {} messages:", messages.len());
            for message in messages {
                println!("{:?}", message);
            }
        }
    } else {
        println!("{:?}", response);
    }
}

Also, I can successfully list all channels, using the other code example in this repo. The channel(s) I've tried reading is in this outputted list!

Any ideas? I've tried with multiple channels, some public and some private. I also tried having a # before the channel name and not having it. PS. I know both of these functions should likely return a Result of some kind — I'm just trying to get it up and running.

dten commented 4 years ago

What did you pass as channel? It takes something like C1234567890

sts10 commented 4 years ago

oooh is that like a channel ID or something? I was just passing it #general, which worked for send_message, so I just assumed it would work for read_channel.

What's the best way to get that ID of a particular channel?

dten commented 4 years ago

True that can take either. Hmm. The easiest way to see and test is to open a channel on a web browser and you can see the id in the URL.

sts10 commented 4 years ago

The easiest way to see and test is to open a channel on a web browser and you can see the id in the URL.

This worked! And feeding that "ID" into my read_channel function works as expected. Thanks!

I'll close this issue for now, but it might be nice to note this ID requirement in the documentation somewhere?

dten commented 4 years ago

It's in the real docs, sort of https://api.slack.com/methods/channels.history Glad it worked