Linux Computer Tweeting.

by Computothought in Circuits > Linux

3669 Views, 16 Favorites, 0 Comments

Linux Computer Tweeting.

twitter-bird-logo.jpeg
Done this for quite a while and I love it. Real computer automation. This is an instructable to get your linux computer to access twiiter via the command line for both sending and receiving tweets. The great thing about this is it will work on a system without a gui. An older computer would be perfect for this project. In fact I run my tweeting computer without a monitor and access the tweeting computer via ssh. You can even receive tweets and turn them in to commands to be executed by the computer. That saves having to open a port to your network to access it. Gave you just enough to work with.

Another instructable, but you could add software kind of like the Dr. Eliza program to almost have a natural language interface. This could be an automated  help center or even some kind of auto response chat line. You could go one step further and have the tweeting computer issue commands to other computers behind firewalls without having to open a port or human intervention all via a twitter account or accounts.

Note: Twidge is available for the Apple Macintosh and you could probably use almost the same batch files.. Microsoft Windows computers may be able to do the same thing with a program called "tweetc". You definitely you have to use different batch files though. (not a subject of this instructable).

Update: Set up your own twiiter like server: https://www.instructables.com/id/Statusnet-the-Twitter-clone-setup/

Get a Spare Email Account.

Screenshot-Yahoo! Registration - Mozilla Firefox.png
You can use whatever provider you want. I usually use yahoo.  So that I do not forget the name I will use the same name I plan to use for twitter.

Set Up Twitter Account.

Screenshot-Twitter - Create an Account - Mozilla Firefox.png

Go to www.twitter.com and join. Try to use the email username that you used to set up the mail.

Download and Install Twidge

Screenshot-Twitter - Authorize an application - Mozilla Firefox-1.png

You should be able to install twidge from you linux repository such as:

$ sudo apt-get install twidge.

or you will have to install your version from: https://github.com/jgoerzen/twidge/downloads.

Besure to get the right version for your computer's cpu,

I downloaded the debian version .deb file and installed it on Ubuntu 10.04 with:

$ dpkg -i twidegepackagename.deb

Once it is installed, you will need to run this (on the server I usually ssh into the server so that I can use the browser of the remote machine.):

$ twidge setup

