auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps) => {
}
function roundThousands(value) {
return Math.abs(value) > 999 ? Math.floor(value/1000) + 'k+' : Math.sign(value)*Math.abs(value)
}
function roundHundreds(value){
return Math.abs(value) > 100 ? Math.floor(value/100)*100 + '+' : Math.sign(value)*Math.abs(value)
}
function roundTens(value){
return Math.abs(value) > 10 ? Math.floor(value/10)*10 + '+' : Math.sign(value)*Math.abs(value)
}
const imps = roundThousands(event.tweet.impression_count)
const likes = roundHundreds(event.tweet.like_count)
const retweets = roundTens(event.tweet.retweet_count)
const quotes = roundTens(event.tweet.quote_count)
const replies = roundTens(event.tweet.reply_count)
this.title = `This tweet has ${imps} impressions, ${likes} likes & ${retweets} retweets`
this.description = `An exploration of the Twitter Labs & Dev.to APIs`
this.markdown = `
<blockquote class="twitter-tweet"><p lang="en" dir="ltr">This tweet updates itself...<a href="https://t.co/FwNRzUaxtu">https://t.co/FwNRzUaxtu</a></p>— Tod Sacerdoti (@tod) <a href="https://twitter.com/tod/status/1263131709861539841">May 20, 2020</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
In fact, the exact metrics for this tweet are:
+ ${event.tweet.impression_count} impressions
+ ${event.tweet.like_count} likes
+ ${event.tweet.retweet_count} retweets
+ ${event.tweet.reply_count} quotes
+ ${event.tweet.reply_count} replies
The reason the title doesn't include the exact numbers is due to limitations of the Twitter API, which I will get to later.
**View the live workflow code [here](https://pipedream.com/@tod/p_7NCOBo/).**
This workflow is powered by [Pipedream](https://pipedream.com)'s impossibly fast serverless integration platform and was inspired by similar self-reflexive automation efforts on [Hacker News](https://news.ycombinator.com/item?id=3742902) and [YouTube](https://www.youtube.com/watch?v=BxV14h0kFs0).
**How this workflow works (in three easy steps):**
![Summary](https://i.ibb.co/fxDQJjF/summary.png)
**1. Event Source - emits new events when Tweet Metrics change**
*Note: Since the [Twitter Tweet Metrics API](https://developer.twitter.com/en/docs/labs/tweet-metrics/faq) is in preview, you must have your own Twitter developer credentials and [activate](https://developer.twitter.com/en/account/labs) the preview in your developer account.*
![Event Source](https://i.ibb.co/ZNDtN9N/step1.png)
**2. Code Step - Dev.to post in markdown with title and description**
![Code Step](https://i.ibb.co/WKbthLF/step2.png)
**3. Action - update the article using the Dev.to [API](https://docs.dev.to/api/#operation/updateArticle)**
![Action](https://i.ibb.co/hRMSjQY/step3.png)
**Why does the tweet not update in real time?**
+ Unfortunately, Twitter doesn't update the data of twitter cards in real time, which is why the metrics are rounded in the tweet
+ Twitter cards can be updated via the [Card Validator](https://cards-dev.twitter.com/validator)
+ However, doing this programmatically would violate Twitter's [Developer Terms](https://developer.twitter.com/en/developer-terms)
+ Twitter says [DO NOT](https://help.twitter.com/en/rules-and-policies/twitter-automation) "use non-API-based forms of automation, such as scripting the Twitter website. The use of these techniques may result in the permanent suspension of your account."
**Why not build your own?**
1. View the live workflow code [here](https://pipedream.com/@tod/p_7NCOBo/)
2. **Copy** the workflow into your account
3. Add your Twitter / Dev.to credentials and make any code changes
4. **Deploy the workflow** and you are live
`
ID of the article
async
(params, auths) => {
}
const { title, description, body_markdown } = params
return await require("@pipedreamhq/platform").axios(this, {
method: "PUT",
url: `https://dev.to/api/articles/${params.id}`,
headers: {
"api-key": auths.dev.api_key,
},
data: {
article: {
title,
description,
body_markdown,
},
},
})