auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps) => {
}
const ISO6391 = require('iso-639-1')
const _ = require('lodash')
// Return a friendly language name for ISO language codes
function getLanguageName(isocode) {
try {
return ISO6391.getName(isocode)
} catch (err) {
console.error(err)
return "Unknown"
}
}
// Format numbers over 1000
function kFormatter(num) {
return Math.abs(num) > 999 ? Math.sign(num)*((Math.abs(num)/1000).toFixed(1)) + 'k' : Math.sign(num)*Math.abs(num)
}
// Format the Tweet as a quoted Slack message
let quotedMessage = ''
event.full_text.split('\n').forEach(line => quotedMessage = quotedMessage + '> ' + line + '\n' )
// Define metadata to include in the Slack message
const language = `${getLanguageName(event.lang)} (${event.lang})`
let type = "Original Tweet"
let intro = "New mention"
if (event.in_reply_to_status_id) {
type = "Reply"
intro = "New reply"
}
if (event.retweeted_status) {
type = "Retweet"
intro = "Retweet"
}
const tweetUrl = `https://twitter.com/${event.user.screen_name}/statuses/${event.id_str}`
const userUrl = `https://twitter.com/${event.user.screen_name}/`
const mediaUrl = _.get(steps, 'trigger.event.extended_entities.media[0].media_url_https', '')
const mediaType = _.get(steps, 'trigger.event.extended_entities.media[0].type', '')
// Format the message as Slack blocks
// https://api.slack.com/block-kit
const blocks = []
blocks.push({
"type": "section",
"text": {
"type": "mrkdwn",
"text": `*<${tweetUrl}|${intro}> by <${userUrl}|${event.user.screen_name}> (${event.created_at}):*\n${quotedMessage}`
},
"accessory": {
"type": "image",
"image_url": event.user.profile_image_url_https,
"alt_text": "Profile picture"
}
})
if(mediaUrl !== '' && mediaType === 'photo') {
blocks.push({
"type": "image",
"image_url": mediaUrl,
"alt_text": "Tweet Image"
})
}
blocks.push({
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": `*User:* ${event.user.screen_name}`
},
{
"type": "mrkdwn",
"text": `*Followers:* ${kFormatter(event.user.followers_count)}`
},
{
"type": "mrkdwn",
"text": `*Location:* ${event.user.location}`
},
{
"type": "mrkdwn",
"text": `*Type:* ${type}`
},
{
"type": "mrkdwn",
"text": `*Original Language:* ${language}`
},
{
"type": "mrkdwn",
"text": `*Description:* ${event.user.description}`
}
]
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "View on Twitter",
"emoji": true
},
"url": tweetUrl
}
]
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": `Sent via <https://pipedream.com/@/${steps.trigger.context.workflow_id}|Pipedream>`
}
]
},
{
"type": "divider"
})
return blocks
Text of the message to send. See Slack's formatting docs for more information. This field is usually required, unless you're providing only attachments instead. Provide no more than 40,000 characters or risk truncation.
Channel, private group, or IM channel to send message to. Can be an encoded ID, or a name. See below for more details.
A JSON-based array of structured blocks, presented as a URL-encoded string.
async
(params, auths) => {
}
const { WebClient } = require('@slack/web-api')
const web = new WebClient(auths.slack.oauth_access_token)
return await web.chat.postMessage({
attachments: params.attachments,
unfurl_links: params.unfurl_links,
text: params.text,
unfurl_media: params.unfurl_media,
parse: params.parse,
as_user: params.as_user || false,
mrkdwn: params.mrkdwn || true,
channel: params.channel,
username: params.username,
blocks: params.blocks,
icon_emoji: params.icon_emoji,
link_names: params.link_names,
reply_broadcast: params.reply_broadcast || false,
thread_ts: params.thread_ts,
icon_url: params.icon_url,
})