Fetch the latest tweet when compiling

After some experiments with other static site generators (including, but not limited to, Zola and Pelican), I am currently playing with Hugo again. As it would - if it works out - replace a dynamic website, I wonder how many features I would have to give up.

Among the features of my current website is a Twitter widget that dynamically fetches my latest tweet (via PHP) and displays it in my sidebar. Now Hugo has a built-in shortcode that lets me display a certain tweet, but I can’t find a way to determine my newest tweet. I could probably run a separate API client as a part of my compilation, but that would add more overhead and complicate my build process.

Are there any solutions for this problem yet?

Do you want to show the lastest tweet, or the latest tweet as of the build date?

The latest tweet as of the build date. I am not interested in loading dynamic JavaScript from third-party servers on any of my sites.

Be advised that the “built-in” shortcode you referred to does just that, embedding something like this:

<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Owl bet you&#39;ll lose this staring contest 🦉 <a href="https://t.co/eJh4f2zncC">pic.twitter.com/eJh4f2zncC</a></p>&mdash; San Diego Zoo Wildlife Alliance (@sandiegozoo) <a href="https://twitter.com/sandiegozoo/status/1453110110599868418?ref_src=twsrc%5Etfw">October 26, 2021</a></blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

Not necessarily; and, as I live in Germany, I should probably use that.

Yeah, it’s this one:

{{< twitter_simple user="SanDiegoZoo" id="1453110110599868418" >}}

Which renders to:

    <style type="text/css">
      .twitter-tweet {
        font: 14px/1.45 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;
        border-left: 4px solid #2b7bb9;
        padding-left: 1.5em;
        color: #555;
      }
      .twitter-tweet a {
        color: #2b7bb9;
        text-decoration: none;
      }
      blockquote.twitter-tweet a:hover,
      blockquote.twitter-tweet a:focus {
        text-decoration: underline;
      }
    </style>
  <blockquote class="twitter-tweet"><p lang="en" dir="ltr">Owl bet you&#39;ll lose this staring contest 🦉 <a href="https://t.co/eJh4f2zncC">pic.twitter.com/eJh4f2zncC</a></p>&mdash; San Diego Zoo Wildlife Alliance (@sandiegozoo) <a href="https://twitter.com/sandiegozoo/status/1453110110599868418?ref_src=twsrc%5Etfw">October 26, 2021</a></blockquote>

And looks like:

1 Like

Now the relevant question remains: How can I fetch the latest id by a given user during the compilation?

The source for the embedded shortcodes:

Just copy these into your layouts/shortcodes directory and edit as needed.

See Twitter timeline API docs.

1 Like