Skip to content
A C# app that tweets a random post from a Ghost blog, using AWS Lambda and Tweetinvi.
C#
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
TweetRandomFeedItem
.gitignore
LICENSE
README.md
TweetRandomFeedItem.sln

README.md

Tweet a Random Post from a Ghost Blog

Using Ghost, AWS Lambda, and Tweetinvi

My first experience with AWS Lambda was using Ephemeral to clean up my Twitter feed, which I lightly modified to delete "likes" as well as tweets.

Ephemeral is a Go app written by Vicky Lai - check out her posts too:

I wanted to try my hand at writing my own app to run in AWS Lambda, and I wanted to do it in C#. I've also had an idea for awhile now that it'd be nice to be able to randomly select and tweet my old blog posts.

Usage

So here it is, a C# console app you can run in AWS Lambda. Schedule it to run as often as you like - I'm thinking maybe once a day.

If you'd like to read more about it, I wrote a blog post about it too.

Getting the code

  1. Clone the repo: git clone git@github.com:grantwinney/TweetRandomFeedItemForGhost.git
  2. Open the project in Visual Studio.
  3. Build the project (you may have to right-click the NuGet folder and choose 'restore' to download the dependencies, although VS usually takes care of that for you)
  4. Find the bin directory on disk, and drill down until you get to the assemblies (dll files). Most likely: bin/Debug/netcoreapp2.0
  5. Zip up the contents of the inner-most directory, but not the directory itself. (e.g. just select all the files inside netcoreapp2.0.)

Setting up AWS Lambda

Now you need to create a new AWS Lambda job. Check out the first article linked above for a brief intro to setting up a job.

  1. Create a new function and choose C# (.NET Core 2.0) for the runtime.
  2. The name of the function, and the role it makes you create, don't matter.
  3. Upload the zip file you created above.
  4. Set the handler as TweetRandomFeedItem::TweetRandomFeedItem.Program::Main
  5. Under "Basic settings", decrease the memory to 128MB and increase the timeout to a minute. For me, it generally takes about 15-20 seconds to run, and uses 50MB or less of memory.
  6. Set the environment variables as follows:

Environment Variables

I make heavy use of environment variables, so credentials and other settings can easily be changed between runs, without having to recompile the code and upload it again.

Field Required Description
GHOST_CLIENT_ID
GHOST_CLIENT_SECRET
Yes If you don't have the public API enabled in Ghost, do it now. Open any post on your blog and view the source code for something like the folowing - you'll need both values.

ghost.init({clientId: "ghost-frontend", clientSecret: "55f1e0f55123"});
GHOST_USERNAME
GHOST_PASSWORD
No If you leave the public API enabled, you can omit these variables (or include them but leave the value blank).

If you have the public API disabled, you need to include your Ghost username and password too, so that the app can retrieve an auth token from Ghost.
GHOST_URI Yes Specify your blog URL, like: https://grantwinney.com

Without this, the app can't make an API call to your blog to select a random post.
GHOST_POST_RETRIEVAL_LIMIT No Specify the number of posts you want to retrieve. It uses the limit parameter of the API.

If you omit this, or leave the value empty, the app will consider all your posts when selecting a random one.
TWITTER_CONSUMER_KEY
TWITTER_CONSUMER_SECRET
TWITTER_USER_ACCESS_TOKEN
TWITTER_USER_ACCESS_TOKEN_SECRET
Yes These values all come from your Twitter account. You need to create a new app to get these values, which allows you to post tweets.
TWITTER_MAX_TAG_COUNT No Although tag spamming is somewhat prevalent on Facebook, and even more-so on Instagram, I don't see a lot of it on Twitter. If you tend to use a lot of tags for posts on your blog, then you can limit how many of those transfer to your tweet.

If you omit this, or leave the value empty, the app will use the first 3 tags.
TAGS_TO_TWEET No Specify a comma-delimited list of tag slugs which should be the only ones considered for tweeting. If omitted, then any tags are fair game.
You can’t perform that action at this time.