This is my first Twitter updating script, written in BASH (shell script). It uses the CURL program (which is included in most systems).
The script will work with Linux, Mac OS X and also cygwin. My script can currently log in, post a new tweet and log out. But there are plenty of things you could do in between, like reading messages, sending private messages, reading tweets… If you have a feature request post it in the comments. And also don’t forget to post your own improvements! :)
Download: twitter-bot
Usage:
bash tweet.sh "My new tweet!"
And make sure that you:
- Enter a correct username and password,
- Have CURL with SSL support installed,
- Have read/write permissions in current directory.
- Run it with “bash” and not with “sh”.
#!/bin/bash #Twitter status update bot by http://360percents.com #Author: Luka Pusic <pusic93@gmail.com> #REQUIRED PARAMS username="username" password="password" tweet="$*" #must be less than 140 chars #EXTRA OPTIONS uagent="Mozilla/5.0" #user agent (fake a browser) sleeptime=0 #add pause between requests if [ $(echo "$tweet" | wc -c) -gt 140 ]; then echo "[FAIL] Tweet must not be longer than 140 chars!" && exit 1 elif [ "$tweet" == "" ]; then echo "[FAIL] Nothing to tweet. Enter your text as argument." && exit 1 fi touch "cookie.txt" #create a temp. cookie file #GRAB LOGIN TOKENS echo "[+] Fetching twitter.com..." && sleep $sleeptime initpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" "https://mobile.twitter.com/session/new") token=$(echo "$initpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//') #LOGIN echo "[+] Submitting the login form..." && sleep $sleeptime loginpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$token&username=$username&password=$password" "https://mobile.twitter.com/session") #GRAB COMPOSE TWEET TOKENS echo "[+] Getting compose tweet page..." && sleep $sleeptime composepage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L -A "$uagent" "https://mobile.twitter.com/compose/tweet") #TWEET echo "[+] Posting a new tweet: $tweet..." && sleep $sleeptime tweettoken=$(echo "$composepage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//' | tail -n 1) update=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$tweettoken&tweet[text]=$tweet&tweet[display_coordinates]=false" "https://mobile.twitter.com/") #GRAB LOGOUT TOKENS logoutpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" "https://mobile.twitter.com/account") #LOGOUT echo "[+] Logging out..." && sleep $sleeptime logouttoken=$(echo "$logoutpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//' | tail -n 1) logout=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$logouttoken" "https://mobile.twitter.com/session/destroy") rm "cookie.txt"
Actually my curl answered
curl: option --ssl: is ambiguous
until I changed both –ssl with –sslv3.
Thanks for the script!
Cheers
Fixed, thanks :)
Epic! Thanks for the fix!
Hi I’m getting this error message and can’t figure it out
./tweet.sh: line 30: unexpected EOF while looking for matching “’
Even after I comment out line thirty, I get the same error message, (still in reference to line thirty).
The only change I made was to change the variable called tweet to $1 allowing the string to be tweeted to be a parameter on the command line.
Kurt
Are you running it with “bash” and not “sh”? Which bash version are you using? Looks like there is a missing quotation mark…
The new version of the script above is accepting the tweet as $* parameters, so you can download that.
Best regards
it’s working. thanks a lot.
Doesn’t work for me. It looks like it’s working and the command completes without error, but no message is tweeted.
Maybe it’s a problem with certificates. Removed -s of the first curl line and got the error message:
problem with the SSL CA cert (path? access rights?)
Maybe it’s because I tested it from a linux box that has no web browser and has no twitter certificates.
Curl version:
curl 7.21.1 (arm-angstrom-linux-gnueabi) libcurl/7.21.1 GnuTLS/2.10.2 zlib/1.2.3
Protocols: dict file ftp ftps http https imap imaps pop3 pop3s rtsp smtp smtps telnet tftp
Features: Largefile NTLM SSL libz
I have done some more tests. Curl fetches return nothing. if I add to the commands “-cacert mobile.twitter.com”, curl fetches retrieve data, but the command still completes without error but tweets nothing.
If I get some time I’ll do some more tests.
I have done some more tests. Token is properly retrieved, but it looks like the script doesn’t even login because instead of login page (https://mobile.twitter.com/session/new) curl retrieves the default page (https://mobile.twitter.com).
Maybe the –cacert switch is causing this strange behaviour, but if I remove it, curl returns nothing but a CA Certificate error.
Very nice script, worked instantly!
worked nice!..
Thanks! Your session-based approach is much simpler than trying to muck around with OAuth!
What kind of license are you posting this code under? I would like to include it in an open source project of mine, if possible, but I need your permission :)
You can include the code in your project, but leave a reference to my site :)
BTW, people having SSL certificate trouble: just add the -k switch to curl, to force it not to validate twitter’s certificate. Or if you’re really paranoid about someone poisoning your DNS and pretending to be twitter, you’ll have to find a Certificate Authority bundle that has the right signature somewhere in it (and whose source you trust!), and tell curl to use it. See http://curl.haxx.se/docs/sslcerts.html
Thanks. it’s working!
greatttss .. its works :)..
how to add url sorten ?
For example, bitly has a simple API – here is an ugly implementation:
longurl="http://example.com" #make sure it's url encoded
bitly=`curl -s -A "$uagent" --get -d "login=LOGINNAME&apiKey=APIKEY&format=txt&longUrl=$longurl" "http://api.bitly.com/v3/shorten"`
thax very much …
it works without any change… many thanks…
thank you man.
The code runs for me. It authenticates successfully and logs out, but my tweet does not post.
It works for me, are you sure the username/password is correct and the status is not empty? I hope you solve this.
the same thing happens for me as well. it shows me logging in, getting the home page, posting and logging out. yet nothing is tweeted. double checked the login information and it is correct
What OS do you use? Give me as much debug info as you can, because i’m out of ideas…
Ubuntu 11.10
I will be happy to provide any information I can give you to get this resolved. Just let me know.
This is what I do but nothing is displayed on Twitter.
bynw@outlaw:~$ bash tweet.sh “testing again”
[+] Fetching twitter.com…
[+] Submitting the login form…
[+] Getting your twitter home page…
[+] Posting a new tweet…
[+] Logging out…
I have a pastbin of the -x debug of the script at:
http://pastebin.com/V1RCLHVC
I just got the updated version here on the site and it now works great!
Cool! I’m really glad it works :) If you find bugs in my script or have any improvements you can contribute on github https://github.com/lukapusic/twitter-bot
Thanks, worked painlessly. It should probably print an error if the user hasn’t replaced the username/password fields, though.
My debug show this message :
+ initpage=’403 Forbidden (Rate Limit Exceeded)’
++ echo ’403 Forbidden (Rate Limit Exceeded)’
Is it mean my ip is blocked?
I don’t think so, reread your error message ;)
FYI: There is ONE bug – or at least a not so nice process: If you don’t enter anything as text, the script still does connect to twitter + does all the shenannigans.
To avoid this, you simply have to add the following few lines right after the wordcount error handler:
if [ "$tweet" == "" ]; then
echo "[ERROR] Nothing to do / missing text."
exit 1;
fi
cu, w0lf.
Fun with the OS X Terminal – using Twitter
Hello,
Thank you for your Script. I’m new to Twitter Dev and I cant get any tweet on ma account. This is the cookie output. Is this normal? Thank you in advance!
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.
.twitter.com TRUE / FALSE xxxxxxxxxxxx k xx.xx.xxx.xxx.xxxxxxxxxxxx
mobile.twitter.com FALSE / FALSE 1 oauth_token
mobile.twitter.com FALSE / FALSE xxxxxxxxxxxx _mobile_sess
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx–xxxxxxxxxx
It looks ok, I think you should debug the script by echoing output, if you have some BASH experience. Or run the script with bash -x.
Hi i’ve same error cookies
COOKIES DELETED
any idea to solve?
Hey, I don’t know what could be happening. What OS do you use? Did it work before?
PS: I deleted the cookies you posted because somebody could log in to your account!
I’ve solved.
insert -k into every curl command line.
Is there a way to tweet the contents of a text file? Can’t figure out how to have the contents what comes after “bash tweet.sh “
Sure, just do a:
or, alternatively:
how can i get the first five twits of my account on the terminal …?
Been looking for an easy solution that doesn’t use OAuth for ages. This is a great solution. Thank you so much.
working good, thank you for grate job :-)
Thank you a lot. This is working pretty fine under Ubuntu 11.04.
for extra security, please add a line
umask 077before the
touch ...line
An excellent tweet script. I’ve tried quite a few but none seem to work as easily as this one does!!
It works! thanks.
thx
recently updated to Ubuntu 12.04 LTS, CURL was updated to 7.22.0, my tweets doesnt post, no error messages tho… any ideas?
At the line where the script sends the update, insert the param
--sslv3after-Land change protocol from http to https.Thanks, that fixed it:)
;)
tweet=`/usr/games/fortune /usr/share/games/fortunes/de`;
while ((${#tweet} – 140 >= 0)); do tweet=`/usr/games/fortune`; done;
I had problems with curl and SSL, so I had to put -k option to all curl calls.
Also, on Solaris, commad tail should be “tail -1l ”
But thanks for your script.
modify all http on https and added –sslv3 into all lines
Been using this script and its GREAT!
Ran into some issues recently, I had a string that says “This is Alwyn & he is sexy!”, apparently twitter only displayed “This is Alwyn “.
‘&’ breaks the script… any ideas why its like this? should I type in ASII?
You have to escape the ampersand (\&).
Brilliant! Thank you very much for the script. This is my use case:
I’ve set up a home git server. I want to use it when away from home but I’m not on a static IP. I’ve modified your script to DM from one twitter account to another. I just periodically poll for my external IP and DM myself the external IP address whenever it changes.
Thank you so much! I set up a Twitter account for my home servers. I use your script to tweet system alerts from my servers to my personal Twitter account. I wrote a script to force updates of my DynDNS account every 25 days and I use your script to tweet the notification of the update.
Hello. And Free.
How can use this script to output the latest tweet to the command line and automatically update. ie tail username timeline count=1
So, this is pretending to login on the website, rather than posting over API with oauth. I’m looking for a means to authentic with oauth and post with curl in bash. Does anybody have an example of that?
This is so unbelievable, it really worked!!! I was looking all over for something similar and found none like this! KUDOS! I just had to add the -k switch to all curl commands to make it work. Thanks much.
Jerome.
I tried this script on ubuntu, and it works with no problem. However, I run it on a redhat machine.. and get the following error:
[+] Fetching twitter.com…
[+] Submitting the login form…
[+] Getting compose tweet page…
[+] Posting a new tweet: my tweet was here
curl: no URL specified!
curl: try ‘curl –help’ for more information
line 40: https://mobile.twitter.com/: No such file or directory
[+] Logging out…
Any ideas?
Check if URL’s are quoted properly, check whitespaces, check for bad line breaks…
Works great in Ubuntu Lucid! Thanks a lot!
BTW, any ideas on extending the script to include photos uploading?
Any PHP fan who converted this script to PHP ? A PHP version would be really good….
Here you go! :) It uses the command line, just like the original. If you want to use it via browser, you’ll want to change $argv[1] to $_POST["tweet"] or whatever and add instead of the \n, or mute it altogether. Obviously it requires PHP with cURL enabled.
Well this kinda sucks. CODE tags were ignored and I didn’t remove by username & password :P Obviously changed it, but it would be nice to have this post fixed.
Oh great, and it thought my Regex was HTML and completely removed it :P Script is unusable as it is posted now. Look here instead: https://gist.github.com/4288384
Found a bug. Stuff sent via post needs to be urlencode()’d, otherwise problems will arise when characters such as & or ? are in the password or tweet. Here’s the updated version: https://gist.github.com/737f942943d8b62b555b
Hi dawt!
Thanks for your contribution! I edited the original comment and replaced the code with the version from Pasebin. Sorry for taking so long.
Best regards
Tweeting OpenBSD Network IP Changes With Minimal Dependencies | Adam Flott
Предлагаем работу дома 4000$ в неделю!
Сайт http://pootxsm.ru/ Там все подробности!
it Work… thanks!!
Technische Informationen 2012 (Archiv) | EarlyBird
Technische Informationen 2012 | EarlyBird
AWESOME !