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.2: Bot gibt jetzt ein 'Thanks!' zurueck + ich hab mein privates Jabber Icon raus :)
+
: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
 
  #!/usr/bin/env ruby
  # Metafridge Jabber/Twitter Bot - Version 0.2
+
  # Metafridge Jabber/Twitter Bot - Version 0.3
# SAT July 26 2008
 
 
  # Consti AT Consti PUNKT de
 
  # Consti AT Consti PUNKT de
 
  require 'xmpp4r'
 
  require 'xmpp4r'
Zeile 16: Zeile 16:
 
  require 'uri'
 
  require 'uri'
 
  require 'open-uri'
 
  require 'open-uri'
 
+
 
  client = Jabber::Client.new(Jabber::JID.new('metafridge@jabber.metalab.at/metalab'))
 
  client = Jabber::Client.new(Jabber::JID.new('metafridge@jabber.metalab.at/metalab'))
 
  client.connect
 
  client.connect
  client.auth('PASSWORT')  
+
  client.auth('PASSWORT')
 
   
 
   
 
  client.send(Jabber::Presence.new.set_show(:chat).set_status('Feed me.'))
 
  client.send(Jabber::Presence.new.set_show(:chat).set_status('Feed me.'))
Zeile 28: Zeile 28:
 
     begin
 
     begin
 
       incoming = URI.encode(msg.body)
 
       incoming = URI.encode(msg.body)
       @spl = incoming.split(/:%20/)
+
       if (msg.body == '/last' || msg.body == '/last+time')
      if (@spl.length == 2)
+
        begin
        puts "\n------SENDING TO FRIDGE-------\nUsername: #{@spl[0]}\nMessage:#{@spl[1]}\n--------------------\n"
+
        Net::HTTP.start('twitter.com') {|http|
        open("http://fridge.metalab.at:1337/cgi-bin/post_text.cgi?lauftext=#{@spl[1]}&playfunction=1").read
+
          req = Net::HTTP::Get.new('/statuses/user_timeline.xml')
         puts "\n-----SENT-----\n"
+
          req.basic_auth 'metafridge', 'metafoo'
 +
          response = http.request(req)
 +
          resp = response.body.to_s
 +
          lastmsgs = resp.scan(/<text>(.*)<\/text>$/)
 +
          lasttime = resp.scan(/<created_at>(.*)<\/created_at>$/)
 +
          jabresponse = "The last 10 Metafridge messages:\n---------------------\n"
 +
          for i in 0...10
 +
            j = i+1
 +
            jabresponse += "#{j}: '#{lastmsgs[i]}'\n"
 +
            if (msg.body == '/last+time')
 +
              jabresponse += "(#{lasttime[i]})\n"
 +
              jabresponse += "---------------------\n"
 +
            end
 +
          end
 +
          m = Jabber::Message.new(msg.from, jabresponse).set_type(:normal).set_id('1').set_subject('last 10 metafridge messages')
 +
          client.send m
 +
        }
 +
         rescue
 +
          puts "timeout"
 +
        end
 
       else
 
       else
         puts "\n------SENDING TO FRIDGE-------\nMessage:#{incoming}\n--------------------\n"   
+
         if (msg.from == 'twitter@twitter.com')
        open("http://fridge.metalab.at:1337/cgi-bin/post_text.cgi?lauftext=#{incoming}&playfunction=1").read
+
          @spl = incoming.split(/:%20/)   
        puts "\n-----SENT-----\n"
+
          if (@spl.length == 2)
        puts "\n------SENDING RESPONSE-------\nTo:#{msg.from}\nMessage: Thanks!\n--------------------\n"
+
            puts "\n------SENDING TO FRIDGE-------\nUsername: #{@spl[0]}\nMessage:#{@spl[1]}\n--------------------\n"
        m = Jabber::Message.new(msg.from, 'Thanks!').set_type(:normal).set_id('1').set_subject('Thanks!')
+
            open("http://fridge.metalab.at:1337/cgi-bin/post_text.cgi?lauftext=#{@spl[1]}&playfunction=1").read
        client.send m
+
            puts "\n-----SENT-----\n"
      end
+
          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"
 +
          m = Jabber::Message.new(msg.from, 'Thanks!').set_type(:normal).set_id('1').set_subject('Thanks!')
 +
          client.send m
 +
        end
 +
    end
 
     rescue
 
     rescue
 
       puts "Keine Korrekte Message  "
 
       puts "Keine Korrekte Message  "
Zeile 46: Zeile 74:
 
   end
 
   end
 
  }
 
  }
 
 
  Thread.stop
 
  Thread.stop

Version vom 26. Juli 2008, 22:14 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.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.3
# Consti AT Consti PUNKT de
require 'xmpp4r'
require 'net/http'
require 'cgi'
require 'iconv'
require 'uri'
require 'open-uri'

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('Feed me.'))

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')
       begin
       Net::HTTP.start('twitter.com') {|http|
          req = Net::HTTP::Get.new('/statuses/user_timeline.xml')
          req.basic_auth 'metafridge', 'metafoo'
          response = http.request(req)
          resp = response.body.to_s
          lastmsgs = resp.scan(/<text>(.*)<\/text>$/)
          lasttime = resp.scan(/<created_at>(.*)<\/created_at>$/)
          jabresponse = "The last 10 Metafridge messages:\n---------------------\n"
          for i in 0...10
            j = i+1
            jabresponse += "#{j}: '#{lastmsgs[i]}'\n"
            if (msg.body == '/last+time')
             jabresponse += "(#{lasttime[i]})\n"
             jabresponse += "---------------------\n"
            end
          end
          m = Jabber::Message.new(msg.from, jabresponse).set_type(:normal).set_id('1').set_subject('last 10 metafridge messages')
          client.send m
        }
       rescue
         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"
         m = Jabber::Message.new(msg.from, 'Thanks!').set_type(:normal).set_id('1').set_subject('Thanks!')
         client.send m
       end
    end
   rescue
     puts "Keine Korrekte Message  "
   end
  end
}
Thread.stop