Ruby 中的随机消息?
Random msg in Ruby?
在聊天机器人中,有一个功能可以让机器人用一个短语来回答确定的命令,例如,in
match /^Loggy, te amo/, :use_prefix => false
def execute(user)
if user.name.eql? 'Cquintero6'
@client.send_msg '/me besa a Carlos'
@client.send_msg 'Io también te jamón <3'
else
@client.send_msg "#{user.name}, ¿En serio crees que amaria a una basurilla como tú? Sáquese. "
end
end
当我写 "Loggy, te amo" 时,机器人会在聊天中回答,具体取决于用户。问题是,我如何制作一个答案列表,以便机器人 select 随机回答一个?谢谢! (“@Client.send_msg”的随机字符串)
构造数组很简单:
phrases = [
"a phrase",
"another phrase"
]
你也可以使用fakergem。有关选项,请参阅其自述文件,但这是一个示例:
phrases = 10.times.map { Faker::Company.catch_phrase }
此时你只需要select一个随机条目,很简单:
random_phrase = phrases.sample
@client.send_msg random_phrase
假设您还想获得一个随机短语,但根据运行时变量更改文本。这是一种方法:
require 'erb'
phrases = [
"your name is <%= name %>"
]
name = user.name
random_phrase = ERB.new(phrases.sample).result(binding)
@client.send_msg random_phrase
这会在运行时编译 ERB 字符串
在聊天机器人中,有一个功能可以让机器人用一个短语来回答确定的命令,例如,in
match /^Loggy, te amo/, :use_prefix => false
def execute(user)
if user.name.eql? 'Cquintero6'
@client.send_msg '/me besa a Carlos'
@client.send_msg 'Io también te jamón <3'
else
@client.send_msg "#{user.name}, ¿En serio crees que amaria a una basurilla como tú? Sáquese. "
end
end
当我写 "Loggy, te amo" 时,机器人会在聊天中回答,具体取决于用户。问题是,我如何制作一个答案列表,以便机器人 select 随机回答一个?谢谢! (“@Client.send_msg”的随机字符串)
构造数组很简单:
phrases = [
"a phrase",
"another phrase"
]
你也可以使用fakergem。有关选项,请参阅其自述文件,但这是一个示例:
phrases = 10.times.map { Faker::Company.catch_phrase }
此时你只需要select一个随机条目,很简单:
random_phrase = phrases.sample
@client.send_msg random_phrase
假设您还想获得一个随机短语,但根据运行时变量更改文本。这是一种方法:
require 'erb'
phrases = [
"your name is <%= name %>"
]
name = user.name
random_phrase = ERB.new(phrases.sample).result(binding)
@client.send_msg random_phrase
这会在运行时编译 ERB 字符串