My First TwitterBot

Twitter

I have just “finished?” my first twitterbot!

If you send it a direct message with part of the name of someone who works at Eclectic it will send you their contact details as a return direct message.

The API is very straight forward, only my lack of bash skills slowed me down.

The basic structure is this:

  • Get all the direct messages into a text file
  • Store the message IDs in an array
  • Store the sender IDs in an array
  • Store the message text in an array
  • Loop through an array
  • Reply with the requested details
  • Delete the direct message
  • End Loop

I have set this up in a cron to run every minute.

I am manually following people to ensure that this does not go to the “outside” world.

Please feel free to suggest improvements to the code, it’s been a LONG time since I have had to create any “proper” code!

 #!/bin/bash
declare -a MSGIDS
declare -a SENDERIDS
declare -a MSGTEXTS
cd ~rossgoodman/scripts/twitterbot
DIRECT=./direct.txt
SEARCH=./telephone.txt
MSGIDFILE=./tmp_msgid.txt
SENDERIDFILE=./tmp_ids.txt
MSGTEXTFILE=./tmp_text.txt
SENDRESULTFILE=./tmp_sendresult.txt
DELETERESULTFILE=./tmp_deleteresult.txt
TWUSER=username
TWPASS=password
curl -u $TWUSER:$TWPASS http://twitter.com/direct_messages.xml > $DIRECT

grep "^    <id>" $DIRECT | sed -e 's/^[ /t]*//' -e 's/<id>//' -e 's/<\/id>//' > $MSGIDFILE
exec 10<$MSGIDFILE
let count=0
while read LINE <&10; do
   MSGIDS[$count]=$LINE
   ((count++))
done

grep sender_id $DIRECT | sed -e 's/^[ /t]*//' -e 's/<sender_id>//' -e 's/<\/sender_id>//' > $SENDERIDFILE
exec 10<$SENDERIDFILE
let count=0
while read LINE <&10; do
   SENDERIDS[$count]=$LINE
   ((count++))
done
exec 10>&-

grep text $DIRECT | sed -e 's/^[ /t]*//' -e 's/<text>//' -e 's/<\/text>//' > $MSGTEXTFILE
exec 10<$MSGTEXTFILE
let count=0
while read LINE <&10; do
   MSGTEXTS[$count]=$LINE
   ((count++))
done
exec 10>&-

#echo Number of elements: ${#MSGIDS[@]}
for ((x=0;x<$count;x++)); do
   let SENDERID=${SENDERIDS[${x}]}
   let MESSAGEID=${MSGIDS[${x}]}
   MESSAGETEXT=${MSGTEXTS[${x}]}
   REPLY=`grep -i $MESSAGETEXT $SEARCH`
   if [ ! "$REPLY" ]
   then REPLY="Your search for $MESSAGETEXT found nothing"
   fi
#   echo MessageID : $MESSAGEID
#   echo SenderID  : $SENDERID
#   echo Message   : $MESSAGETEXT
#   echo Reply     : $REPLY
#   echo Sending
   curl -u $TWUSER:$TWPASS --data "user=$SENDERID" --data "text=$REPLY" http://twitter.com/direct_messages/new.xml > $SENDRESULTFILE
#   echo Deleting
   curl -u $TWUSER:$TWPASS http://twitter.com/direct_messages/destroy/$MESSAGEID.xml > $DELETERESULTFILE
done

rm $DIRECT
rm $MSGIDFILE
rm $SENDERIDFILE
rm $MSGTEXTFILE
rm $SENDRESULTFILE
rm $DELETERESULTFILE

About 

    Find more about me on:
  • facebook
  • flickr
  • googleplus
  • linkedin
  • skype
  • twitter
  • youtube

7 thoughts on “My First TwitterBot

  1. It’s been many a year since I would classift myself as a programmer! I just had a problem to solve and experimented until I had something that worked!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>