Hi,
I want to send messages to a private channel and be able to list all historical messages. I'm using:
var RtmClient = require('@slack/client').RtmClient;
var MemoryDataStore = require('@slack/client').MemoryDataStore;
var token = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
var slack = new RtmClient(token, { logLevel: 'debug', dataStore: new MemoryDataStore() });
slack.start();
but in slack.channels I got only public channels so I can't interact with the one I want. BTW, is it possible to list all channel messages?
Thanks,
_This response looks long but it really only takes like two to three minutes to read. Sorry about that._
How do I find the list of DMs using the RTM client?
The data store of the RTM client has a field named dms which holds all known DM (see footnote 1) channels by the client.
// side note: currently, a MemoryDataStore is created for the client if none is
// supplied, so you don't really need to provide one yourself
var client = new RtmClient(token);
client.start();
client.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, function() {
console.log(client.dataStore.dms);
});
How do I list all channel messages?
Depends on what kind of channel you want to list messages from. Here's a small little table of the channel type and the web method you should use for that channel type to get message history:
| Channel Type | Web Method |
| --- | --- |
| Regular | channels.history |
| Group | groups.history |
| DM/IM | im.history |
| Multi-Party DM/IM | mpim.history |
To use these, you'll need to use the WebClient. Given your last question was about DMs, I'll show off a small example of the web client myself:
var web = new WebClient(token);
// using a filler dm channel id
web.im.history('DXXXXXXXX', function(err, result) {
// Regular error-handling for web request callbacks
if (err)
return console.log(err);
if (!result.ok)
return console.log(result.error);
if (result.warning)
console.log(result.warning);
console.log(result.messages);
});
You also have the choice of promises with the web client if you prefer that fashion:
web.im.history('DXXXXXXXX')
.then(function(result) {
if (result.warning)
console.log(result.warning);
console.log(result.messages);
})
.catch(function(err) {
// Slack API errors are handled by the client in the case of promises.
console.log(err);
});
Have fun.
[1]: the repository refers to person-to-person messages as DMs while the actual web API that Slack provides uses the term IM. The only time you will (or, better yet, _should_) see IM in this repository is if it relates to the actual web API, like in the case of web API method names or properties (key names) in responses from the API.
I'm going to close this one as solved!
Since the response does not address the ticket question, and this is the first entry in google that covers private channels, I want to add that Slack private channels are not handled internally as channels but as groups. If you are looking for a listing of private channels, you would find them in slack.datastore.groups; or can retrieve the channel object via getGroupByName()/getGroupById().
Some caveats to be noted, is that while proper channels can accept the name with or without a leading # sign, since it gets stripped out within getChannelByName(), this is not the case with getGroupByName() - a group does not include a leading #. If you are attempting to allow passing in either a valid channel name, or a private channel name at the same time, you should pre-sanitize the channel name, and then call them after each other to find a proper channel object; i.e.
function getChannelEntity(channel) {
channel = channel.replace(/^#/, '');
return slack.dataStore.getChannelByName(channel) || slack.dataStore.getGroupByName(channel);
}
Most helpful comment
Since the response does not address the ticket question, and this is the first entry in google that covers private channels, I want to add that Slack private channels are not handled internally as channels but as groups. If you are looking for a listing of private channels, you would find them in slack.datastore.groups; or can retrieve the channel object via
getGroupByName()/getGroupById().Some caveats to be noted, is that while proper channels can accept the name with or without a leading
#sign, since it gets stripped out withingetChannelByName(), this is not the case withgetGroupByName()- a group does not include a leading#. If you are attempting to allow passing in either a valid channel name, or a private channel name at the same time, you should pre-sanitize the channel name, and then call them after each other to find a proper channel object; i.e.