twitterposter.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from event import Event
  2. try:
  3. import twitter
  4. except ImportError:
  5. twitter = object
  6. try:
  7. from basemodule import BaseModule
  8. except ImportError:
  9. from modules.basemodule import BaseModule
  10. """
  11. to use this, you'll need to create a file in the modules directory called 'twitter_credentials.py', and it should look like this:
  12. class PybotTwitter:
  13. api_key = "<your api key (sometimes called consumer key)>"
  14. api_secret = "<api/consumer secret>"
  15. access_token = "<your access token>"
  16. access_token_secret = "<access token secret>"
  17. """
  18. class TwitterPoster(BaseModule):
  19. try:
  20. from modules.twitter_credentials import PybotTwitter as pt
  21. except ImportError:
  22. class PhonyPt:
  23. api_key = ""
  24. api_secret = ""
  25. access_token = ""
  26. access_token_secret = ""
  27. pt = PhonyPt()
  28. user_to_track = "bhhorg"
  29. def post_init(self):
  30. twitter = Event("__.twitter__")
  31. twitter.define(user_definition=self.user_to_track)
  32. twitter.subscribe(self)
  33. self.bot.register_event(twitter, self)
  34. twitter_command = Event("__.twitter_command__")
  35. twitter_command.define(msg_definition="^\.twitter url")
  36. twitter_command.subscribe(self)
  37. self.bot.register_event(twitter_command, self)
  38. def handle(self, event):
  39. if event._type == "__.twitter__":
  40. pt = self.pt
  41. api = twitter.Api(consumer_key=pt.api_key,
  42. consumer_secret=pt.api_secret,
  43. access_token_key=pt.access_token,
  44. access_token_secret=pt.access_token_secret)
  45. try:
  46. status = api.PostUpdate(event.user + ": " + event.msg[:(140 - (len(event.user) + 2))]) # "username: <message>" all must be <= 140 char
  47. except Exception as e:
  48. print e
  49. print len(event.msg[:(140 - len(event.user) + 2)])
  50. elif event._type == "__.twitter_command__":
  51. self.say(event.channel, "https://twitter.com/pybot_posts_irc")