-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
5,769 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "thibmaek" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
Simple example of a cloud function to which tweets whenever a new entry gets published on Contentful. | ||
Set a contentful webhook to point to the function url and voila: | ||
|
||
![screenshot](https://res.cloudinary.com/thibault-maekelbergh/image/upload/c_scale,w_664/v1537129011/Screen_Shot_2018-09-16_at_22.15.27_pkodn6.png) | ||
|
||
# How | ||
|
||
It checks if the content type is equal to 'blog-post'. If the post's creation date is longer than one day ago it will not trigger since making edits and saving them in Contentful requires to republish the article which would retrigger the cloud function and send out a new tweet. | ||
|
||
# Usage | ||
|
||
Twitter credentials will be added as an env variable trough serverless. | ||
Define a `config.<stage>.js` file where stage is one of 'dev' or 'prod'. | ||
|
||
The dev config is already added and you can copy it over as config.prod.js and fill in details to deploy the function quickly | ||
|
||
```json | ||
{ | ||
"TWITTER_ACCESS_TOKEN_SECRET": "", | ||
"TWITTER_ACCESS_TOKEN": "", | ||
"TWITTER_CONSUMER_KEY": "", | ||
"TWITTER_CONSUMER_SECRET": "" | ||
} | ||
``` | ||
|
||
## Testing offline | ||
|
||
serverless-offline is added as a plugin to mock the lambda locally. Install all dependencies and run: | ||
|
||
```console | ||
$ npm start | ||
> sls offline --stage dev | ||
|
||
Serverless: Starting Offline: dev/us-east-1. | ||
|
||
Serverless: Routes for contentful_webhook_twitter: | ||
Serverless: POST / | ||
|
||
Serverless: Offline listening on http://localhost:3000 | ||
``` | ||
|
||
To test you can then send a post request to the address listed in ouput. Contentful sends a lot over in the webhook call but this is the minimum of required data: | ||
|
||
```json | ||
{ | ||
"sys": { | ||
"createdAt": "2018-09-16T20:25:32.024Z", | ||
"contentType": { | ||
"sys": { | ||
"id": "blog-post" | ||
} | ||
} | ||
}, | ||
"fields": { | ||
"title": { | ||
"en-US": "Example title of a blogpost" | ||
}, | ||
"slug": { | ||
"en-US": "some-post" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
# Deploy | ||
|
||
```console | ||
$ npm run deploy | ||
> sls deploy --stage prod | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"TWITTER_ACCESS_TOKEN_SECRET": "", | ||
"TWITTER_ACCESS_TOKEN": "", | ||
"TWITTER_CONSUMER_KEY": "", | ||
"TWITTER_CONSUMER_SECRET": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const { Twitter } = require(`twitter-node-client`); | ||
const differenceInDays = require(`date-fns/difference_in_days`); | ||
|
||
const twitter = new Twitter({ | ||
accessToken: process.env.TWITTER_ACCESS_TOKEN, | ||
accessTokenSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET, | ||
consumerKey: process.env.TWITTER_CONSUMER_KEY, | ||
consumerSecret: process.env.TWITTER_CONSUMER_SECRET, | ||
}); | ||
|
||
// eslint-disable-next-line import/no-commonjs | ||
exports.handler = (event, ctx, done) => { | ||
const { sys, fields } = JSON.parse(event.body); | ||
|
||
if (differenceInDays(new Date(), sys.createdAt) > 1) { | ||
return done(null, { body: `Publication date was longer than 1 day ago`, statusCode: 204 }); | ||
} | ||
|
||
if (sys.contentType.sys.id === `blog-post`) { | ||
twitter.postTweet( | ||
{ | ||
status: ` | ||
📝 Just published something now on my blog: ${fields.title[`en-US`]} | ||
https://thibmaek.com/post/${fields.slug[`en-US`]} | ||
`, | ||
}, | ||
err => done(err, { statusCode: 500 }), | ||
() => done(null, { statusCode: 200 }), | ||
); | ||
} | ||
|
||
done(null, { body: `Content Type was not a blog post`, statusCode: 422 }); | ||
}; | ||
|
Oops, something went wrong.