Metafridge Jabber Support: Unterschied zwischen den Versionen
aus Metalab Wiki, dem offenen Zentrum für meta-disziplinäre Magier und technisch-kreative Enthusiasten.
Zur Navigation springenZur Suche springenConsti (Diskussion | Beiträge) |
Consti (Diskussion | Beiträge) |
||
Zeile 4: | Zeile 4: | ||
Benoetigt 'xmpp4r' gem; | Benoetigt 'xmpp4r' gem; | ||
+ | :Version 0.4: /command wurde zu !command; Added !help und !sense | ||
:Version 0.3: Das Command '/last' gibt jetzt die letzten 10 Metafridge Nachrichten aus - '/last+time' ist mit Timestamp; | :Version 0.3: Das Command '/last' gibt jetzt die letzten 10 Metafridge Nachrichten aus - '/last+time' ist mit Timestamp; | ||
:Version 0.2: Bot gibt jetzt ein 'Thanks!' zurueck + ich hab mein privates Jabber Icon raus :) | :Version 0.2: Bot gibt jetzt ein 'Thanks!' zurueck + ich hab mein privates Jabber Icon raus :) | ||
− | + | #!/usr/bin/env ruby | |
− | + | # Metafridge Jabber/Twitter Bot - Version 0.4 | |
− | + | # Consti AT Consti PUNKT de | |
− | + | require 'xmpp4r' | |
− | + | require 'net/http' | |
− | + | require 'cgi' | |
− | + | require 'iconv' | |
− | + | require 'uri' | |
− | + | require 'open-uri' | |
− | + | def jabrespond client, msg_in, msg_out, subj | |
− | + | m = Jabber::Message.new(msg_in.from, msg_out).set_type(:normal).set_id('1').set_subject(subj) | |
− | + | client.send m | |
− | + | end | |
− | + | client = Jabber::Client.new(Jabber::JID.new('metafridge@jabber.metalab.at/metalab')) | |
− | + | client.connect | |
− | + | client.auth('PASSWORT') | |
− | + | client.send(Jabber::Presence.new.set_show(:chat).set_status('Send: !help for help :)')) | |
− | + | client.add_message_callback { |msg| | |
− | + | if msg.type == :chat | |
− | + | puts "\n------INCOMING-------\nFrom: #{msg.from}\nMessage:#{msg.body}\n--------------------\n" | |
− | + | begin | |
− | + | incoming = URI.encode(msg.body) | |
− | + | if (msg.body == '/last' || msg.body == '/last+time') | |
− | + | jabrespond(client, msg, "Error: The '/command' is deprecated. Please use '!command'.", '/commands are deprecated, use !command') | |
− | + | elsif (msg.body == '!help') | |
− | + | response = "!last => Last 10 metafridge messages\n" | |
− | + | response += "!last+time => Last 10 metafridge messages + timestamp\n" | |
− | + | response += "!sense => Metasense Status" | |
− | + | jabrespond(client, msg, response, 'Help?') | |
− | + | elsif (msg.body == '!sense') | |
− | + | begin | |
− | + | #http_header = nil | |
− | + | http_request = nil | |
− | + | Net::HTTP.start('metalab.at', 80) {|http| | |
− | + | #http_header = http.head('/metasense/status_lock.txt') | |
− | + | http_request = http.get('/metasense/status_lock.txt') | |
− | + | } | |
− | + | #puts http_header['content-type'] | |
− | + | sense = http_request.body.to_s | |
− | + | if (sense == '1') | |
− | client. | + | jabrespond(client, msg, 'metalab is OPEN', 'Metasense') |
− | + | elsif (sense == '0') | |
− | + | jabrespond(client, msg, 'metalab is CLOSED', 'Metasense') | |
− | + | else | |
− | + | jabrespond(client, msg, 'metasense if turned off', 'Metasense') | |
− | + | end | |
− | + | rescue | |
− | + | jabrespond(client, msg, 'Error: sorry. metalab.at seems to be down. try again later.', 'Error') | |
− | + | puts "timeout" | |
− | + | end | |
− | + | elsif (msg.body == '!last' || msg.body == '!last+time') | |
− | + | begin | |
− | + | res =Net::HTTP.start('twitter.com') {|http| | |
− | + | req = Net::HTTP::Get.new('/statuses/user_timeline.xml') | |
− | + | req.basic_auth 'metafridge', 'metafoo' | |
− | + | http.request(req) | |
− | + | } | |
− | + | resp = res.body.to_s | |
− | + | lastmsgs = resp.scan(/<text>(.*)<\/text>$/) | |
− | + | lasttime = resp.scan(/<created_at>(.*)<\/created_at>$/) | |
− | + | response = "The last 10 Metafridge messages:\n---------------------\n" | |
+ | for i in 0...10 | ||
+ | j = i+1 | ||
+ | response += "#{j}: '#{lastmsgs[i]}'\n" | ||
+ | if (msg.body == '!last+time') | ||
+ | response += "(#{lasttime[i]})\n" | ||
+ | response += "---------------------\n" | ||
+ | end | ||
+ | end | ||
+ | jabrespond(client, msg, response, 'last 10 metafridge messages') | ||
+ | rescue | ||
+ | jabrespond(client, msg, 'Error: sorry. twitter seems to be down. try again later.', 'Error') | ||
+ | puts "timeout" | ||
+ | end | ||
+ | else | ||
+ | if (msg.from == 'twitter@twitter.com') | ||
+ | @spl = incoming.split(/:%20/) | ||
+ | if (@spl.length == 2) | ||
+ | puts "\n------SENDING TO FRIDGE-------\nUsername: #{@spl[0]}\nMessage:#{@spl[1]}\n--------------------\n" | ||
+ | open("http://fridge.metalab.at:1337/cgi-bin/post_text.cgi?lauftext=#{@spl[1]}&playfunction=1").read | ||
+ | puts "\n-----SENT-----\n" | ||
+ | end | ||
+ | else | ||
+ | puts "\n------SENDING TO FRIDGE-------\nMessage:#{incoming}\n--------------------\n" | ||
+ | open("http://fridge.metalab.at:1337/cgi-bin/post_text.cgi?lauftext=#{incoming}&playfunction=1").read | ||
+ | puts "\n-----SENT-----\n" | ||
+ | puts "\n------SENDING RESPONSE-------\nTo:#{msg.from}\nMessage: Thanks!\n--------------------\n" | ||
+ | jabrespond(client, msg, 'Thanks!', 'Thanks!') | ||
+ | end | ||
+ | end | ||
+ | rescue | ||
+ | puts "Keine Korrekte Message " | ||
end | end | ||
− | |||
− | |||
end | end | ||
− | + | } | |
− | + | Thread.stop | |
− |
Version vom 28. Juli 2008, 21:52 Uhr
Ein Bot in Rails, welcher per Jabber Account neue Messages entgegen nimmt und an den Fridge sendet; Sollte eigentlich primär für den Twitterbot sein, damit man auch 'direct' messages an den Twitter Bot schicken kann, die dann angezeigt werden. Aber nachdem Twitter im Moment kein Jabber anbietet (Performance Probleme) ists nun mehr 'nur' ein Jabber Bot :) Benoetigt 'xmpp4r' gem;
- Version 0.4: /command wurde zu !command; Added !help und !sense
- Version 0.3: Das Command '/last' gibt jetzt die letzten 10 Metafridge Nachrichten aus - '/last+time' ist mit Timestamp;
- Version 0.2: Bot gibt jetzt ein 'Thanks!' zurueck + ich hab mein privates Jabber Icon raus :)
#!/usr/bin/env ruby # Metafridge Jabber/Twitter Bot - Version 0.4 # Consti AT Consti PUNKT de require 'xmpp4r' require 'net/http' require 'cgi' require 'iconv' require 'uri' require 'open-uri' def jabrespond client, msg_in, msg_out, subj m = Jabber::Message.new(msg_in.from, msg_out).set_type(:normal).set_id('1').set_subject(subj) client.send m end client = Jabber::Client.new(Jabber::JID.new('metafridge@jabber.metalab.at/metalab')) client.connect client.auth('PASSWORT') client.send(Jabber::Presence.new.set_show(:chat).set_status('Send: !help for help :)')) client.add_message_callback { |msg| if msg.type == :chat puts "\n------INCOMING-------\nFrom: #{msg.from}\nMessage:#{msg.body}\n--------------------\n" begin incoming = URI.encode(msg.body) if (msg.body == '/last' || msg.body == '/last+time') jabrespond(client, msg, "Error: The '/command' is deprecated. Please use '!command'.", '/commands are deprecated, use !command') elsif (msg.body == '!help') response = "!last => Last 10 metafridge messages\n" response += "!last+time => Last 10 metafridge messages + timestamp\n" response += "!sense => Metasense Status" jabrespond(client, msg, response, 'Help?') elsif (msg.body == '!sense') begin #http_header = nil http_request = nil Net::HTTP.start('metalab.at', 80) {|http| #http_header = http.head('/metasense/status_lock.txt') http_request = http.get('/metasense/status_lock.txt') } #puts http_header['content-type'] sense = http_request.body.to_s if (sense == '1') jabrespond(client, msg, 'metalab is OPEN', 'Metasense') elsif (sense == '0') jabrespond(client, msg, 'metalab is CLOSED', 'Metasense') else jabrespond(client, msg, 'metasense if turned off', 'Metasense') end rescue jabrespond(client, msg, 'Error: sorry. metalab.at seems to be down. try again later.', 'Error') puts "timeout" end elsif (msg.body == '!last' || msg.body == '!last+time') begin res =Net::HTTP.start('twitter.com') {|http| req = Net::HTTP::Get.new('/statuses/user_timeline.xml') req.basic_auth 'metafridge', 'metafoo' http.request(req) } resp = res.body.to_s lastmsgs = resp.scan(/<text>(.*)<\/text>$/) lasttime = resp.scan(/<created_at>(.*)<\/created_at>$/) response = "The last 10 Metafridge messages:\n---------------------\n" for i in 0...10 j = i+1 response += "#{j}: '#{lastmsgs[i]}'\n" if (msg.body == '!last+time') response += "(#{lasttime[i]})\n" response += "---------------------\n" end end jabrespond(client, msg, response, 'last 10 metafridge messages') rescue jabrespond(client, msg, 'Error: sorry. twitter seems to be down. try again later.', 'Error') puts "timeout" end else if (msg.from == 'twitter@twitter.com') @spl = incoming.split(/:%20/) if (@spl.length == 2) puts "\n------SENDING TO FRIDGE-------\nUsername: #{@spl[0]}\nMessage:#{@spl[1]}\n--------------------\n" open("http://fridge.metalab.at:1337/cgi-bin/post_text.cgi?lauftext=#{@spl[1]}&playfunction=1").read puts "\n-----SENT-----\n" end else puts "\n------SENDING TO FRIDGE-------\nMessage:#{incoming}\n--------------------\n" open("http://fridge.metalab.at:1337/cgi-bin/post_text.cgi?lauftext=#{incoming}&playfunction=1").read puts "\n-----SENT-----\n" puts "\n------SENDING RESPONSE-------\nTo:#{msg.from}\nMessage: Thanks!\n--------------------\n" jabrespond(client, msg, 'Thanks!', 'Thanks!') end end rescue puts "Keine Korrekte Message " end end } Thread.stop