Ruby 24/7 工作 XMPP 机器人

发布于 2024-10-05 10:00:27 字数 157 浏览 8 评论 0原文

有人可以帮我解决这个问题吗? 我想创建一个机器人,它接收一种语言的单词,查找数据库,获取翻译并将其发回。我的理解是,这在共享主机上是不可能的,但在自己的服务器或 VDS 上是可能的。那么我是否需要使用 EventMachine 和 xmpp4r 等库来制作我的机器人?如果是,如何同时处理多个请求?

Could someone help me with this?
I would to create a bot which receives word in one language, looks in database, gets translation and sends it back. How i understand it's not possible on shared hostings, but possible on own servers or VDS. So do i need to make my bot using libs like EventMachine and xmpp4r? If yes how to work with many requests at one time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饭团 2024-10-12 10:00:27

接收单词、查找数据库并将响应发回非常简单。
您的机器人应该接受每个新联系人并将其添加到其名册(联系人列表)中。

看看这段代码。我写了一个像“Google Bots”这样的机器人,它使用谷歌翻译服务。

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 = from
  @to   = 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 the 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 the bot activity
 def demarrer
  while true
     .received_messages do |msg|
    translated_text = translate( msg.body )
    @client.deliver( msg.from.to_s, translated_text.to_s )  
      end   
     sleep 1
  end
 end

 private
 # A method to send back the 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

该机器人接收消息,使用谷歌服务翻译它们并将它们发送回发件人。

PS:我使用 yaml 文件进行设置。

此致,

Receing a word, looking in database and sending the response back is very simple .
Your bot should accept every new contact and add them to its roster (contact list).

Take a look a this code. I wrote a bot like "Google Bots" it 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 = from
  @to   = 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 the 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 the bot activity
 def demarrer
  while true
     .received_messages do |msg|
    translated_text = translate( msg.body )
    @client.deliver( msg.from.to_s, translated_text.to_s )  
      end   
     sleep 1
  end
 end

 private
 # A method to send back the 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 and send them back to senders.

PS : I used a yaml file for the setting.

Best regards,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文