It will ask you for your twidge username and password.
Enter name and password. (this will be kept in a plan text file know as .twitterrc i think.
You will then be asked to paste a url into the browser to get a security code to enter.
I usually take a screenshot of this page for documentation purposes.

You should be returned back to the command prompt.

twidge setup
Welcome to twidge. We will now configure twidge for your use with Twitter (or a similar service). This will be quick and easy!

Please wait a moment while I query the server...

OK, next I need you to authorize Twidge to access your account. Please cut and paste this URL and open it in a web browser:

https://api.twitter.com/oauth/authorize?oauth_token=somekind of text

Click Allow when prompted. You will be given a numeric key in your browser window. Copy and paste it here. (NOTE: some non-Twitter services supply no key; just leave this blank if you don't get one.)

Authorization key: NNNNNNN Successfully authenticated! Twidge has now been configured for you and is ready to use.

Sending Tweets.

Manually sending a tweet.

$ twidge update "text to be sent to your twitter account."

Sending a message to a particular account.
 
$ twidge dmsend username "Message goes here."

Downloads

Getting Tweets.

PCDV0208.JPG

Try at your own risk. I am assuming you know how to use twitter and twidge. Just some example scripts to experiment with:
Possible command to enter into twitter:

echo Hello world!

robot
———

###################################
# twitter control
#
#=================================
# daffynitions
# ——————————–
username=”[accountname]“
datafile1=”inlist”
datafile2=”joblist”
datafile3=”jobsdone”
a=1
#===========================================
# code
#———————————————
# get data
twidge -c twidgetest lsrecent -su > $datafile1
# strip username
sed -i ‘s/ //g’ $datafile1
# reverse file order
tac $datafile1 > $datafile2
# do commands in order
while read line
do injob=$line
echo ./dojob $injob > job
./job $1 $2 $3 $4 $5 $6 $7 $8 $9
done > $datafile3
#===================================
# End.
####################################

job
—-

./dojob

dojob
——-

command=”$1″
comment=”Job #$$”
hdr “$comment” “$command” “$USER”
$1 $2 $3 $4 $5 $6 $7 $8 $9
ftr “$comment” “$command” “$USER”

hdr
———–

echo “#————————————————————#”
echo ” COMPANY NAME: $1″
echo ” $3 is running: $2″
echo “#————————————————————#”

ftr
———

echo “#————————————————————#”
echo ” COMPANY NAME: $1″
echo ” $3, your job: $2 is done.”
echo “#————————————————————#”

original robot
————————

###################################
# twitter control
#
#=================================
# code
# ——————————–
datafile=”todolist”
twidge -c twidgetest lsrecent -su > $datafile
sed -i ‘s/ //g’ $datafile
while read line
do injob=$line
echo ./tdojob $injob > job
./job $1 $2 $3 $4 $5 $6 $7 $8 $9
done < $datafile
#===================================
# End.
####################################

#!/bin/bash
#Get Recent Direct Messages sent to directmessage_account
echo `twidge lsdmarchive -us` | grep directmessage_account >> /path/to/todo.txt
#Remove sender/recipiant information since I like to keep
sed -i 's/<twitterusername> <directmessage_account> //g' /path/to/todo.txt
#Remove blanklines to keep todo.txt formated right
sed -i '/^$/d' /path/to/todo.txt
#Search for keyword "dnes" and if it is there send a direct message to myself with the things that are due the next day and if not say "Not Found"
if grep -q "dnes" "/path/to/todo.txt" ; then
grep `date --date '1 day' '+%m%d%y'` "/path/to/todo.txt" | sed 's/ /_/g' | xargs twidge dmsend twitterusername && sed -i 's/dnes//g' /path/to/todo.txt
else
echo "Not found"
fi

Using cron to run programs on a schedule

cron is a Linux system process that will execute a program at a preset time. To use cron you must prepare a text file that describes the program that you want executed and the times that cron should execute them. Then you use the crontab program to load the text file that describes the cron jobs into cron.

Here is the format of a cron job file:

[min] [hour] [day of month] [month] [day of week] [program to be run]

where each field is defined as
[min] Minutes that program should be executed on. 0-59. Do not set as * or the program will be run once a minute.
[hour] Hour that program should be executed on. 0-23. * for every hour.
[day of month] Day of the month that process should be executed on. 1-31. * for every day.
[month] Month that program whould be executed on. 1-12 * for every month.
[day of week] Day of the week. 0-6 where Sunday = 0, Monday = 1, …., Saturday = 6. * for every day of the week.
[program] Program to be executed. Include full path information.

Here are some examples:

0,15,30,45 * * * * /usr/bin/foo

Will run /usr/bin/foo every 15 minutes on every hour, day-of-month, month, and day-of-week. In other words, it will run every 15 minutes for as long as the machine it running.

10 3 * * * /usr/bin/foo

Will run /usr/bin/foo at 3:10am on every day.

10 * 1 * * /usr/bin/foo

Will run /usr/bin/foo at 12:10am on the first day of the month.

10 * * 1 * /usr/bin/foo

Will run /usr/bin/foo at 12:10am on the first month of the year.

10 14 * * 1 /usr/bin/foo

Will run /usr/bin/foo at 2:10pm on every Monday.

There are more options for these. See man man crontab -S 5.

You must use crontab to load cron jobs into cron. First create a text file that uses the above rule to describe the cron job that you want to load into cron. But before you load it, type crontab -l to list any jobs that are currently loaded in crontab.

If none are listed, then it is safe to load your job. Example. If you wanted to run /usr/local/bin/foo once a day at 3:10am, then create a text file

10 3 * * * /usr/bin/foo

Save it as foo.cron. Then type crontab foo.cron. Check to see if it was loaded by typing crontab -l. It should display something like this:

# DO NOT EDIT THIS FILE – edit the master and reinstall.
# (ipwatch.cron installed on Thu Nov 18 11:48:02 1999)
# (Cron version — $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
10 3 * * * /usr/bin/foo

If you want to edit the cron job, then edit foo.cron and then remove the existing cron job (crontab -r) and load it again (crontab foo.cron). You can have multiple jobs. Just put each different one on a seperate line in foo.cron.

contab jobs will run under the user that was in effect when you loaded the job in crontab.

See man cron, man crontab and man crontab -S 5 for more information.

Security Reporting System.

PCDV0131.JPG
PCDV0115.JPG
One is to use the original lizzysaf.bas and modify it or use modify our existing set of server files,

See https://www.instructables.com/id/Security-system-with-old-pentium-1/ for more details.

lizzysaf.bas over simplified. (Note: you would have to have networking enabled and twidge installed on the system. Probably required a beefier system than just something floppy based.).
Replace

domodem:
OPEN "com1:1200,n,8,1" FOR RANDOM AS #2
PRINT #2, "ATDT1234567;"
FOR clickon = 1 TO 10000: NEXT clickon
PRINT #2, "DT121212121212121212121212121212121212121212121212121212121"
FOR clickoff = 1 TO 10000: NEXT clickoff
CLOSE #2
RETURN

with:
domodem:
shell "twidge update "door is being attacked""
RETURN

Compile lizzysaf.bas

$ sudo ./lizzysaf &

and leave running

At this point if you got the tweet you could ssh into the machine and look at the datafile with history of the door recording.

Downloads

Experimenting With Gmail.

gmail.jpg
You can also grab your gmail and save it as ascii and do the same sort of thing. It will be more private and you do not have to use twitter.

You could use the following as a starting point.

getgmail.sh
[code]
username="Yourusername"
pword="Yourpassword"
curl -u $username:$pword --silent "https://mail.google.com/mail/feed/atom" | tr -d '\n' | awk -F '<entry>' '{for (i=2; i<=NF; i++) {print $i}}' | perl -pe 's/^<title>(.*)<\/title><summary>(.*)<\/summary>.*?<name>(.*?)<\/name>.*$/\n$3\n\t$1\n\t$2/' >> currentmail
[/code]

Make it executable.
$ chmod +x getgmail.sh

Have it check for mail every hour
$ crontab -e

*  */1  * * * /home/eddie/bin/getgmail.sh

gedit/vim/nano currentmail

currentmal:
[text]
me
    inre. project.
    Marie: We have decided to go ahead with the project. Good luck
[/text]