This Tweet Updates Itself
@tod
code:
data:privatelast updated:3 years ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 800,000+ developers using the Pipedream platform
steps.
trigger
active
last updated:-last event:-
steps.
create_post
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 executions 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
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>&mdash; 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
`
steps.
update_an_article
This endpoint allows the client to update an existing article
auth
(auths.dev)
params
Post ID

ID of the article

 
string ·params.id
Optional
code
async (params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
}
17
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,
    },
  },
})