Twitter Status IDs and Direct Message IDs
I recently created a Magic Eight Ball twitter-bot as a demo. Written in Python using the python-twitter API wrapper, it runs every 2 minutes and polls twitter for new replies (status updates containing @osric8ball) and direct messages (DMs) to osric8ball. If there are any, it replies with a random 8-Ball response.
Every status update and DM has an associated numeric ID. Initially, I stored the highest ID in a log file and used that when I polled twitter (i.e. “retrieve all replies and DMs with ID > highest ID”). However, I discovered that status updates and DMs apparently are stored in separate tables on twitter’s backend, as they have a separate set of IDs. Since the highest status ID was an order of magnitude larger than the highest DM ID, my bot completely ignored all DMs. This was not entirely obvious at first, as the IDs looked very similar, other than an extra digit: 2950029179 and 273876291.
My fix for this was to store both the highest status update ID and the highest DM ID is separate log files.
Another interesting twist: you have to be a follower of a user in order to send that user a DM.
At first I thought I had solved this by telling the bot to follow anyone who sent it a message. However, if you submit an API request to follow a user whom you are already following, it produces an error. So I added a check to see if the bot was already following the user, and, if not, follow the user.
In case anyone is interested, the entire code is below:
#!/usr/bin/python2.4
import twitter
import random
replyfile = 'twitterreply.log'
dmfile = 'twitterdm.log'
last_id = 0
api = twitter.Api(username='osric8ball', password='[actualpassword]')
friends = api.GetFriends()
responses = ['Signs Point To Yes','Yes','You May Rely On It','Ask Again Later','Concentrate And Ask Again','Outlook Good','My Sources Say No','Better Not Tell You Now','Without A Doubt','Reply Hazy, Try Again','It Is Decidedly So','Cannot Predict Now','My Reply Is No','As I See It Yes','It Is Certain','Yes Definitely','Don\'t Count On It','Most Likely','Very Doubtful','Outlook Not So Good']
def areWeFriends(screen_name):
isFriend = 0
for friend in friends:
if friend.screen_name == screen_name:
isFriend = 1
return isFriend
def makeFriend(screen_name):
if areWeFriends(screen_name) == 0:
api.CreateFriendship(user=screen_name)
def sendReply(screen_name):
makeFriend(screen_name)
response = responses[random.randint(0,19)]
api.PostUpdates('@' + screen_name + ': ' + response)
print '@' + screen_name + ': ' + response
def sendDirectMessage(screen_name):
makeFriend(screen_name)
response = responses[random.randint(0,19)]
api.PostDirectMessage(screen_name,response)
print 'd ' + screen_name + ': ' + response
#Read logs
log = open(replyfile, 'r')
last_reply_id = log.read()
log.close()
log = open(dmfile, 'r')
last_dm_id = log.read()
log.close()
if last_reply_id > 0 and last_dm_id > 0:
#Get replies and direct messages
replies = api.GetReplies(since_id=last_reply_id)
directMessages = api.GetDirectMessages(since_id=last_dm_id)
#Update logs
if directMessages != []:
last_dm_id = directMessages[0].id
if replies != []:
last_reply_id = replies[0].id
if last_reply_id > 0:
log = open(replyfile,'w')
log.write( str(last_reply_id) )
log.close()
if last_dm_id > 0:
log = open(dmfile,'w')
log.write( str(last_dm_id) )
log.close()
# Loop through replies
for reply in replies:
sendReply(reply.user.screen_name)
# Loop through direct messages
for directMessage in directMessages:
sendDirectMessage(directMessage.sender_screen_name)
else:
print 'Unable to read ' + replyfile + ' or ' + dmfile
1 Comment to Twitter Status IDs and Direct Message IDs
Leave a comment
Pages
Archives
- April 2012
- March 2012
- January 2012
- December 2011
- November 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- March 2011
- February 2011
- January 2011
- November 2010
- May 2010
- March 2010
- January 2010
- December 2009
- October 2009
- September 2009
- August 2009
- July 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
Looks like I need to find a better way to embed Python code.