Tweet a Random Post from a Ghost Blog
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:
- Running a free Twitter bot on AWS Lambda (intro on how to setup AWS Lambda)
- Why I'm automatically deleting all my old tweets, and the AWS Lambda function I use to do this (food for thought, and how to setup Ephemeral)
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.
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
- Clone the repo:
git clone firstname.lastname@example.org:grantwinney/TweetRandomFeedItemForGhost.git
- Open the project in Visual Studio.
- 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)
- Find the
bindirectory on disk, and drill down until you get to the assemblies (dll files). Most likely:
- Zip up the contents of the inner-most directory, but not the directory itself. (e.g. just select all the files inside
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.
- Create a new function and choose
C# (.NET Core 2.0)for the runtime.
- The name of the function, and the role it makes you create, don't matter.
- Upload the zip file you created above.
- Set the handler as
- 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.
- Set the environment variables as follows:
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.
||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.
||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.
||Yes||Specify your blog URL, like:
Without this, the app can't make an API call to your blog to select a random post.
||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.
||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.|
||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.
||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.|