Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tem Twitter Module.
- # MODULE VERSION: 0.1 (BETA)
- # 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
- # and until it is resolved I will use a basic HTTP REST Method.
- # Functions:
- # .twitter status update <update text> - Update your twitter status.
- # .twitter get last - Prints out your current last status update.
- # .twitter follow <screen name> - Follow a twitter screen name.
- # Change these options.
- $Twit_User = 'Jeedey'
- $Twit_Password = '*****'
- require 'net/http'
- require 'base64'
- class Twit
- def initialize(subject, parameters)
- @subject = subject
- @http_handle = Net::HTTP.new("twitter.com", 80)
- @http_headers = {"Content-Type" => "text/xml", "Authorization" => "Basic "+Base64.encode64($Twit_User+":"+$Twit_Password)}
- if parameters =~ /^status update/
- # Seems they want to update status.
- parameters = parameters.gsub /^status update/, '' # Clip to leave just the status text
- status_update(parameters)
- elsif parameters =~ /^get last/
- parameters = parameters.gsub /^get last/, ''
- get_last_status()
- elsif parameters =~ /^follow/
- parameters = parameters.gsub /^follow/, ''
- parameters.slice!(0)
- follow_name(parameters)
- end
- end
- def status_update(msg)
- if(msg.length > 139)
- privmsg(@subject, "Twitter Module: Your status update exceeds 140 characters and will be truncated by twitter.")
- end
- xml_req_path = "/statuses/update.xml"
- xml_req = "<status>#{msg}</status>"
- response_code, data = @http_handle.post(xml_req_path, xml_req, @http_headers)
- if response_code =~ /403/ or data =~ /\<error\>/
- privmsg(@subject, "Twitter Module: An error occured, it seems to be down to going over the request limit or authentication problems.")
- else
- privmsg(@subject, "Twitter Module: Status Successfully Updated.")
- end
- end
- def get_last_status()
- xml_req_path = "/statuses/user_timeline.xml?screen_name="+$Twit_User+"&count=1"
- response_code, data = @http_handle.get(xml_req_path, @http_headers)
- if response_code =~ /403/ or data =~ /\<error\>/
- privmsg(@subject, "Twitter Module: An error occured, it seems to be down to going over the request limit or authentication problems.")
- else
- status = data.match(/\<text\>(.+)\<\/text\>/)
- created_at = data.match(/\<created_at\>(.+)\<\/created_at\>/)
- if status and created_at
- privmsg(@subject, "Twitter Module: Last user status:")
- privmsg(@subject, "Twitter Module: Created on: "+ created_at.captures[0])
- privmsg(@subject, "Status: "+ status.captures[0])
- else
- privmsg(@subject, "Twitter Module: Unknown error :(")
- end
- end
- end
- def follow_name(screen_name)
- xml_req_path = "/friendships/create/id.xml?screen_name="+screen_name
- xml_req = "<screen_name>#{screen_name}</screen_name>"
- response_code, data = @http_handle.post(xml_req_path, xml_req, @http_headers)
- if response_code =~ /403/ or data =~ /\<error\>/
- privmsg(@subject, "Twitter Module: An error occured, it seems to be down to going over the request limit or authentication problems.")
- else
- status = data.match(/\<screen_name\>#{screen_name}\<\/screen_name\>/)
- if status
- privmsg(@subject, "Twitter Module: You are now following "+screen_name+" successfully!")
- else
- privmsg(@subject, "Twitter Module: Unknown error :(")
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment