Ruby 24/7 working XMPP bot -
could me this? create bot receives word in 1 language, looks in database, gets translation , sends back. how understand it's not possible on shared hostings, possible on own servers or vds. need make bot using libs eventmachine , xmpp4r? if yes how work many requests @ 1 time?
receing word, looking in database , sending response simple . bot should accept every new contact , add them roster (contact list).
take code. wrote bot "google bots" uses google translation service.
require 'rubygems' require 'xmpp4r-simple' require 'yaml' class monbottraducteur def initialize( from='fr', to='en' ) @url = 'http://ajax.googleapis.com/ajax/services/language/translate' @from = @to = end # def connect config= yaml::load( file.read( 'config/settings.yaml' ) ) @client = jabber::simple.new( config['settings']['jabber']['jid'], config['settings']['jabber']['password'] ) @client end # translate received message def translate( text="" ) params = { :langpair => "#{@from}|#{@to}", :q => text, :v => 1.0 } query = params.map{ |k,v| "#{k}=#{cgi.escape(v.to_s)}" }.join('&') reponse = net::http.get_response( uri.parse( "#{@url}?#{query}" ) ) repondre( reponse ) end # start bot activity def demarrer while true .received_messages |msg| translated_text = translate( msg.body ) @client.deliver( msg.from.to_s, translated_text.to_s ) end sleep 1 end end private # method send response def repondre( reponse ) json = json.parse( reponse.body ) if json['responsestatus'] == 200 json['responsedata']['translatedtext'] else raise(standarderror, response['responsedetails']) end end end bot = monbottraducteur.new bot.connect bot.demarrer
this bot receives messages, translate them using google service , send them senders.
ps : used yaml file setting.
best regards,
Comments
Post a Comment