Translated Twitter Mentions -> Slack
@pravin
code:
data:privatelast updated:7 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
twitter-search
last updated:last year
steps.
translate
auth
to use OAuth tokens and API keys in code via theauths object
(auths.google_cloud_translate)
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, 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
const fetch = require('node-fetch')

if (event.lang !== 'en' && event.lang !== '') {
  try {
    console.log(`Language is ${event.lang}. Translating`)
    url = `https://translation.googleapis.com/language/translate/v2?key=${auths.google_cloud_translate.api_key}`
    method = "POST"
    headers = {
      'Content-Type': 'application/json',
      'charset': 'utf-8',
    }
    const body = JSON.stringify({ q: event.full_text, target: 'en' })
    const data = await fetch(url, { 
      method, 
      body, 
      headers,
    })
    const response = await data.json()
    this.languageCode = response.data.translations[0].detectedSourceLanguage
    this.text = response.data.translations[0].translatedText
  } catch (err) {
    this.error = err
    this.languageCode = event.lang
    this.text = event.full_text
  }
} else {
  this.languageCode = event.lang
  this.text = event.full_text
} 
steps.
generate_messages
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
124
125
126
127
}
128
const ISO6391 = require('iso-639-1')
const _ = require('lodash')
const mediaUrl = _.get(steps, 'trigger.event.extended_entities.media[0].media_url_https', '')
const mediaType = _.get(steps, 'trigger.event.extended_entities.media[0].type', '')

function getLanguageName(isocode) {
  try {
    return ISO6391.getName(isocode)
  } catch (err) {
    console.error(err)
    return "Unknown"
  }
}

function kFormatter(num) {
    return Math.abs(num) > 999 ? Math.sign(num)*((Math.abs(num)/1000).toFixed(1)) + 'k' : Math.sign(num)*Math.abs(num)
}

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"
}

let language = ""
if (steps.translate) {
	console.log('using translated output')
	this.message = steps.translate.text
	language = `${getLanguageName(steps.translate.languageCode)} (${steps.translate.languageCode})`
} else {
	console.log('using event data')
	this.message = event.full_text
	language = `${getLanguageName(event.lang)} (${event.lang})`
}

let quotedMessage = ''
const lines = this.message.split('\n')
lines.forEach(line=>{
	quotedMessage = quotedMessage + '> ' + line + '\n'
})

const tweetUrl = `https://twitter.com/${event.user.screen_name}/statuses/${event.id_str}`
const userUrl = `https://twitter.com/${event.user.screen_name}/`

this.blocks = []
this.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') {
	this.blocks.push({
		"type": "image",
		"image_url": mediaUrl,
		"alt_text": "Tweet Image"
	})
}

this.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"
})
steps.
send_a_message
Send a message to a channel, group or user
auth
(auths.slack)
params
Text
1
{{steps.generate_messages.message}}
string ·params.text
Channel
1
C1234567890
string ·params.channel
Blocks
1
{{steps.generate_messages.blocks}}
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
23
24
25
26
}
27
const { WebClient } = require('@slack/web-api')

try {
  const web = new WebClient(auths.slack.oauth_access_token)
  this.response = await web.chat.postMessage({
    attachments: params.attachments,
    unfurl_links: params.unfurl_links,
    text: params.text,
    unfurl_media: params.unfurl_media,
    parse: params.parse || 'none',
    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,
  })
} catch (err) {
  this.error = err
  throw err
}