Skip to content

Commit

Permalink
Merge pull request #2278 from botpress/sp_slack-users
Browse files Browse the repository at this point in the history
fix(channel-slack): added user info to messages
  • Loading branch information
slvnperron committed Aug 27, 2019
2 parents 5aaa89b + 0defa15 commit 8768441
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 7 deletions.
7 changes: 5 additions & 2 deletions modules/channel-slack/README.md
Expand Up @@ -23,11 +23,14 @@
1. Edit `data/bots/YOUR_BOT_ID/config/channel-slack.json` (or create it) and set

- enabled: Set to `true`
- signingSecret: Take the value `Signing Secret` on the page `Basic Information`
- botToken: Take the value `Bot User OAuth Access Token` on the page `OAuth & Permissions`
- signingSecret: Take the value `Signing Secret` on the page **`Basic Information`**
- botToken: Take the value `Bot User OAuth Access Token` on the page **`OAuth & Permissions`**

2. Restart Botpress

![botToken](./assets/botToken.png)
![signingSecret](./assets/secret.png)

### Configure Callback on Slack

These steps are required so users can click on quick reply buttons, select dropdown options or any other interactive method.
Expand Down
Binary file added modules/channel-slack/assets/botToken.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added modules/channel-slack/assets/secret.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion modules/channel-slack/package.json
Expand Up @@ -22,6 +22,8 @@
"@slack/interactive-messages": "^1.1.1",
"@slack/rtm-api": "^5.0.1",
"@slack/web-api": "^5.0.1",
"axios": "^0.19.0"
"axios": "^0.19.0",
"lru-cache": "^5.1.1",
"ms": "^2.1.2"
}
}
37 changes: 34 additions & 3 deletions modules/channel-slack/src/backend/client.ts
Expand Up @@ -4,6 +4,8 @@ import { WebClient } from '@slack/web-api'
import axios from 'axios'
import * as sdk from 'botpress/sdk'
import _ from 'lodash'
import LRU from 'lru-cache'
import ms from 'ms'

import { Config } from '../config'

Expand All @@ -15,6 +17,8 @@ const debugOutgoing = debug.sub('outgoing')

const outgoingTypes = ['text', 'image', 'actions', 'typing', 'carousel']

const userCache = new LRU({ max: 1000, maxAge: ms('1h') })

export class SlackClient {
private client: WebClient
private rtm: RTMClient
Expand Down Expand Up @@ -86,11 +90,31 @@ export class SlackClient {
debugIncoming(`Received real time payload %o`, payload)

if (!discardedSubtypes.includes(payload.subtype)) {
await this.sendEvent(payload, { type: 'text', text: payload.text })
await this.sendEvent(payload, {
type: 'text',
text: _.find(_.at(payload, ['text', 'files.0.name', 'files.0.title']), x => x && x.length) || 'N/A'
})
}
})

await this.rtm.start()
return this.rtm.start()
}

private async _getUserInfo(userId: string) {
if (!userCache.has(userId)) {
const data = await new Promise((resolve, reject) => {
this.client.users
.info({ user: userId })
.then(data => resolve(data && data.user))
.catch(err => {
debug('error fetching user info:', err)
resolve({})
})
})
userCache.set(userId, data)
}

return userCache.get(userId) || {}
}

async handleOutgoingEvent(event: sdk.IO.Event, next: sdk.IO.MiddlewareNextCallback) {
Expand Down Expand Up @@ -133,13 +157,20 @@ export class SlackClient {
private async sendEvent(ctx: any, payload: any) {
const threadId = _.get(ctx, 'channel.id') || _.get(ctx, 'channel')
const target = _.get(ctx, 'user.id') || _.get(ctx, 'user')
let user = {}

if (target && this.config.fetchUserInfo) {
try {
user = await this._getUserInfo(target.toString())
} catch (err) {}
}

this.bp.events.sendEvent(
this.bp.IO.Event({
botId: this.botId,
channel: 'slack',
direction: 'incoming',
payload,
payload: { ...ctx, ...payload, user_info: user },
type: payload.type,
preview: payload.text,
threadId: threadId && threadId.toString(),
Expand Down
7 changes: 7 additions & 0 deletions modules/channel-slack/src/config.ts
Expand Up @@ -14,4 +14,11 @@ export interface Config {
* @default signin_secret
*/
signingSecret: string

/**
* Fetch or not the user information when receiving a message.
* This uses caching for efficiency.
* @default true
*/
fetchUserInfo: boolean
}
2 changes: 1 addition & 1 deletion modules/channel-slack/yarn.lock
Expand Up @@ -3706,7 +3706,7 @@ ms@2.0.0:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=

ms@^2.1.1:
ms@^2.1.1, ms@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==
Expand Down

0 comments on commit 8768441

Please sign in to comment.