<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
Using mutt & cron To Text Yourself The Weather
</title>
<meta name="generator" content="//dj-chase.com/Make.py">
<link rel="stylesheet" href="../style.css?cache-bust=2024-02-15">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<nav>
<a href="#start">Skip to start</a>
<!-- screen-reader–only p -->
<p>You are here:</p>
<ol>
<li><a href="/" aria-label="home">⌂ <span>›</span></a></li>
<li><a href="/documents/">documents <span>›</span></a></li>
<li><a href="/documents/mutt-cron-text-yourself-weather.gmi">mutt-cron-text-yourself-weather.gmi</a></li>
</ol>
</nav>
<main id="start">
<h1>
Using mutt & cron To Text Yourself The Weather
</h1>
<p>
I have a flip phone (it’s much less distracting than a smartphone), so I cannot use an app or a browser to check the weather when getting dressed in the morning. I tried NOAA’s dial-a-forecast, but the number for my state is out of service.
</p>
<p>
So, I decided to make a script that texts me the weather. This was surprisingly simple. It broke down into three steps:
</p>
<ol>
<li>Get the weather
</li>
<li>Text it to myself
</li>
<li>Automate that
</li>
</ol>
<p>
This post assumes that you have email working on a system-level. Since setting that up is out-of-scope for this post, you can either do it yourself or get an account on a <a href="https://tildeverse.org/">pubnix</a>.
</p>
<h2>
Step one: getting the weather
</h2>
<p>
There are two ways I went about this. At first, I just used wttr.in in image-mode (to avoid text-wrapping issues). This worked well but I generally prefer NOAA’s forecast, so I eventually switched to the weather.gov API. <a href="https://wttr.in/:help">wttr.in</a> <a href="https://www.weather.gov/documentation/services-web-api">weather.gov API</a>
</p>
<p>
The wttr-version of the script looks like this:
</p>
<pre>
#!/bin/sh
# POSIX sh lacks mktemp
umask -S u=rw,g=,o= > /dev/null
unset WEATHER
trap 'rm -f "$WEATHER"' INT TERM HUP EXIT
WEATHER="/tmp/$$.$USER.$(awk 'BEGIN{srand(); print rand()}').png"
:> "$WEATHER"
curl --silent https://wttr.in/LOCATION.png?1Fqn > "$WEATHER"
</pre>
<p>
Replace <em>LOCATION</em> with your location / zip-code.
</p>
<h2>
Step two: texting it to myself
</h2>
<p>
Now that we have the weather, it’s time to text it. I thought this part would be challenging, but it turned out to be very simple. I found out that most US carriers provide an email-to-MMS gateway, which meant that I could just send myself an email and it would show up as a text.
</p>
<p>
I chose mutt instead of something POSIXly correct because attachments are really simple with mutt, and this is just a hack.
</p>
<p>
The full script now looks like this:
</p>
<pre>
#!/bin/sh
# POSIX sh lacks mktemp
umask -S u=rw,g=,o= > /dev/null
unset WEATHER
trap 'rm -f "$WEATHER"' INT TERM HUP EXIT
WEATHER="/tmp/$$.$USER.$(awk 'BEGIN{srand(); print rand()}').png"
:> "$WEATHER"
curl --silent https://wttr.in/LOCATION.png?1Fqn > "$WEATHER"
mutt -a "$WEATHER" -- 8005551234@mms-gateway.example
</pre>
<h2>
Step three: automating it
</h2>
<p>
I then had a script that texted me the weather when run, but I wanted to get the weather every morning. To do this, I just made a simple cronjob (<code>run crontab -e</code>):
</p>
<pre>
# minute hour day-of-month month day-of-week command
0 10 * * * /home/u9000/bin/pushweather.sh
</pre>
<p>
That’s it. This project was surprisingly simple but very fun, and I use it daily.
</p><!-- html code generated by txt2tags 3.3 (http://txt2tags.org) -->
<footer>
<p>
Questions, comments, or wrote a reply? <a href="mailto:u9000@posteo.mx">Email me</a>.
</p>
<p>
© DJ Chase 2022-06-10. Licensed under the Academic Free License (AFL 3.0).
</p>
<p>
<a href="/cgi-bin/cite.sh">Cite this page</a>
</p>
<p>
<a href="https://u9.tel/mutt-cron-weather">Short link</a>
</p>
</footer><!-- cmdline: txt2tags -->
</main>
</body>
</html>
Response:
20 (Success), text/plain