Sitemap
PMB Digital

Posts (mostly about technology) from the digital team at Primedia Broadcasting

Connecting free tools to solve business challenges

Or, how we built an intuitive CMS for our in-studio screens on the cheap

--

Anyone who’s worked with me knows that I’ll look to unix whenever there’s a challenge to be solved. And this is not because I’m an impartial unix zealot (OK maybe just a bit), but because I love the way that it provides a host of very specific tools that can be chained together in ways only limited (for the most part) by your imagination.

And so it was this approach that we took when trying to solve our latest challenge — how do we let people quickly and easily update the in-studio screens, when they are pressed for time and on the move? The brief was something like this:

A guest arrives on short notice and we need the producers to be able to take a photo and get it onto the screens — if they could just email it somewhere and have it appear, that would be great.

This makes complete sense. We could easily have built a small CMS for uploading the images and guest details, but that would require the producers to take the photo, open the CMS on their phone (or get the photo off their phone) and upload. These are people who don’t have time for such luxuries, and so simply being able to send an email somewhere and have the rest “just happen” would be the path of least resistance.

For the first part of the puzzle we turned to https://postmarkapp.com — this is great service for handling email delivery from your apps, but the feature that makes it most attractive for this task is the inbound mail processing. With this feature you get a unique email address to which you can bind a webhook — any mail that arrives on that email address gets parsed, and the data POST’ed to the webhook endpoint of your choice. You start with 10 000 credits and once you’ve run out, 1000 credits will cost you $1.50, so it’s not going to break the bank.

For the hosting of the webhook, Heroku works perfectly. The first endpoint to create is the one that http://postmarkapp.com hits when mail arrives. Here’s a very simplified code snippet from my Sinatra app:

 post ‘/maildump’ do
body = JSON::parse(request.body.read)
subject = body[“Subject”]
textbody = body[“TextBody”]
body[“Attachments”].each do |attached|
name = attached[“Name”]
attachment = Base64.decode64(attached[“Content”])
end
status 200
end

Basically, parse the JSON request body, extract the subject and email body, then loop through each of the attachments, decoding the base64 encoded attachment as you go. Our live code has some logic that strips out our business signatures as we don’t want that coming up on the screens. You can check out the Postmark developer documentation for the full details of what gets delivered in the JSON payload.

The next question is what to do with the data once it arrives? Initially we were just saving the attachment and storing the data in a database, but what if they made a typo, or want to be able to go back to a previous image? Building an admin interface for this seemed like a bit of overkill, so for this we turned to Trello. For a free service, Trello is amazing — we use it for organising everything from our personal to-do lists through to large projects, and it has a full API as well. So why not use it to store the info we want to display on the screens?

I created a new board with two lists: Live and Archive. I’ll use the Live list for card(s) that I want to appear on the screen and the Archive list for keeping the old cards in case I want to reuse them.

To access the board you’ll need two things: a developer key and a user token. To get a developer key, log in to Trello and then visit this URL:

https://trello.com/1/appKey/generate

When you have the key, plug it into this URL to get the token:

https://trello.com/1/connect?key=yourdeveloperkey&name=YourAppName&response_type=token&expiration=never&scope=read,write

Get Allan Kent’s stories in your inbox

Join Medium for free to get updates from this writer.

If you only need read access, just remove ,write from the scope in the URL, but since I want to push content into the board, I need read and write access.

One other thing you’ll need to grab for your code is the ID for the Live list that you want to add the card to. The Board ID you can get from the URL when you visit that Board, then plug it into this API call to get the ID’s of the Lists in that board:

https://api.trello.com/1/boards/yourboardid/lists?key=yourkey&token=yourtoken

Here’s my /maildump endpoint code again, but this time including the code to store the info in Trello:

post ‘/maildump’ do
body = JSON::parse(request.body.read)
subject = body[“Subject”]
textbody = body[“TextBody”]
auth_params = {
:key => ‘yourdeveloperkeyhere’,
:token => ‘yourusertokenhere’
}
post_params = auth_params
post_params[:idList] = ‘yourlistidgoeshere’
post_params[:name] = subject
post_params[:desc] = textbody
post_params[:pos] = ‘top’
response = Unirest.post “https://api.trello.com/1/cards",
headers:{“Accept” => “application/json”},
parameters: post_params
card = response.body[“id”]
body[“Attachments”].each do |attached|
name = attached[“Name”]
attachment = Base64.decode64(attached[“Content”])
tfile = File.open(‘./tmp/attached.jpg’, ‘w’)
tfile.write(attachment)
tfile.close
file = File.open(“./tmp/attached.jpg”, 'r')
attach_params = auth_params
attach_params[:file] = file
attach_params[:name] = name
response = Unirest.post “https://api.trello.com/1/cards/# {card}/attachments",
headers:{“Accept” => “application/json”},
parameters: attach_params
end
status 200
end

As you can see, we create the new card, setting the pos to top, so that it is first in the list. Th excellent http://unirest.io library makes this simple. Once the card is created, we attach any email attachments to the newly created card. That code is a bit of a mess — when making the API call for uploading the attachment, you need to pass in a File handle to a file opened for reading, so you have to first write the temp file, close it, and then open it for reading. If anyone knows of a more elegant way to do this, please let me know — I tried a couple of permutations with in-memory files but the attachment always ended up as corrupted on Trello.

The email arriving as a card in Trello

Because Trello shows you real time updates, you can actually watch the card pitch up in Trello just after you send it.

The card detail

The eagle-eyed amongst you will point out that Trello provides an email address for creating cards — we did try this, but there was a significant delay between sending the email and it arriving in Trello. We also wanted to be able to control the structure of the card (and do some text wrangling first) so while it was a bit more work, the Postmark/Heroku combo worked better for us.

The last piece of the puzzle is to get the info out of Trello, and of course there’s an API for this:

get ‘/screen.json’ do
content_type :json
response[‘Access-Control-Allow-Origin’] = ‘*’
auth_params = {
:key => ‘yourdeveloperkeyhere’,
:token => ‘yourusertokenhere’
}
query_params = auth_params
query_params[:fields] = ‘name,desc’
query_params[:attachments] = ‘true’
query_params[:attachment_fields] = ‘url’
query_params[:limit] = 1
resp = Unirest.get “https://api.trello.com/1/lists/yourlisidgoeshere/cards",
headers:{ “Accept” => “application/json” },
parameters: query_params
{ :subject => resp.body.first[“name”], :body => resp.body.first[“desc”], :image => resp.body.first[“attachments”].first[“url”] }.to_json
end

This will return the first card from the list in JSON format:

{
"subject": "Another test",
"body": "Here we go!\r\n",
"image": "https://trello-attachments.s3.amazonaws.com/.../attached.jpg"
}

The data that this endpoint provides is then used with some Handlebars magic to create the display for the screens. We’ve now got a very simple pipeline of (almost) free tools that deliver ease of use to the producers (send an email) as well as some control when required (open Trello and edit).

There’s still some work to be done:

  • error checking would be handy ☺
  • having the display cycle through all of the cards in the Live list, rather than just showing one (if required)
  • setting up some WebSockets (or properly investigating the functionality that Client.js provides) so that we can push changes to the displays

--

--