Slack Message Router
@dylburger
code:
data:privatelast updated:4 years ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 1,000,000+ developers using the Pipedream platform
steps.
trigger
HTTP API
Deploy to generate unique URL
This workflow runs on Pipedream's servers and is triggered by HTTP / Webhook requests.
steps.
CONSTANTS
auth
to use OAuth tokens and API keys in code via theauths object
params
Text to match
Star Wars
string ·params.textToMatch
Response
May the force be with you
string ·params.response
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, params) => {
1
2
3
4
5
6
7
8
}
9
// You can export data for use in future steps
// https://docs.pipedream.com/workflows/steps/#step-exports

// You can also accept input from the params form above:
// https://docs.pipedream.com/workflows/steps/#passing-data-to-steps-step-parameters
this.textToMatch = params.textToMatch
this.response = params.response
steps.
parse_filter_and_send_response
auth
to use OAuth tokens and API keys in code via theauths object
(auths.slack_bot)
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
}
54
// First, parse the relevant data from the incoming event. 
// The message payload is stored in event.body. Click on
// an event on the left, and examine its contents in the 
// trigger step above.
const { channel, text, user, type, subtype } = event.body.event

// We only want to process messages
// https://docs.pipedream.com/workflows/steps/code/#end
if (type !== "message" || subtype === "message_deleted") {
  $end("Not a message")
}

this.channelId = channel
this.messageText = text
this.userId = user

// Get the channel name associated with the ID
const { WebClient } = require('@slack/web-api')

// This uses an OAuth access token tied to the account connected
// to this step. It assumes the user has permission to run channels.info
// on the target channel
const web = new WebClient(auths.slack_bot.bot_token)
const res = await web.channels.info({ channel });
this.channelName = res.channel.name

// FILTER MESSAGES SENT FROM THIS BOT
// Returns the authed user ID
const { user_id } = await web.auth.test();

if (user_id === this.userId) {
  $end("Message sent by bot")
}

// FILTER ON MESSAGE PATTERN
const pattern = new RegExp(`.*${steps.CONSTANTS.textToMatch}.*`, 'gi')

if (!this.messageText.match(pattern)) {
  $end(`Message doesn't match ${steps.CONSTANTS.textToMatch}`)
}

// SEND A FORCEFUL MESSAGE
try {
  this.response = await web.chat.postMessage({
    as_user: true,
    text: steps.CONSTANTS.response,
    channel: this.channelName,
  })
} catch (err) {
  this.error = err
  throw err
}