Tweet from Raspberry Pi with Wolfram

By Russell Barnes. Posted

How to easily send tweets in the Wolfram Language, using the SendMessage function

The Wolfram Language is a powerful knowledge-based language that makes computation accessible to everyone, and is an easy language to dip your toes into as a first-time programmer. To send tweets from Twitter on your Raspberry Pi, all you need is a single function. We’ll be using SendMessage, which is a multipurpose function in the Wolfram Language that gives Raspberry Pi users the ability to seamlessly tweet from their device.

The full article can be found in The MagPi 42 and was written by Dana Flinn and Brett Haines

Using the SendMessage function, we'll walk you through how to apply the symbolic language and tweet using your Raspberry Pi. Don’t feel limited to just tweeting with this function, though! SendMessage can also be used to generate an email, update a Facebook or LinkedIn status, and generate a beep.

You'll need

A Twitter account
The Wolfram Language Documentation Center
Internet access

STEP-01 Prepare the syntax

First, you need to launch your copy of Mathematica on your Raspberry Pi and open a blank notebook. With over 5,000 functions built into the Wolfram Language, it can seem intimidating at first. However, using the Documentation Center will make it simple to find the right function for your project.

In the Wolfram Language, all functions use square brackets and have names that start with capital letters. The function needed for this project is SendMessage, which is rather straightforward. It’s used to send a message to a specified channel, which in our case is Twitter.

STEP-02 Setting up your code

To set up your code, open Mathematica on your Pi and type the SendMessage function, then the text you want your tweet to say. So, from the Documentation Center, we know it should be set up like this:

SendMessage["Twitter", "Tweet text"]

Where it says “Tweet text”, the text can be changed to anything you want posted to your Twitter account.

STEP-03 Evaluate your code

Once you’ve typed that in, press SHIFT+ENTER. This will run your code in Mathematica and prompt an authorisation dialogue to allow the Wolfram Language to send tweets on your behalf. Once authorised, a tweet is sent to your account. Select ‘Done’ after entering the access key, and the tweet will appear in your Twitter stream.

STEP-04 Create a variable

The previous example showed a static string being tweeted. However, this is far from all the Wolfram Language is capable of. Much like other programming languages, it allows users to set variables, in which we can store whatever we like. To create a variable, just type the name you want to give it, an equals sign, then whatever you want to assign to the variable, like so:

myVariable = "Some string here!"

Unlike several other programming languages, you don’t need to specify what kind of data is being stored in your variables – the Wolfram Language is smart enough to figure out what it is and how to use it on its own!

STEP-05 Using StringJoin

You can combine strings using the StringJoin function. Just like SendMessage, StringJoin is used by putting the strings you want to join in between square brackets ([ and ]), separated by commas. So, to join “Hello ” and “world”, you’d enter:

StringJoin["Hello ", "world"]

This isn’t limited to static strings – variables can be joined to strings too, like so:

StringJoin["Hello ", myVariable]

STEP-06 Combining it all

Now it’s time to put it all together. We can take a variable, store a string to it, join it to another string, then send it to Twitter:

myName = "WolframLanguage"
SendMessage["Twitter", StringJoin["This tweet brought to you by ", myName]]

Now that you know how functions work in the Wolfram Language, it’s easy to dive into the documentation and explore the thousands of functions built in. Now go forth and code!

 The full code is very simple, as shown here

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.