Send messages Twitter with your Raspberry Pi

For this new article, KoS (author of this article) has contacted San Bergmans site http://www.sbprojects.com to talk about its tutorials. And so today KoS offers with the help of San a method to send tweets from the Raspberry via a script.

Tweet

Article translated from http://www.sbprojects.com/projects/raspberrypi/tweet.php

Before your Raspberry Pi can send Twitter messages, also known as the Tweets, you need to create a twitter account for him http://www.twitter.com/ (If you do not want your Pi tweet on your own account). But that is not all, You must also create an account on Supertweet http://www.supertweet.net/. Supertweet will be linked to the Twitter account via the 0Auth process, This will allow you to use the Supertweet API to send tweets. This is necessary because Twitter has stopped providing its own API.
You will also need to install curl, a small tool that you can install by typing the following command :
sudo apt - get install curl
Just copy the script below and paste it in a text editor such as nano on your Raspberry Pi. After you paste the text into your editor change two variables with your information. TWIT_USER is the username of your Twitter account and TWIT_SECRET is the password of your account Supertweet. Then save the file with the name Tweet in your binary directory (NdT : /default bin). Personally I always keep my scripts in the directory /Home/pi/bin, This is my way.
And don't forget to make the file executable with the command

chmod u   x tweet
NdT : Alternatively, you can change the owner of the script, What will prevent you from having to do a sudo to call it : sudo chown ft. tweet
#! /bin/bash
# Tweet
#
# This script sends tweets
# Use :
# Tweet "the string to tweeter"
# Special characters will not be taken into account if you do not delimit your string with single quotes
#
# Author: San Bergmans
#         www.sbprojects.com
# Configuration variables
TWIT_USER ="nom_utilisateur_twitter"
TWIT_SECRET ="Mot_de_pass_supertweet"
# Other variables
TWIT_URL ="http://api.supertweet.net/1.1/statuses/update.json"
TWEET = $(echo "$*"|cut-c -140)
curl u $TWIT_USER:$TWIT_SECRET-d "status = $TWEET" $TWIT_URL s > /dev/null
NdT : attention, It is possible that the address of the API changes, If the script does not work anymore, check the address on Supertweet
Once you have backed up and made the script executable , You can begin to send tweets with a command such as :
Tweet “Wow, My Raspberry Pi can send tweets through the script found on www.sbprojects.com
Remember that the tweets are limited to 140 characters. The script will simply truncate messages larger than 140 characters. And apparently it deals web links also, as you can see in the previous example.

Room for improvements

Supetweet sends an XML file after each sent tweet, containing a wealth of information. I simply delete this XML file by sending it to /dev/null. However, you can modify the script to see what Supertweet has to tell you if you want to.
You can change the behavior of the script to reject the above tweet 140 characters instead of cut off. You choose what you prefer.

Some safety advice

Twitter will ignore the same tweet sent at too frequent intervals. Which means you won't be able to send a tweet with the same text again and again. You can easily solve this problem by adding a timestamp to your message.
Furthermore, I saw that Supertweet (or Twitter) sometimes jumps a message. Therefore, You cannot be absolutely sure that your tweet is going to happen. I have no idea why it happens, It is thus. The XML message returned by Supertweet (ignored by my script) gives no idea of why some tweets are ignored.
[Chris] Share your ideas for using this script. And thanks to San for the article !

You may also like...

  • b3w (@b3w_fr)

    Good tuto, good idea.

    On the other hand, on the side of safety within the meaning “purist” of the term. Use an OAuth proxy as supertweet makes life easier, but it is not a little work around that wanted to impose Twitter security precisely ?

    After I confess that to create an OAuth and request an API Key for just post for a Raspberry Pi it is somewhat violating. But doable : http://www.bentasker.co.uk/documentation/20-developmentprogramming/23-howto-tweet-from-bash-scripts-using-oauth

  • KoS

    Personally I have a special account for the Zhao and it serves me in twitter TOR and later possibly the IP log to find the computer in the event of undesired change. Short nothing very sensitive. But in principle you are right and thank you for the link !