I want my phone's background to automatically update to a dramatic looking image when there's an urgent email in my inbox. To implement this, I need to scan my Gmail inbox for such a message and report this to my phone. I've implemented this behavior using a bit of shell scripting on a nano-sized AWS Linux server.
I can access my inbox via the Gmail API. Years ago, I wrote gmail_tool to manage an overrun Gmail inbox from the command line. I recently implemented a cleaned up version of this script, as gmailassist.
gmailassist lets me run a search for matching Gmail threads. Consider this search of my SPAM folder:
$ gmailassist -p i2x -a threads -q 'label:spam' | head -3 19baeee1f181be4b|沉默。 娘亲看起来好难过,姐姐也闷闷不乐的,可是为什么祖母跟别人都笑得这么开心涅。 “笑儿,娘舍不得你。”回屋的路上,连翘终是忍不住落下泪来,如果是京中那还好些,偏偏要去无双辣那么远的地方,不""知谷那无双云爷是什么样的人,会不会对笑儿好,嫁过去之后笑儿会不会受欺负。 越想,连翘心里越是难过,仿风已久可以看见慕容笑笑以后悲惨的生活。 “煮母,小姐受媓上赐婚,是件无上荣耀的事情, 19ba891f8fae60ae|Standard Capital Can Help Apply for a Term Loan Now As a small business owner, you understand that having access to flexible and affordable funding is essential for your success. Standard Capital is 19ba3a0df841439c|Confirmation for your recent purchase is attached,. 2026-01-10-Call Our Helpline: +1(983) 220-2512
With this command line tool in place, it's straightforward to generate a single 'inbox-status'. Here's the current logic for doing so:
checks="work_urgent|work|label:inbox_label:unread_subject:urgent"
checks="$checks personal_urgent|personal|label:inbox_label:unread_subject:urgent_category:primary"
checks="$checks wife|personal|label:inbox_label:unread_from:wife@gmail.com"
checks="$checks ok|work|ben" # [A]
now=$(date +%Y-%m-%d)
for check in $checks ; do
status=$(echo $check | cut -d'|' -f1)
profile=$(echo $check | cut -d'|' -f2)
query=$(echo $check | cut -d'|' -f3 | tr '_' ' ')
gmailassist -a threads -q "$query" -p $profile > $TMP/inbox-status.hits
hits=$(cat $TMP/inbox-status.hits | wc -l)
if [ "$hits" -gt 0 ] ; then
echo "$now $status" >> $TMP/inbox-status.log
if [ -n "$V" ] ; then
cat $TMP/inbox-status.hits
else
echo $status
fi
exit
fi
done
echo "$now unknown" >> $TMP/inbox-status.log
echo "unknown" # [B]
;;
This bit of code loops through each $checks. Each 'check' has the format:
- Status - the value that will be printed to the screen if the Gmail search returns any rows.
- Profile - the Gmail profile to search. I've set up work and personal so I can get alerts from both my personal and work email.
- Query - The search to use against Gmail. This looks odd because spaces are replaced with _. Other than this, however, this is a normal Gmail search.
In the first version of inbox-status I neglected to include the last check, [A]. I assumed that if none of the queries returned any rows, then all must be ok. The problem is, occasionally the API glitches. In this case, the searches return no rows not because they are empty queries, but because the API has failed. By setting up [A], the inbox status will only be OK if a search actively returns rows.
[B] is also essential, it says that if none of the rows match then the status is unknown. This will be skipped by the phone, so that when the API is down, the results are ignored.
Finally, I added support for a -v option when querying the inbox status. This reports the matching threads that correspond to the status. This is useful for quickly seeing which message has triggered the inbox status logic.
With a script to derive my inbox status, all that was left was to deliver this information to my phone. I make use of AutoRemote to accomplish this.
AutoRemote is a magic service that allows information to be delivered to an Android phone via a web request.
In my Linux server's crontab I have:
*/10 * * * * i2xassist -a inbox-status | andsend SetInboxStatus
andsend is little more than a simple wrapper around curl. It invokes the URL:
$ status=$(i2xassist -a inbox-status)
$ curl -s https://autoremotejoaomgcd.appspot.com/sendmessage \
-d key=YOUR_AUTO_REMOTE_KEY \
-d "message=SetInboxStatus=:=$status"
Every 10 minutes, my phone receives a SetInboxStatus message with the current inbox status. Up next is to have the phone react to this. We're almost there!
No comments:
Post a Comment