DEV Community

Daniele for XDevelopers

Posted on

Hey Siri, text my latest Tweet

As someone whose first language isn’t English, I tend to find myself Tweeting and dreaming in English; many of my friends do not. And I'm too lazy to translate my Tweets.

I also happen to like iOS 13 a lot. Dark Mode looks fantastic, and I'm enjoying Memojis a little too much. But my favorite app has to be Shortcuts. It ships by default with iOS 13, and it's more powerful than ever. With Shortcuts, you can instruct Siri to do things for you, including tasks no other app can do.

Shortcuts is a true hidden gem; its power and ease of use allows you to build automations without writing a single line of code. The app is extremely powerful; among other things, it has a HTTP client and a JSON parser. This is all you need to connect to any API.
So I thought, what if I can translate my latest Tweet, and send it only to my friends? Shortcuts can translate for me, and I can use Twitter Developer Labs to get my Tweets. Labs is our program where we’re building our next API in the open by inviting developers to help shape the future of the Twitter API. You can test drive new features and even full endpoints before they are broadly released, and you can tell us what you like and we can improve through our feedback channel (we already shipped some features based on feedback from people like you). It's free and you can try it out now.
Here's what we're going to do:

  1. Get access to Labs
  2. Tell Shortcuts to generate a bearer token
  3. Get our latest Tweet using Labs
  4. Ask Shortcuts to translate the Tweet
  5. Send an iMessage with the Tweet

🧪 Enter Labs

If you already activated Tweets and users you can jump to the next section.

For this tutorial, you need access to Labs and the API. First though, you will need a valid app – if you don't have one, you can apply on our Developer site.

Once you have an app, join Labs and follow the instructions (it only takes a minute). When done, you should see a list of endpoints:

A list of endpoints. Under

In this tutorial, you're going to deal with Tweets and users, so select Activate for that row, and attach the app you want to enable (you can enable one app per endpoint, but multiple endpoints can be attached to the same app).
Now, go to your app list, locate the app you've just enabled for Tweets and users, and take note of the consumer key and secret.

🤖 Coding with no code

Now we'll start coding… With no code. Shortcuts allows you to assemble tasks together, and if you know how to use an API, it's even more flexible.

Open the Shortcuts app (if you don't have it, you can download it for free from the App Store). Tap Create Shortcut.

On the first screen, tap Add Action. You'll see a lot of options, but you simply need a place to store your configuration (your credentials and your Twitter username). To do so, you're going to use a dictionary:

This animation shows how to tap Add action, and type Dictionary to select a Dictionary.

Configure the dictionary with three keys:

  • consumer key with your consumer key
  • consumer secret with your consumer secret
  • username with your Twitter username (without @)

In my case, it looks like this:

My dictionary filled with three keys and values

Now let's request a Bearer token. To do so, we need to call the Twitter API by combining your consumer key and secret with a colon, for example, consumer key:consumer secret, then encode in base64, and send the resulting string in the Authorization header. In Shortcuts, we simply have to add a text component to combine the credentials, then add a Base64 Encode step:

Alt Text

Next, create your actual request. Like I said, Shortcuts contains a full HTTP client, so for this step you just have to fill in the blanks:

  1. Select Get contents of URL.
  2. Use https://api.twitter.com/oauth2/token as the URL.
  3. Tap Show more.
  4. In Method, select POST.
  5. In Headers, add Basic, then select ⚙️ Base64 Encoded
  6. In Request Body, select Form.
  7. Add a field called grant_type. Its value should be client_credentials.

This steps should have the Authorization header set to Basic, and the Request Body set to Form.

Shortcuts has built-in JSON support. Our response will be JSON, so all we have to do is to tell Shortcut to retrieve the access_token field. Just add a Get Dictionary Value step after your request:

Select the Dictionary Value step and drop it as the current step.

Get your latest Tweet

You’re now ready to get your latest Tweet, and Labs is going to help us a lot. In Tweets and users, there are two API endpoints (one to get Tweets, one to get users). In this case, you're going to use the users endpoint, because it always returns the ID of the most recent Tweet from the users you're requesting. But wait, you need the full Tweet, not just its ID! How can you avoid making another request just to get the full Tweet?

Thankfully, one of my favorite Labs features is going to help you a lot here. It's called expansions, and it allows you to get a full object instead of its ID. It works like this:

  • If I request /labs/1/users?usernames=i_am_daniele I get my user and most recent Tweet ID back
  • If I request /labs/1/users?usernames=i_am_daniele&expansions=most_recent_tweet_id I get my user and my most recent Tweet back (including its text!)

So now you’re going to create two steps. The first is a simple Get value for username from the dictionary you created at the very beginning. The second step is another Get contents of URL step configured like this:

  • Get contents of https://api.twitter.com/labs/1/users?usernames= Dictionary value &expansions=most_recent_tweet_id (yes, you can combine strings and variables together)
  • Tap Show more
  • Add a Header named Authorization. Its value should be Bearer (leave a space after Bearer)
  • Tap the magic wand icon on your keyboard, scroll back up a little, then tap on the "Dictionary value" label from "Get value for access_token in Contents of URL".

Alt Text

Let's add these next steps to get the actual Tweet text:

Alt Text

🇺🇸🇪🇸 Translate

Now that you have your Tweet, you can ask Shortcuts to translate it. The app is integrated with Microsoft Translator, so all you have to do is to add the Translate Text with Microsoft block to your workflow.

Alt Text

💬 ¡Enviamos!

Now it's time to send your text. First, combine the translated Tweet with a URL via a Text snippet, and then add a Send Message block. You can disable Show More -> Show when Run so your action runs seamlessly.

Alt Text

If you have Hey Siri enabled, try saying "Hey Siri, text my latest Tweet" to always be in touch with your friends, no matter what the language of your Tweets!

Top comments (3)

Collapse
 
allwafe_10 profile image
Alwafe
Collapse
 
jessicagarson profile image
Jessica Garson

So amazing!

Collapse
 
allwafe_10 profile image
Alwafe