@hook.command('twitter', 'tw', 'twatter')
def twitter(text):
 if (tw_api is None):
  return 'This command requires a twitter API key.'
 if re.match('^\\d+$', text):
  try:
   tweet = tw_api.get_status(text)
  except tweepy.error.TweepError as e:
   if ('404' in e.reason):
    return 'Could not find tweet.'
   else:
    return 'Error: {}'.format(e.reason)
  user = tweet.user
 elif (re.match('^\\w{1,15}$', text) or re.match('^\\w{1,15}\\s+\\d+$', text)):
  if (text.find(' ') == (-1)):
   username = text
   tweet_number = 0
  else:
   (username, tweet_number) = text.split()
   tweet_number = (int(tweet_number) - 1)
  if (tweet_number > 200):
   return 'This command can only find the last \x02200\x02 tweets.'
  try:
   user = tw_api.get_user(username)
  except tweepy.error.TweepError as e:
   if ('404' in e.reason):
    return 'Could not find user.'
   else:
    return 'Error: {}'.format(e.reason)
  user_timeline = tw_api.user_timeline(id=user.id, count=(tweet_number + 1))
  if (not user_timeline):
   return 'The user \x02{}\x02 has no tweets.'.format(user.screen_name)
  try:
   tweet = user_timeline[tweet_number]
  except IndexError:
   tweet_count = len(user_timeline)
   return 'The user \x02{}\x02 only has \x02{}\x02 tweets.'.format(user.screen_name, tweet_count)
 elif re.match('^#\\w+$', text):
  search = tw_api.search(text)
  if (not search):
   return 'No tweets found.'
  tweet = random.choice(search)
  user = tweet.user
 else:
  return 'Invalid Input'
 text = ' '.join(tweet.text.split())
 if user.verified:
  prefix = '\\u2713'
 else:
  prefix = ''
 time = timeformat.time_since(tweet.created_at, datetime.utcnow())
 return '{}@\x02{}\x02 ({}): {} ({} ago)'.format(prefix, user.screen_name, user.name, text, time)

@ hook . command ( 'twitter' , 'tw' , 'twatter' ) def twitter ( text ) : if ( tw_api is None ) : return 'This command requires a twitter API key.' if re . match ( '^\\d+$' , text ) : try : tweet = tw_api . get_status ( text ) except tweepy . error . TweepError as e : if ( '404' in e . reason ) : return 'Could not find tweet.' else : return 'Error: {}' . format ( e . reason ) user = tweet . user elif ( re . match ( '^\\w{1,15}$' , text ) or re . match ( '^\\w{1,15}\\s+\\d+$' , text ) ) : if ( text . find ( ' ' ) == ( - 1 ) ) : username = text tweet_number = 0 else : ( username , tweet_number ) = text . split ( ) tweet_number = ( int ( tweet_number ) - 1 ) if ( tweet_number > 200 ) : return 'This command can only find the last \x02200\x02 tweets.' try : user = tw_api . get_user ( username ) except tweepy . error . TweepError as e : if ( '404' in e . reason ) : return 'Could not find user.' else : return 'Error: {}' . format ( e . reason ) user_timeline = tw_api . user_timeline ( id = user . id , count = ( tweet_number + 1 ) ) if ( not user_timeline ) : return 'The user \x02{}\x02 has no tweets.' . format ( user . screen_name ) try : tweet = user_timeline [ tweet_number ] except IndexError : tweet_count = len ( user_timeline ) return 'The user \x02{}\x02 only has \x02{}\x02 tweets.' . format ( user . screen_name , tweet_count ) elif re . match ( '^#\\w+$' , text ) : search = tw_api . search ( text ) if ( not search ) : return 'No tweets found.' tweet = random . choice ( search ) user = tweet . user else : return 'Invalid Input' text = ' ' . join ( tweet . text . split ( ) ) if user . verified : prefix = '\\u2713' else : prefix = '' time = timeformat . time_since ( tweet . created_at , datetime . utcnow ( ) ) return '{}@\x02{}\x02 ({}): {} ({} ago)' . format ( prefix , user . screen_name , user . name , text , time )
twitter restful controller @todo: action button to update async .