簡體   English   中英

使用 ruby​​ Telegram bot 發送消息的示例方法的概率

[英]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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM