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
Keine Bearbeitungszusammenfassung
KKeine Bearbeitungszusammenfassung
 
(8 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
== Ist: ==
Ein Bot in Rails, welcher per Jabber Account neue Messages entgegen nimmt und an den Fridge sendet;
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.
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 :)
Aber nachdem Twitter im Moment kein Jabber anbietet (Performance Probleme) ists nun mehr 'nur' ein Jabber Bot :)
Benoetigt 'xmpp4r' gem;
Benoetigt: ruby on rails, 'xmpp4r' gem (alles andere sollte schon da sein :));


#!/usr/bin/env ruby
== Kann: ==
   
 
require 'xmpp4r'
!help
require 'net/http'
!last
require 'cgi'
!last+time
require 'iconv'
!events
require 'uri'
!sense
require 'open-uri'  
!about
!door
client = Jabber::Client.new(Jabber::JID.new('metafridge@jabber.metalab.at/metalab'))
!door+time
client.connect
 
client.auth('PASSWORT')  
 
== Will: ==
client.send(Jabber::Presence.new.set_show(:chat).set_status('Feed me.'))  
 
Ideen und Vorschläge was man(n) noch so reinbauen kann :)
client.add_message_callback { |msg|
 
  if msg.type == :chat
 
  puts "\n------INCOMING-------\nFrom: #{msg.from}\nMessage:#{msg.body}\n--------------------\n"
== Dankt: ==
  begin
 
    incoming = URI.encode(msg.body)
Overflo, Wizard23, Marius für den Metafridge + Fin für die Twitteranbindung
     @spl = incoming.split(/:%20/)
 
    if (@spl.length == 2)
Sushimako fürs Script-Hosting und seine Geduld, alle paar Stunden ne neue Version drauf zu tun :)
      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
== Diff: ==
      puts "\n-----SENT-----\n"
 
    else   
:Version 0.6: Added !door (Last 10 Metadoor Videos) and !door+time
      open("http://fridge.metalab.at:1337/cgi-bin/post_text.cgi?lauftext=#{incoming}&playfunction=1").read
:Version 0.5: Added !about (About the Bot) and !events (For the Metalab Event list)
      puts "\n-----SENT-----\n"
: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 :)
 
 
== Source: ==
 
  #!/usr/bin/env ruby
  version = "Metafridge Jabber Bot - Version 0.6"
  # 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') # Backwards 'compatibility'
        jabrespond(client, msg, "Error: The '/command' is deprecated. Please use '!command'.", '/commands are deprecated, use !command')
      elsif (msg.body[0..0] == '!')
        case msg.body
        # !help
        when '!help'
          response  = "!last => Last 10 metafridge messages\n"
          response += "!last+time => Last 10 metafridge messages + timestamp\n"
          response += "!door => Last 10 metadoor videos\n" 
          response += "!door+time => Last 10 metadoor videos + timestamp\n" 
          response += "!sense => Metasense Status\n"
          response += "!events => List the next Metalab Events\n"
          response += "!about => About the Metafridge Jabber Bot\n"
          jabrespond(client, msg, response, 'Help?')
        # !about Show some Info about the bot + fridge
        when '!about'
          response  = "This is " + version +"\n"
          response += "By: Constantin -> Consti AT Consti PUNKT de\n"
          response += "For: Metafridge (TM)\n\n"
          response += "Project: http://metalab.at/wiki/Fridge\n"
          response += "Webinterface: http://fridge.metalab.at:1337\n"
          response += "On Twitter: http://twitter.com/metafridge\n"
          jabrespond(client, msg, response, 'About?')
        # !events (Metalab Events)
        when '!events', '!event'
          begin
            http_request = nil
            Net::HTTP.start('metalab.at', 80) {|http|
              http_request = http.get('/feeds/events/')   
            }
            events = http_request.body.to_s
            events = events.gsub(/<\/title>/,"\n")
            titles = events.scan(/<title>(.*)$/)
            response = "The next #{titles.length-1} Events in the Metalab:\n---------------------\n"
              for i in 1...titles.length
                response += "'#{titles[i]}'\n"
              end
            jabrespond(client, msg, response, 'metalab events')
          rescue
            jabrespond(client, msg, "Error: Either metalab.at is down, or the feed is broken. try again later. Don't kill me brother!", 'Error')
            puts "timeout - feeds/events"
          end
        # !sense (Metasense)
        when '!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 metasense/status_lock.txt"
          end
        # !door and !door+time, last 10 videos on the door      
        when '!door', '!door+time'
          begin
            http_request = nil
            Net::HTTP.start('metadoor.soup.io', 80) {|http|
              http_request = http.get('/rss/')   
            }
            rss = http_request.body.to_s
            rss_youtube = rss.gsub(/&gt;&lt;/,"\n")
            youtube = rss_youtube.scan(/value="(http:\/\/www.youtube.com\/v\/.*)"$/) 
            rss_title = rss.gsub(/<\/title>/,"\n")
            title = rss_title.scan(/<title>(.*)$/)
            rss_pubdate = rss.gsub(/<\/pubDate>/,"\n")
            pubdate = rss_pubdate.scan(/<pubDate>(.*)$/)
            response = "The last 10 Metadoor Videos:\n---------------------\n"
            for i in 0...10
              j = i+1
              response += "#{j}: '#{title[j]}'\n'#{youtube[i]}'\n"
              if (msg.body == '!door+time')
                response += "(#{pubdate[i]})\n"
                response += "---------------------\n"
              end
            end
            jabrespond(client, msg, response, 'last 10 metadoor videos')
          rescue
            jabrespond(client, msg, 'Error: sorry. metadoor.soup.io seems to be down. try again later.', 'Error')
            puts "timeout"
          end
        # !last and !last+time, last 10 messages on the fridge     
        when '!last', '!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 show error message 
        else
          jabrespond(client, msg, 'Error: command not known - try !help', 'Error')
        end
      # Check if its a direct message from twitter, if yes: split and send to fridge; if no: send directly   
      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 method for all kind of jabber msg crap coming in
    rescue
      puts "Not a correct message type"
     end
     end
  rescue
    end
    puts "Keine Korrekte Message  "
   }
  end
  Thread.stop
   end
 
}
[[Kategorie:Küche]]
[[Kategorie:Projekte]]
Thread.stop

