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:
- Get access to Labs
- Tell Shortcuts to generate a bearer token
- Get our latest Tweet using Labs
- Ask Shortcuts to translate the Tweet
- 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:
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:
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:
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:
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:
- Select Get contents of URL.
- Use
https://api.twitter.com/oauth2/token
as the URL. - Tap Show more.
- In Method, select POST.
- In Headers, add Basic, then select ⚙️ Base64 Encoded
- In Request Body, select Form.
- Add a field called
grant_type
. Its value should beclient_credentials
.
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:
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".
Let's add these next steps to get the actual Tweet 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.
💬 ¡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.
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!