auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps, auths) => {
}
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
}
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')
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"
})
Text of the message to send. See below for an explanation of formatting. 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')
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
}