#!/usr/local/bin/bash

# Tweet Listen - a simple demonstration Twitter robot that can be 
#   tied to a Twitter account and respond to predefined queries.
#   This could be used on its own account (see @davesbot) or it
#   could foreseeably be listening to your own Twitter stream and
#   answering queries that it understands.
#
# (c) 2010 by Dave Taylor. Written for LinuxJournal.com
#     Permission is granted to use, hack, etc., as long as this
#     original copyright statement is retained.
#
# Finally, note that this is a BASH script and hiccups all over 
#   the place if you try to run it with /bin/sh on most Linuxes

 curl="/usr/local/bin/curl -s"
inurl="http://www.twitter.com/statuses/mentions.xml"
 user="davesbot"		# set to your twitter ID
   pw='xxREDACTEDxx'		# obviously fix the pw!
 temp="/tmp/$(basename $0).$$"

# the following needs to be a writeable directory to keep
# track of what has or hasn't been answered

lastidcache="/home/taylor/scripts/davesbot/.last-tweet-id"

# the "tweet.sh" script can be found at linuxjournal.com

tweet="/home/taylor/scripts/davesbot/tweet.sh"

trap "/bin/rm -f $temp" 0 1 9 15	# axe our temp file

$curl -u "${user}:$pw" $inurl | \
  grep -E '(<screen_name>|<text>|<id>)' | \
  sed "s/@$user //;s/  <text>//;s/<\/text>//" | \
  sed 's/ *<screen_name>//;s/<\/screen_name>//' | \
  sed 's/ *<id>//;s/<\/id>//' | \
  awk '{ if (NR % 4 == 0) { printf ("name=%s;\n", $0) } else 
         if (NR % 4 == 1) { printf ("id=%s; ",$0) } else 
         if (NR % 4 == 2) { printf ("msg=\"%s\"; ", $0) } }' > $temp

# manage our last ID tracking system

latestid=""	# latest of the current batch
answered=0	# haven't answered any yet

if [ -f "$lastidcache" ] ; then
  previouslatestid="$(cat "$lastidcache")"
else 
  previouslatestid="0"
fi

# the main read and process loop

while read buffer
do
  eval $buffer
  # strip out the &quot; sequences, etc, along with punctuation
  msg="$(echo $msg | sed 's/\&quot;//g' | tr -cd ' [:alpha:]')"

  if [ -z "$latestid" ] ; then
    latestid=$id	# store most recent ID
  fi

  if [ "$id" == "$previouslatestid" -o $answered -eq 1 ] ; then
    # echo "already answered query \"$msg\" from $name: skipped"
    answered=1
  else
    # here's the big "if word then respond" block...
    if [ "$msg" == "hours" ] ; then
      $tweet "@$name our hours are Mon-Fri 9-5, Sat 10-4. Thanks for asking!"
    elif [ "$msg" == "time" ] ; then
      $tweet @$name the time here is $(date)
    elif [ "$msg" == "directions" ] ; then 
      $tweet "@$name directions to my office are here: SOMEURL"
    elif [ "$msg" == "address" ] ; then 
      $tweet "@$name we're located at 123 University Avenue, Anywhere USA"
    elif [ "$msg" == "date" ] ; then 
      $tweet "@$name sorry, I'm already in a relationship and it's complicated."
    elif [ "$msg" == "about" -o "$msg" == "faq" ] ; then 
      $tweet "@$name I'm a tweetbot written by @DaveTaylor for @LinuxJournal"
    elif [ "$msg" == "happy" ] ; then 
      $tweet "@$name I think I'm pretty happy, but it's hard to tell some days"
    elif [ "$msg" == "uptime" ] ; then
      $tweet "@$name uptime: $(uptime)"
    elif [ "$msg" == "bio" ] ; then
      $tweet "@$name I was born a small BASIC program but have since evolved."
    elif [ "$msg" == "mood" ] ; then 
      $tweet "@$name light a candle, put on some Barry White and we can talk..."
    elif [ "$msg" == "who" ] ; then 
      $tweet "@$name are we quoting songs from The Who now? Um, okay...."
    else
      $tweet "@$name I understand: time, date, about, uptime, bio and ..."
    fi
  fi
done < $temp

# now let's store the most recent ID for the next invocation

echo $latestid > "$lastidcache"

exit 0