auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps, params) => {
}
// 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
auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps, auths) => {
}
// 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
}