Aktuelle Version vom 5. April 2013, 12:20 Uhr

Ist:

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: ruby on rails, 'xmpp4r' gem (alles andere sollte schon da sein :));

Kann:

!help !last !last+time !events !sense !about !door !door+time


Will:

Ideen und Vorschläge was man(n) noch so reinbauen kann :)


Dankt:

Overflo, Wizard23, Marius für den Metafridge + Fin für die Twitteranbindung

Sushimako fürs Script-Hosting und seine Geduld, alle paar Stunden ne neue Version drauf zu tun :)

Diff:

Version 0.6: Added !door (Last 10 Metadoor Videos) and !door+time
Version 0.5: Added !about (About the Bot) and !events (For the Metalab Event list)
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 :)


Source:

 #!/usr/bin/env ruby
 version =  "Metafridge Jabber Bot - Version 0.6"
 # 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') # Backwards 'compatibility'
        jabrespond(client, msg, "Error: The '/command' is deprecated. Please use '!command'.", '/commands are deprecated, use !command')
      elsif (msg.body[0..0] == '!')
        case msg.body
        # !help
        when '!help'
          response  = "!last => Last 10 metafridge messages\n"
          response += "!last+time => Last 10 metafridge messages + timestamp\n"
          response += "!door => Last 10 metadoor videos\n"   
          response += "!door+time => Last 10 metadoor videos + timestamp\n"   
          response += "!sense => Metasense Status\n"
          response += "!events => List the next Metalab Events\n"
          response += "!about => About the Metafridge Jabber Bot\n"
          jabrespond(client, msg, response, 'Help?')
        # !about Show some Info about the bot + fridge
        when '!about'
          response  = "This is " + version +"\n"
          response += "By: Constantin -> Consti AT Consti PUNKT de\n"
          response += "For: Metafridge (TM)\n\n"
          response += "Project: http://metalab.at/wiki/Fridge\n"
          response += "Webinterface: http://fridge.metalab.at:1337\n"
          response += "On Twitter: http://twitter.com/metafridge\n"
          jabrespond(client, msg, response, 'About?')
        # !events (Metalab Events)
        when '!events', '!event'
          begin
            http_request = nil 
            Net::HTTP.start('metalab.at', 80) {|http|
             http_request = http.get('/feeds/events/')    
            }
            events = http_request.body.to_s
            events = events.gsub(/<\/title>/,"\n")
            titles = events.scan(/<title>(.*)$/) 
            response = "The next #{titles.length-1} Events in the Metalab:\n---------------------\n"
             for i in 1...titles.length
               response += "'#{titles[i]}'\n"
             end
            jabrespond(client, msg, response, 'metalab events')
          rescue
            jabrespond(client, msg, "Error: Either metalab.at is down, or the feed is broken. try again later. Don't kill me brother!", 'Error') 
            puts "timeout - feeds/events"
          end
        # !sense (Metasense)
        when '!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 metasense/status_lock.txt"
          end
        # !door and !door+time, last 10 videos on the door      
        when '!door', '!door+time'
          begin
            http_request = nil
            Net::HTTP.start('metadoor.soup.io', 80) {|http|
             http_request = http.get('/rss/')    
            }
            rss = http_request.body.to_s
            rss_youtube = rss.gsub(/></,"\n")
            youtube = rss_youtube.scan(/value="(http:\/\/www.youtube.com\/v\/.*)"$/)  
            rss_title = rss.gsub(/<\/title>/,"\n")
            title = rss_title.scan(/<title>(.*)$/)
            rss_pubdate = rss.gsub(/<\/pubDate>/,"\n")
            pubdate = rss_pubdate.scan(/<pubDate>(.*)$/)
            response = "The last 10 Metadoor Videos:\n---------------------\n"
            for i in 0...10
              j = i+1
              response += "#{j}: '#{title[j]}'\n'#{youtube[i]}'\n"
              if (msg.body == '!door+time')
               response += "(#{pubdate[i]})\n"
               response += "---------------------\n"
              end
            end
            jabrespond(client, msg, response, 'last 10 metadoor videos')
          rescue
            jabrespond(client, msg, 'Error: sorry. metadoor.soup.io seems to be down. try again later.', 'Error') 
            puts "timeout"
          end
        # !last and !last+time, last 10 messages on the fridge      
        when '!last', '!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 show error message  
        else
          jabrespond(client, msg, 'Error: command not known - try !help', 'Error')
        end
      # Check if its a direct message from twitter, if yes: split and send to fridge; if no: send directly    
      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 method for all kind of jabber msg crap coming in
    rescue
      puts "Not a correct message type"
    end
   end
 }
 Thread.stop