Start > Linux Tips & Trics > SMS to Twitter

SMS to Twitter

31 maart 2010

I’ve wrote this shell script that uses an SMS to post to Twitter. It is ment to be used as an Eventhander for SMS Server Tools, but it’s simple enough for stand alone usage with simple text files.

If used as an Eventhandler the arguments are “RECEIVED” and the path to the SMS file, add “eventhandler=/path/to/sms2twitter.sh” to your smsd.conf. Without SMS Server Tools you can also use a simple file for posting to twitter:
Create a file starting like this:

  1. Phone: 0634567890
  2.  
  3. Your tweet in max. 140 characters

Then call this script with the path to your file:

  1. sms2twitter.sh /path/to/file.txt

You will need to create a sqlite3 DB with this schema:

  1. sqlite3 sms2twitter.db \
  2.     "CREATE TABLE account (phone, username, password, PRIMARY KEY(phone))"

For every Phone that you allow to post Tweets thru SMS you need to run:

  1. sqlite3 sms2twitter.db \
  2.     "INSERT INTO account VALUES (‘phonenumber’, ‘username’, ‘password’)"


This is the bash script (click here to download):

  1. #!/bin/bash
  2.  
  3. DB="sms2twitter.db"
  4. CURL=`which curl`
  5. SQLITE3=`which sqlite3`
  6. FORMAIL=`which formail`
  7. UPDATE_URL="http://api.twitter.com/1/statuses/update.json"
  8.  
  9. test -f "$DB" || {
  10.         echo "DB $DB not found" 1>&2
  11.         exit 1
  12. }
  13.  
  14. if [[ -z $CURL ]]
  15. then
  16.         echo "CURL not found" 1>&2
  17.         exit 1
  18. fi
  19.  
  20. if [[ -z $SQLITE3 ]]
  21. then
  22.         echo "SQLITE3 not found" 1>&2
  23.         exit 1
  24. fi
  25.  
  26. if [[ -z $FORMAIL ]]
  27. then
  28.         echo "FORMAIL (part of procmail) not found" 1>&2
  29.         exit 1
  30. fi
  31.  
  32. usage()
  33. {
  34.         echo "usage: $0 [RECEIVED] ^lt;smsfile>"
  35. }
  36.  
  37. test -z "$1" && {
  38.         usage
  39.         exit 1
  40. }
  41.  
  42. #if used as a direct Eventhandler then we need to test for the 1st argument:
  43. if [ $# -gt 1 ]
  44. then
  45.         STATUS=$1
  46.         if [ "$STATUS" != "RECEIVED" ]
  47.         then
  48.                 echo "expected RECEIVED status" 1>&2
  49.                 exit 2
  50.         fi
  51.         shift
  52. fi
  53.  
  54. SMS=$1
  55.  
  56. test -f "$SMS" || {
  57.         echo "SMS file not found" 1>&2
  58.         exit 1
  59. }
  60.  
  61. PHONE=$(formail -zx From: < $SMS)
  62.  
  63. test -z "$PHONE" && {
  64.         echo "Failed to extract Phonenumber from SMS" 1>&2
  65.         exit 1
  66. }
  67.  
  68. #search database for phone:
  69. ROW=$($SQLITE3 $DB \
  70.     ‘SELECT username, password FROM account WHERE phone="’$PHONE‘"’)
  71. test -z "$ROW" && {
  72.         echo "no account for phone $PHONE" 1>&2
  73.         exit 2
  74.        
  75. }
  76.  
  77. USERNAME=$(echo "$ROW"|cut -d‘|’ -f 1)
  78. PASSWORD=$(echo "$ROW"|cut -d‘|’ -f 2)
  79.  
  80. if [[ -z $USERNAME ]] || [[ -z $PASSWORD ]]
  81. then
  82.         echo "invalid credentials for phone $PHONE" 1>&2
  83.         exit 3
  84. fi
  85.  
  86. #get message from SMS:
  87. MSG=$(formail -I "" < $SMS | sed -e"1d")
  88.  
  89. if [ ${#MSG} -gt 140 ]
  90. then
  91.         echo "tweets can only be 140 characters long" 1>&2
  92.         exit 3
  93. fi
  94.  
  95. if [ ${#MSG} -eq 0 ]
  96. then
  97.         echo "empty tweet" 1>&2
  98.         exit 3
  99. fi
  100.  
  101. ERROR=$($CURL -s -S \
  102.     -H "X-Twitter-Client: PDP SMS Gateway" \
  103.     -H "X-Twitter-Client-Version: 1.0" \
  104.     -H "X-Twitter-Client-URL: http://www.pictura-dp.nl/twitterclient.xml" \
  105.     -u "$USERNAME:$PASSWORD" \
  106.     -d "status=$MSG" \
  107.     $UPDATE_URL \
  108.          | grep ‘"error"’ \
  109.          | sed ‘s/.\+"error":"\(.\+\)".*}/\1/’
  110. )
  111.  
  112. if [ ! -z "$ERROR" ]
  113. then
  114.         echo $ERROR 1>&2
  115.         exit 4
  116. fi
  117.  
  118. exit 0
Categorie├źn:Linux Tips & Trics Tags:, , ,
Geen reacties mogelijk.