I was a bit hesitant to post this script since it is such a powerful marketing tool that it could be used very badly in the hands of a spammer. The basic premise is to directly respond to someone’s tweet if they mention your product or service. So for example I might want to have a tweet that goes out directly to someone who mentions twitter and python in a tweet and let them know about this blog. This will accomplish the same thing as the TwitterHawk service except you won’t have to pay per tweet.
To do this I had a choice. I could use a service like TweetBeep.com and then write a script that responded to the emails in my inbox, or I could use the Twitter Search API directly. The search API is so dead simple that I wanted to try that route.
The other thing to consider is that I don’t want to send a tweet to the same person more than once so I need to keep a list of twitter users that I have responded to. I used pickle to persist that list of usernames to disk so that it sticks around between uses.
The query functionality provided by the Twitter Search API is pretty cool and provides much more power than I have used in this script. For example it is possible to geo-target, lookup hashtags, or reply tweets. You can check out the full spec at http://apiwiki.twitter.com/Twitter-API-Documentation
Lastly, to keep it a bit simpler I’m ignoring the pagination in the search results and this script will only respond to the first page worth of results. Adding a loop per page would be pretty straight forward but I didn’t want to clutter up the code.
>>> import tweetBack >>> tweetBack.tweet_back('python twitter', 'Here is a blog with some good Python scripts you might find interesting http://halotis.com', 'twitter_username', 'twitter_password') @nooble sent message @ichiro_j sent message @Ghabrie1 sent message .....
Here’s the Python Code:
#!/usr/bin/env python # -*- coding: utf-8 -*- # (C) 2009 HalOtis Marketing # written by Matt Warren # http://halotis.com/ try: import json as simplejson except: import simplejson # http://undefined.org/python/#simplejson import twitter #http://code.google.com/p/python-twitter/ import urllib import pickle TWITTER_USER = 'username' TWITTER_PASSWORD = 'password' USER_LIST_FILE = 'tweetback.pck' #read stored list of twitter users that have been responded to already in a file try: f = open(USER_LIST_FILE, 'r') user_list = pickle.load(f) except: user_list =  def search_results(query): url = 'http://search.twitter.com/search.json?q=' + '+'.join(query.split()) return simplejson.load(urllib.urlopen(url)) def tweet_back(query, tweet_reply, username=TWITTER_USER, password=TWITTER_PASSWORD): results = search_results(query) api = twitter.Api(username, password) try: for result in results['results']: if result['from_user'] not in user_list: api.PostUpdate('@' + result['from_user'] + ' ' + tweet_reply) print '@' + result['from_user'] + ' sent message' user_list.append(result['from_user']) except: print 'Failed to post update. may have gone over the twitter API limit.. please wait and try again' #write the user_list to disk f = open(USER_LIST_FILE, 'w') pickle.dump(user_list, f) if __name__=='__main__': tweet_back('python twitter', 'Here is a blog with some good Python scripts you might find interesting http://halotis.com')
Update: thanks tante for the simplejson note.