Skip to content

Commit

Permalink
Add contentful fn
Browse files Browse the repository at this point in the history
  • Loading branch information
thibmaek committed Apr 19, 2019
1 parent 95d48eb commit 0649789
Show file tree
Hide file tree
Showing 8 changed files with 5,769 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ node_modules
# Secrets
env.*
.env.*
config.prod.json
config.production.json
config.stag.json
config.staging.json

# Artifacts
build/
Expand Down
3 changes: 3 additions & 0 deletions contentful-webhook-twitter-fn/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "thibmaek"
}
70 changes: 70 additions & 0 deletions contentful-webhook-twitter-fn/README.md
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
```
6 changes: 6 additions & 0 deletions contentful-webhook-twitter-fn/config.dev.json
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": ""
}
34 changes: 34 additions & 0 deletions contentful-webhook-twitter-fn/index.js
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 });
};

Loading

0 comments on commit 0649789

Please sign in to comment.