Guest User

Untitled

a guest
Jul 21st, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. # Tem Twitter Module.
  2. # MODULE VERSION: 0.1 (BETA)
  3. # It should be highly noted that I tried to use the twitter gem and its oauth settings to create this module but it has a problem with HMAC and Ruby 1.9
  4. # and until it is resolved I will use a basic HTTP REST Method.
  5.  
  6. # Functions:
  7. # .twitter status update <update text> - Update your twitter status.
  8. # .twitter get last - Prints out your current last status update.
  9. # .twitter follow <screen name> - Follow a twitter screen name.
  10.  
  11. # Change these options.
  12. $Twit_User = 'Jeedey'
  13. $Twit_Password = '*****'
  14.  
  15. require 'net/http'
  16. require 'base64'
  17. class Twit
  18. def initialize(subject, parameters)
  19. @subject = subject
  20. @http_handle = Net::HTTP.new("twitter.com", 80)
  21. @http_headers = {"Content-Type" => "text/xml", "Authorization" => "Basic "+Base64.encode64($Twit_User+":"+$Twit_Password)}
  22. if parameters =~ /^status update/
  23. # Seems they want to update status.
  24. parameters = parameters.gsub /^status update/, '' # Clip to leave just the status text
  25. status_update(parameters)
  26. elsif parameters =~ /^get last/
  27. parameters = parameters.gsub /^get last/, ''
  28. get_last_status()
  29. elsif parameters =~ /^follow/
  30. parameters = parameters.gsub /^follow/, ''
  31. parameters.slice!(0)
  32. follow_name(parameters)
  33. end
  34. end
  35.  
  36. def status_update(msg)
  37. if(msg.length > 139)
  38. privmsg(@subject, "Twitter Module: Your status update exceeds 140 characters and will be truncated by twitter.")
  39. end
  40. xml_req_path = "/statuses/update.xml"
  41. xml_req = "<status>#{msg}</status>"
  42. response_code, data = @http_handle.post(xml_req_path, xml_req, @http_headers)
  43. if response_code =~ /403/ or data =~ /\<error\>/
  44. privmsg(@subject, "Twitter Module: An error occured, it seems to be down to going over the request limit or authentication problems.")
  45. else
  46. privmsg(@subject, "Twitter Module: Status Successfully Updated.")
  47. end
  48. end
  49.  
  50. def get_last_status()
  51. xml_req_path = "/statuses/user_timeline.xml?screen_name="+$Twit_User+"&count=1"
  52. response_code, data = @http_handle.get(xml_req_path, @http_headers)
  53. if response_code =~ /403/ or data =~ /\<error\>/
  54. privmsg(@subject, "Twitter Module: An error occured, it seems to be down to going over the request limit or authentication problems.")
  55. else
  56. status = data.match(/\<text\>(.+)\<\/text\>/)
  57. created_at = data.match(/\<created_at\>(.+)\<\/created_at\>/)
  58. if status and created_at
  59. privmsg(@subject, "Twitter Module: Last user status:")
  60. privmsg(@subject, "Twitter Module: Created on: "+ created_at.captures[0])
  61. privmsg(@subject, "Status: "+ status.captures[0])
  62. else
  63. privmsg(@subject, "Twitter Module: Unknown error :(")
  64. end
  65. end
  66. end
  67.  
  68. def follow_name(screen_name)
  69. xml_req_path = "/friendships/create/id.xml?screen_name="+screen_name
  70. xml_req = "<screen_name>#{screen_name}</screen_name>"
  71. response_code, data = @http_handle.post(xml_req_path, xml_req, @http_headers)
  72. if response_code =~ /403/ or data =~ /\<error\>/
  73. privmsg(@subject, "Twitter Module: An error occured, it seems to be down to going over the request limit or authentication problems.")
  74. else
  75. status = data.match(/\<screen_name\>#{screen_name}\<\/screen_name\>/)
  76. if status
  77. privmsg(@subject, "Twitter Module: You are now following "+screen_name+" successfully!")
  78. else
  79. privmsg(@subject, "Twitter Module: Unknown error :(")
  80. end
  81. end
  82. end
  83. end
Add Comment
Please, Sign In to add comment