Twitter Mentions -> Slack
@pravin
code:
data:privatelast updated:6 months ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 100,000+ developers using the Pipedream platform
steps.
trigger
Search Mentions
last updated:10 months ago
steps.
generate_slack_blocks
auth
to use OAuth tokens and API keys in code via theauths object
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 invocations with$checkpoint.
async (event, steps) => {
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
}
123
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
steps.
send_a_message
Send a message to a channel, group or user
auth
(auths.slack)
params
Text
1
{{event.full_text}}
string ·params.text
Channel
1
C1234567890
string ·params.channel
Blocks
1
{{steps.generate_slack_blocks.$return_value}}
string ·params.blocks
Optional
code
async (params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
}
22
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,
})