Advertisement
Guest User

GrammarBot

a guest
Jun 16th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*- #
  3.  
  4. from twitterbot import TwitterBot
  5. import keys
  6. from random import choice
  7.  
  8. class GrammarBot(TwitterBot):
  9. def bot_init(self):
  10. """ Initialize and configure alicebot """
  11.  
  12. ############################
  13. # REQUIRED: LOGIN DETAILS! #
  14. ############################
  15. self.config['api_key'] = keys.consumer_key
  16. self.config['api_secret'] = keys.consumer_secret
  17. self.config['access_key'] = keys.access_token
  18. self.config['access_secret'] = keys.access_token_secret
  19.  
  20.  
  21. ######################################
  22. # SEMI-OPTIONAL: OTHER CONFIG STUFF! #
  23. ######################################
  24.  
  25. # how often to tweet, in seconds
  26. self.config['tweet_interval'] = 1 * 60
  27.  
  28. # use this to define a (min, max) random range of how often to tweet
  29. # e.g., self.config['tweet_interval_range'] = (5*60, 10*60) # tweets every 5-10 minutes
  30. self.config['tweet_interval_range'] = None
  31.  
  32. # only reply to tweets that specifically mention the bot
  33. self.config['reply_direct_mention_only'] = True
  34.  
  35. # only include bot followers (and original tweeter) in @-replies
  36. self.config['reply_followers_only'] = True
  37.  
  38. # fav any tweets that mention this bot?
  39. self.config['autofav_mentions'] = False
  40.  
  41. # fav any tweets containing these keywords?
  42. self.config['autofav_keywords'] = []
  43.  
  44. # follow back all followers?
  45. self.config['autofollow'] = False
  46.  
  47. # create a new chatterbot and load the AIML file
  48. self.alice = aiml.Kernel()
  49. self.alice.learn("german.aiml")
  50.  
  51.  
  52. def on_scheduled_tweet(self):
  53. """ Make a public tweet to the bot's own timeline. """
  54. pass
  55.  
  56. def on_mention(self, tweet, prefix):
  57. """ Actions to take when a mention is received. """
  58.  
  59. subj = ['I', 'Hannah and Joe', 'The Jacksons', 'Cats', 'The Germans']
  60. verbs = ['eat', 'sleep', 'run', 'watch']
  61. prep = ['an', 'a', 'the', 'on', 'in', 'over']
  62. obj = ['apple', 'tree', 'car', 'television', 'Weisswurst']
  63.  
  64. a = random.choice(subj)
  65. b = random.choice(verbs)
  66. c = random.choice(prep)
  67. d = random.choice(obj)
  68.  
  69. tweetsize = 140 - len(prefix) - 1
  70.  
  71. try:
  72.  
  73. # get a response from the chatterbot
  74. response = a + b + c + d
  75.  
  76. # create a tweet and make sure to cut it off at 140 chars
  77. text = prefix + "Like this?" + response
  78. text = text[:140]
  79.  
  80. # post the tweet
  81. self.post_tweet(text, reply_to=tweet)
  82.  
  83. except Exception as e:
  84. print(e)
  85.  
  86.  
  87. def on_timeline(self, tweet, prefix):
  88. """ Actions to take on a timeline tweet. """
  89. pass
  90.  
  91.  
  92. if __name__ == '__main__':
  93. bot = GrammarBot()
  94. bot.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement