Square + IFTTT(?) + ____ = When should I close up shop?
January 17, 2015 7:48 PM   Subscribe

I run a small arts venue with a bar that operates during events. We don't have a fixed "closing time," and we encourage people to hang around after events if they are continuing to make purchases. I'd like to receive a "smart" notification when it's time to send my bartender home.

My rule of thumb is that if we haven't made a sale in 30 minutes, it's reasonable to issue last call and begin closing up shop. In a social environment, however, it's easy to lose track of time, and I sometimes end up keeping staff on long after it's no longer profitable to do so.

Each transaction is logged in Square Register, and since IFTTT has a Square channel now, it occurred to me that there might be a way to trigger a notification to me or whoever's managing the venue when the last sale is >30 minutes old.

This can't be done in a single step, and I'd like to determine what chain of several programs/cron jobs/actions makes the most sense for my aim.

The relevant trigger in IFTTT is "any new transaction," which has access to the transaction's date/time.

So IFTTT can, for example, add a line to a Google Docs sheet for each transaction with the transaction time, but, then what? Is there a Google Apps Script that can notify me once when the latest time stamp in the file is more than 30 minutes old, or that can conduct some other action that can trigger another IFTTT recipe?

Or maybe I could have new transactions trigger an email, and write some simple script that'll process that mail, keep track of when the last timestamp or transaction email was, and run some kind of cron job to, again, do some IFTTT triggerable action if the last transaction was over 30 min ago? (I'm presuming, here, that the last step in this chain is, again, using IFTTT to trigger an iOS notification to me).

Before I pull my hair out trying to learn Google's app script API or write a perl script that reads my email, or (?) - I'd love advice on how you would go about this.

Additional background: I'm a novice programmer - I can probably do my homework and hack together any number of solutions, so I'm just trying to hive-ascertain which approach might be the most straightforward and future-proof before putting the time into making it work, or hurdles I may not have thought of. Or, if someone industrious wants to contribute some code, I wouldn't complain (and I'd be willing to discuss hiring someone to do it, especially if you can help devise a battle plan).
posted by myrrh to Computers & Internet (8 answers total) 7 users marked this as a favorite
 
I've never used IFTTT but logically what you want is:

On new action:
Cancel any existing timer;
Set new timer for 30 minutes.

On timer expiration:
Send last call notification.

Depending on the nature of your space you may want to set a lockout so no alarms are set during the hours you actually plan to be open, but the general idea is that every action should start a fresh countdown timer, and eventually one of those countdown timers would actually go off.
posted by fedward at 8:49 PM on January 17, 2015 [1 favorite]


This is a really interesting use case. I went though all the IFTTT channels and I don't see any way to trigger timers, so I think you're on the right track with a cron job, assuming of course you have a Linux or Mac machine (ie something that supports basic cron) always on while you're running the bar. What I would do is have IFTTT append to a file in Dropbox on each transaction. On your server/cron box, mount that Dropbox folder so to you have access to Dropbox as part of the file system. Then run a cron job that finds if that file has been modified in the last 30min. If it hasn't, write a new file to the Dropbox folder. Then using a second IFTTT trigger, check if a new file has been added to the Dropbox folder, and send your iOS notification.

So to summarize -- 2 IFTTT triggers, one linking Square to Dropbox, and then Dropbox to iOS. In the middle you have a cron job watching Dropbox and effectively triggering the 2nd IFTTT if nothing's happened in 30min.
posted by cgg at 9:36 PM on January 17, 2015


Before you run off to do this, you should know that IFTTT isn't necessarily instant. It looks like officially it updates every 15 minutes. That would mean a 30 minutes or greater trigger could mean no transactions in the last 30-45 minutes not 30. If a 15 minute delay is ok, then carry on. Otherwise, I'm not sure there's much point figuring this out.
posted by If only I had a penguin... at 12:50 AM on January 18, 2015 [3 favorites]


If you're willing to dig a bit deeper and code more of this yourself, it looks like Square has an API for getting notified when a payment occurs. As this is likely what IFTTT uses under the covers, connecting directly to this API will give you much more control over the process, although it won't be quite as easy to set up. That said, since it looks like you'd already be on the hook for some slightly more complex timeout logic somewhere, it might not be much more effort to add this directly to whatever system you have to handle that.
posted by Aleyn at 10:10 AM on January 18, 2015


This is a really cool problem.

I assume you're going to want your whole solution in the cloud, and not running on your own server. That means you need to find a service that has good features around time-based execution, and there aren't many. Google Apps Script running against a Sheet seems like a smart choice. You can set up time-based triggers and set them to run every minute. You can also use the Sheet as an easy logging & debugging venue.

I generally see two approaches:
  • Start a 30 minute countdown timer with each new transaction, and cancel and previous transaction (fedward's approach)
  • Poll every minute for the most recent transaction, and check if it's at least 30 minutes ago
With Google Apps Script, the latter will be easier. So the general outline is:

In IFTTT, with each transaction, write a record to the Sheet. In Google Apps Script:
  1. Every minute, read the most recent transaction from the Sheet
  2. If the last transaction was at least 30 minutes ago and in today's operating window, trigger last call
  3. LastCall() (a fitting name for the function) executes some action, e.g. sending an email to an email-SMS gateway, which notifies your bartender it's time to shut down
There will be some quirks: today's "operating window" probably spans midnight sometimes, so you can't just compare dates. IFTTT starts a new sheet after logging 2,000 rows. You'll need to make sure your code only results in one alert per night. You may need to handle cases where you have transactions 31 minutes apart and decide to stay open. In general, I think you'll want to use this tool as a guide to help make your decision, but leave the real decision up to a human.
posted by twobit at 10:24 AM on January 18, 2015


Best answer: I'd recommend Pushover to handle the notification aspect - it's very convenient.

1) Using Blockspring, make a little web service by writing a Python program.
1a) The program uses the Square Connect API to download the 1 most recent transaction. Python example here.
1b) Parse the time (which is in ISO 8601 format), and check to see if it is more than 30 minutes beyond the current time.
1c) If so, send a curl request via Python to the Pushover API: code here.
1d) Now, you essentially have a web url that, when accessed online, will check your recent transactions and potentially notify you if applicable.

2) Use a web cron service and tell it to go to that web url every minute.
posted by suedehead at 12:22 PM on January 18, 2015 [1 favorite]


I've been thinking this over and I realized that the IFTTT delay is 15 minutes per trigger. That means if you put an intervening trigger between square and the notification outcome you would have a delay between 0 and 30 minutes.

Given this, if you think that 15 minutes is an ok delay, you should see if you can set the actual notification using something other than IFTTT. For example, if no transactions for 30 minutes set a Google Calendar entry for 1 minute from now. Then let Google Calendar do the notifying, since that would be instant.
posted by If only I had a penguin... at 10:53 AM on January 19, 2015 [1 favorite]


Response by poster: Thank you for enjoying my little puzzle! Given that info about IFTTT's polling period, I really like suedehead's suggestion; I'm going to try and hack myself together a little solution using the Square API, and I'll let you guys know if I hit any interesting stumbling blocks, or publish my code/findings here if/when I achieve success.
posted by myrrh at 9:05 PM on February 1, 2015


« Older How many species have humans put out of business?   |   Statistics are hard. Newer »
This thread is closed to new comments.