My First TwitterBot

Written by

in

,

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

Comments

7 responses to “My First TwitterBot”

  1. Ron Barber avatar

    Hi…
    This looks good…
    How can you be sure that the DM sent is actually there and not everyone ?…
    Thanks…
    Ron…

  2. Ross (Admin) avatar

    I don’t really understand your question.
    Can you re-phrase it please?

    Thanks

    Ross

  3. @vkvraju avatar

    Hi, Thank You for a nice post.
    Neither I know bash programming nor curl. But looks like this can be done using C/C++ too using curl (which I have to learn now).
    Your script was well detailed and I was able to understand what you were trying to do. Thanks again.
    .-= @vkvraju´s last blog ..Having problems installing software at office? =-.

  4. @vkvraju avatar

    Btw (off-topic), even I am using the same theme for my blog!! 🙂
    .-= @vkvraju´s last blog ..Having problems installing software at office? =-.

  5. Ross (Admin) avatar

    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!

  6. jual bali ratih avatar

    Hi, Thank You for a nice post.
    its more easy use php

  7. Ross (Admin) avatar

    Agreed, but I don’t really know PHP, this was more if an intellectual challenge !

Leave a Reply

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