Skip to content

Commit

Permalink
Merge pull request #1135 from simlu/dev
Browse files Browse the repository at this point in the history
fix: adjusted calls and added setPurpose and setTopic
  • Loading branch information
simlu committed Jul 17, 2021
2 parents cb87c6f + 3dcfd98 commit 6c16b5f
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 29 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ npm i --save slack-sdk
```javascript
const slack = require('slack-sdk')('workspace-name', 'user-session-token', {/* options */});

slack.message.channel('channel-name', 'message');
slack.channel.message('channel-name', 'message');
```

## Options
Expand All @@ -49,14 +49,26 @@ Maximum number of entries in cache at any given time. Optional, defaults to `100

## Functions

### message.self(message: string)
### self.message(message: string)

Send `message` to self.

### message.channel(channel: string, message: string)
### channel.meta(channel: string)

Get meta information about channel `channel`

### channel.message(channel: string, message: string)

Send `message` to channel `channel`.

### channel.setTopic(channel: string, topic: string)

Set `topic` of channel `channel`

### channel.setPurpose(channel: string, purpose: string)

Set `purpose` of channel `channel`

### workspace.details(cache: boolean = true)

Obtain details for workspace. Should usually be cached as it is easy to run into rate limits.
Expand All @@ -65,7 +77,7 @@ Obtain details for workspace. Should usually be cached as it is easy to run into

### call(endpoint: string, params: object, cache: boolean = false)

Send request to slack endpoint `endpoint` with parameters `params`.
Send request to slack endpoint `endpoint` with parameters `params`.

E.g. `call("rtm.start", {}, true)` to obtain information about current user. Use cache if information was already obtained before.

Expand Down
39 changes: 30 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,22 @@ module.exports = (workspaceUrl, token, { cacheTtl = 60, cacheMaxEntries = 100 }
});
return lru.memoize(signature, () => req());
};
const channelMeta = async (name) => {
const rtmStart = await call('rtm.start', {}, true);
const channel = rtmStart.channels.find((chan) => chan.name === name);
if (!channel) {
throw new Error(`Channel "${name}" not found.`);
}
return channel;
};

return {
call,
workspace: {
details: (cache = true) => call('rtm.start', {}, cache)
},
message: {
self: async (msg) => {
self: {
message: async (msg) => {
const rtmStart = await call('rtm.start', {}, true);
const user = rtmStart.self;
return call('chat.command', {
Expand All @@ -49,17 +57,30 @@ module.exports = (workspaceUrl, token, { cacheTtl = 60, cacheMaxEntries = 100 }
text: `${user.name} ${msg}`,
channel: rtmStart.ims.find((im) => im.user === user.id).id
});
},
channel: async (name, msg) => {
const rtmStart = await call('rtm.start', {}, true);
const channel = rtmStart.channels.find((chan) => chan.name === name);
if (!channel) {
throw new Error(`Channel "${name}" not found.`);
}
}
},
channel: {
meta: channelMeta,
message: async (name, msg) => {
const channel = await channelMeta(name);
return call('chat.postMessage', {
text: msg,
channel: channel.id
});
},
setTopic: async (name, topic) => {
const channel = await channelMeta(name);
return call('conversations.setTopic', {
topic,
channel: channel.id
});
},
setPurpose: async (name, purpose) => {
const channel = await channelMeta(name);
return call('conversations.setPurpose', {
purpose,
channel: channel.id
});
}
}
};
Expand Down
22 changes: 16 additions & 6 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,28 @@ describe('Testing Slack SDK', {
expect(r).to.deep.contain({ ok: true });
});

it('Testing message.channel', async () => {
const r = await slack.message.channel('channel', 'message');
it('Testing channel.message', async () => {
const r = await slack.channel.message('channel', 'message');
expect(r).to.deep.contain({ ok: true });
});

it('Testing message.channel-unknown', async ({ capture }) => {
const e = await capture(() => slack.message.channel('unknown', 'message'));
it('Testing channel.message-unknown', async ({ capture }) => {
const e = await capture(() => slack.channel.message('unknown', 'message'));
expect(e.message).to.equal('Channel "unknown" not found.');
});

it('Testing message.self', async () => {
const r = await slack.message.self('message');
it('Testing channel.setTopic', async () => {
const r = await slack.channel.setTopic('channel', 'topic');
expect(r).to.deep.contain({ ok: true });
});

it('Testing channel.setPurpose', async () => {
const r = await slack.channel.setPurpose('channel', 'purpose');
expect(r).to.deep.contain({ ok: true });
});

it('Testing self.message', async () => {
const r = await slack.self.message('message');
expect(r).to.deep.contain({ ok: true });
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"scope": "https://workspace.slack.com:443",
"method": "POST",
"path": "/api/rtm.start",
"body": "----------------------------128508069392224419437859\r\nContent-Disposition: form-data; name=\"token\"\r\n\r\nSLACK-SESSION-TOKEN\r\n----------------------------128508069392224419437859--\r\n",
"body": "----------------------------437991048648655338960848\r\nContent-Disposition: form-data; name=\"token\"\r\n\r\nSLACK-SESSION-TOKEN\r\n----------------------------437991048648655338960848--\r\n",
"status": 200,
"response": {
"ok": true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[
{
"scope": "https://workspace.slack.com:443",
"method": "POST",
"path": "/api/rtm.start",
"body": "----------------------------128508069392224419437859\r\nContent-Disposition: form-data; name=\"token\"\r\n\r\nSLACK-SESSION-TOKEN\r\n----------------------------128508069392224419437859--\r\n",
"status": 200,
"response": {
"ok": true,
"channels": [
{
"id": "C3Y9NQTQG",
"name": "channel"
}
]
}
},
{
"scope": "https://workspace.slack.com:443",
"method": "POST",
"path": "/api/conversations.setPurpose",
"body": "----------------------------198402330513246502238920\r\nContent-Disposition: form-data; name=\"token\"\r\n\r\nSLACK-SESSION-TOKEN\r\n----------------------------198402330513246502238920\r\nContent-Disposition: form-data; name=\"purpose\"\r\n\r\npurpose\r\n----------------------------198402330513246502238920\r\nContent-Disposition: form-data; name=\"channel\"\r\n\r\nC3Y9NQTQG\r\n----------------------------198402330513246502238920--\r\n",
"status": 200,
"response": {
"ok": true
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@
"status": 200,
"response": {
"ok": true,
"self": {
"id": "U0G9QF9C6",
"name": "user.name"
},
"ims": [
"channels": [
{
"id": "D19NX2Y3Z",
"user": "U0G9QF9C6"
"id": "C3Y9NQTQG",
"name": "channel"
}
]
}
},
{
"scope": "https://workspace.slack.com:443",
"method": "POST",
"path": "/api/chat.command",
"body": "----------------------------025274732886786331221225\r\nContent-Disposition: form-data; name=\"token\"\r\n\r\nSLACK-SESSION-TOKEN\r\n----------------------------025274732886786331221225\r\nContent-Disposition: form-data; name=\"disp\"\r\n\r\n/me\r\n----------------------------025274732886786331221225\r\nContent-Disposition: form-data; name=\"command\"\r\n\r\n/msg\r\n----------------------------025274732886786331221225\r\nContent-Disposition: form-data; name=\"text\"\r\n\r\nuser.name message\r\n----------------------------025274732886786331221225\r\nContent-Disposition: form-data; name=\"channel\"\r\n\r\nD19NX2Y3Z\r\n----------------------------025274732886786331221225--\r\n",
"path": "/api/conversations.setTopic",
"body": "----------------------------025274732886786331221225\r\nContent-Disposition: form-data; name=\"token\"\r\n\r\nSLACK-SESSION-TOKEN\r\n----------------------------025274732886786331221225\r\nContent-Disposition: form-data; name=\"topic\"\r\n\r\ntopic\r\n----------------------------025274732886786331221225\r\nContent-Disposition: form-data; name=\"channel\"\r\n\r\nC3Y9NQTQG\r\n----------------------------025274732886786331221225--\r\n",
"status": 200,
"response": {
"ok": true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"scope": "https://workspace.slack.com:443",
"method": "POST",
"path": "/api/rtm.start",
"body": "----------------------------240931670220439824763760\r\nContent-Disposition: form-data; name=\"token\"\r\n\r\nSLACK-SESSION-TOKEN\r\n----------------------------240931670220439824763760--\r\n",
"status": 200,
"response": {
"ok": true,
"self": {
"id": "U0G9QF9C6",
"name": "user.name"
},
"ims": [
{
"id": "D19NX2Y3Z",
"user": "U0G9QF9C6"
}
]
}
},
{
"scope": "https://workspace.slack.com:443",
"method": "POST",
"path": "/api/chat.command",
"body": "----------------------------898525272439246643720391\r\nContent-Disposition: form-data; name=\"token\"\r\n\r\nSLACK-SESSION-TOKEN\r\n----------------------------898525272439246643720391\r\nContent-Disposition: form-data; name=\"disp\"\r\n\r\n/me\r\n----------------------------898525272439246643720391\r\nContent-Disposition: form-data; name=\"command\"\r\n\r\n/msg\r\n----------------------------898525272439246643720391\r\nContent-Disposition: form-data; name=\"text\"\r\n\r\nuser.name message\r\n----------------------------898525272439246643720391\r\nContent-Disposition: form-data; name=\"channel\"\r\n\r\nD19NX2Y3Z\r\n----------------------------898525272439246643720391--\r\n",
"status": 200,
"response": {
"ok": true
}
}
]

0 comments on commit 6c16b5f

Please sign in to comment.