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 springen
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
+
  #!/usr/bin/env ruby
# Metafridge Jabber/Twitter Bot - Version 0.3
+
  # Metafridge Jabber/Twitter Bot - Version 0.4
# Consti AT Consti PUNKT de
+
  # Consti AT Consti PUNKT de
require 'xmpp4r'
+
  require 'xmpp4r'
require 'net/http'
+
  require 'net/http'
require 'cgi'
+
  require 'cgi'
require 'iconv'
+
  require 'iconv'
require 'uri'
+
  require 'uri'
require 'open-uri'
+
  require 'open-uri'
+
  def jabrespond client, msg_in, msg_out, subj
client = Jabber::Client.new(Jabber::JID.new('metafridge@jabber.metalab.at/metalab'))
+
    m = Jabber::Message.new(msg_in.from, msg_out).set_type(:normal).set_id('1').set_subject(subj)
client.connect
+
    client.send m
client.auth('PASSWORT')
+
  end
+
  client = Jabber::Client.new(Jabber::JID.new('metafridge@jabber.metalab.at/metalab'))
client.send(Jabber::Presence.new.set_show(:chat).set_status('Feed me.'))
+
  client.connect
+
  client.auth('PASSWORT')
client.add_message_callback { |msg|
+
  client.send(Jabber::Presence.new.set_show(:chat).set_status('Send: !help for help :)'))
  if msg.type == :chat
+
  client.add_message_callback { |msg|
    puts "\n------INCOMING-------\nFrom: #{msg.from}\nMessage:#{msg.body}\n--------------------\n"
+
    if msg.type == :chat
    begin
+
    puts "\n------INCOMING-------\nFrom: #{msg.from}\nMessage:#{msg.body}\n--------------------\n"
      incoming = URI.encode(msg.body)
+
    begin
      if (msg.body == '/last' || msg.body == '/last+time')
+
      incoming = URI.encode(msg.body)
        begin
+
      if (msg.body == '/last' || msg.body == '/last+time')
        Net::HTTP.start('twitter.com') {|http|
+
        jabrespond(client, msg, "Error: The '/command' is deprecated. Please use '!command'.", '/commands are deprecated, use !command')
          req = Net::HTTP::Get.new('/statuses/user_timeline.xml')
+
      elsif (msg.body == '!help')
          req.basic_auth 'metafridge', 'metafoo'
+
        response  = "!last => Last 10 metafridge messages\n"
          response = http.request(req)
+
        response += "!last+time => Last 10 metafridge messages + timestamp\n"
          resp = response.body.to_s
+
        response += "!sense => Metasense Status"
          lastmsgs = resp.scan(/<text>(.*)<\/text>$/)
+
        jabrespond(client, msg, response, 'Help?')
          lasttime = resp.scan(/<created_at>(.*)<\/created_at>$/)
+
      elsif (msg.body == '!sense')
          jabresponse = "The last 10 Metafridge messages:\n---------------------\n"
+
        begin
          for i in 0...10
+
            #http_header = nil
            j = i+1
+
            http_request = nil
            jabresponse += "#{j}: '#{lastmsgs[i]}'\n"
+
            Net::HTTP.start('metalab.at', 80) {|http|
            if (msg.body == '/last+time')
+
            #http_header = http.head('/metasense/status_lock.txt')
              jabresponse += "(#{lasttime[i]})\n"
+
            http_request = http.get('/metasense/status_lock.txt')   
              jabresponse += "---------------------\n"
+
            }
            end
+
            #puts http_header['content-type']
          end
+
            sense = http_request.body.to_s
          m = Jabber::Message.new(msg.from, jabresponse).set_type(:normal).set_id('1').set_subject('last 10 metafridge messages')
+
            if (sense == '1')
           client.send m
+
              jabrespond(client, msg, 'metalab is OPEN', 'Metasense') 
        }
+
            elsif (sense == '0')
        rescue
+
              jabrespond(client, msg, 'metalab is CLOSED', 'Metasense') 
          puts "timeout"
+
            else
        end
+
              jabrespond(client, msg, 'metasense if turned off', 'Metasense') 
      else
+
            end
        if (msg.from == 'twitter@twitter.com')
+
        rescue
          @spl = incoming.split(/:%20/)     
+
          jabrespond(client, msg, 'Error: sorry. metalab.at seems to be down. try again later.', 'Error')
          if (@spl.length == 2)
+
          puts "timeout"
            puts "\n------SENDING TO FRIDGE-------\nUsername: #{@spl[0]}\nMessage:#{@spl[1]}\n--------------------\n"
+
        end   
            open("http://fridge.metalab.at:1337/cgi-bin/post_text.cgi?lauftext=#{@spl[1]}&playfunction=1").read
+
      elsif (msg.body == '!last' || msg.body == '!last+time')
            puts "\n-----SENT-----\n"
+
        begin
          end   
+
        res =Net::HTTP.start('twitter.com') {|http|
        else
+
            req = Net::HTTP::Get.new('/statuses/user_timeline.xml')
          puts "\n------SENDING TO FRIDGE-------\nMessage:#{incoming}\n--------------------\n"   
+
            req.basic_auth 'metafridge', 'metafoo'
          open("http://fridge.metalab.at:1337/cgi-bin/post_text.cgi?lauftext=#{incoming}&playfunction=1").read
+
            http.request(req)  
          puts "\n-----SENT-----\n"
+
          }
          puts "\n------SENDING RESPONSE-------\nTo:#{msg.from}\nMessage: Thanks!\n--------------------\n"
+
          resp = res.body.to_s
          m = Jabber::Message.new(msg.from, 'Thanks!').set_type(:normal).set_id('1').set_subject('Thanks!')
+
          lastmsgs = resp.scan(/<text>(.*)<\/text>$/)
          client.send m
+
          lasttime = resp.scan(/<created_at>(.*)<\/created_at>$/)
        end
+
          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
    rescue
 
      puts "Keine Korrekte Message  "
 
 
     end
 
     end
  end
+
  }
}
+
  Thread.stop
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