Ruby:如何忽略“何时”中的单词

时间:2018-08-01 16:36:55

标签: ruby telegram

即使用户的消息与我在“何时”中写的字词不完全匹配,该如何使漫游器回复?例如,如果有人发送了“ hello there”,而不仅仅是hello,我想让机器人触发。

我听说过when include?('example'),但无法使其正常工作。

    require 'telegram/bot'

token = 'x'

Telegram::Bot::Client.run(token) do |bot|

bot.listen do |message|
   puts "@#{message.from.username}: #{message.text}"
  case message.text

when 'hello'
    bot.api.send_message(chat_id: message.chat.id, text: "answer")

end
end
end

1 个答案:

答案 0 :(得分:2)

您可以使用regexp来匹配您的文本:

case message.text
when /hello/   # matches if there is the 'hello' somewhere in the string
  # ...
when /\Ahello/ # matches if the string starts with 'hello'
  # ...
end

或者您可以在include?条件下使用if

if message.text.include?('hello')
  # ...
end