使用 ruby Telegram 机器人发送消息的示例方法的概率
Probability on sample method sending messages with a ruby Telegram bot
我有一个用 ruby 编写的 Telegram 内联机器人。我想更好地控制它回答的内容,有时 关闭它,即使它 应该 回复。
这是我现在拥有的:
when /WHATEVER/
whatever = ['option 1', 'option 2']
bot.api.send_message(chat_id: message.chat.id, text: "#{whatever.sample}")
我希望它有 50% 的时间回复选项 1,30% 的时间回复选项 2,20% 的时间不回复。
可能吗?
对不起,如果没有解释正确,我不是程序员,英语不是我的母语。
谢谢
您可以生成一个随机数并使用它来决定采取什么行动:
r = rand # 0.0 <= r < 1
if r <= 0.5
bot.api.send_message(chat_id: message.chat.id, text: "option 1")
else if r <= 0.8
bot.api.send_message(chat_id: message.chat.id, text: "option 2")
else
# do nothing, you can actually ommit the `else` clause
end
不太优雅的方式:
options = ["1", "1", "1", "1", "1", "2", "2", "2", nil, nil]
option = options.sample
bot.api.send_message(chat_id: message.chat.id, text: option) if option
我有一个用 ruby 编写的 Telegram 内联机器人。我想更好地控制它回答的内容,有时 关闭它,即使它 应该 回复。
这是我现在拥有的:
when /WHATEVER/
whatever = ['option 1', 'option 2']
bot.api.send_message(chat_id: message.chat.id, text: "#{whatever.sample}")
我希望它有 50% 的时间回复选项 1,30% 的时间回复选项 2,20% 的时间不回复。
可能吗?
对不起,如果没有解释正确,我不是程序员,英语不是我的母语。
谢谢
您可以生成一个随机数并使用它来决定采取什么行动:
r = rand # 0.0 <= r < 1
if r <= 0.5
bot.api.send_message(chat_id: message.chat.id, text: "option 1")
else if r <= 0.8
bot.api.send_message(chat_id: message.chat.id, text: "option 2")
else
# do nothing, you can actually ommit the `else` clause
end
不太优雅的方式:
options = ["1", "1", "1", "1", "1", "2", "2", "2", nil, nil]
option = options.sample
bot.api.send_message(chat_id: message.chat.id, text: option) if option