2017-10-15 87 views
0

我需要從用戶的消息中獲取一個整數,並將其置於變量日。 我該怎麼做?Ruby Telegram bot - 如何從用戶消息中獲取文本並將其放入變量?

這裏的一些代碼:

Telegram::Bot::Client.run(token) do |bot| 
bot.listen do |message| 
case message.text 
when '/start' 
    bot.api.sendMessage(chat_id: message.chat.id, text: "Привет, #{message.from.first_name}. Чтобы узнать сегодняшнее расписание, напишите /today") 

when '/today' 

    bot.api.sendMessage(chat_id: message.chat.id, text: "#{date}") 
if discipline1_name.size == 0 && discipline2_name.size == 0 && discipline3_name.size == 0 && discipline4_name.size == 0 && discipline5_name.size == 0 && discipline6_name.size == 0 then 
    (bot.api.sendMessage(chat_id: message.chat.id, text: "Сегодня воскресенье, можете отдыхать :)") 
    bot.api.sendMessage(chat_id: message.chat.id, text: "Чтобы узнать расписание на другой день, введите дату в формате /13 (13 - день).")) 
else 
    unless discipline1_name.size == 0 
    bot.api.sendMessage(chat_id: message.chat.id, text: "1. #{discipline1_name} в кабинете #{discipline1_cab}") 
    else 
    bot.api.sendMessage(chat_id: message.chat.id, text: "1. ===========") 
    end 

,喜歡它下降了discipline2,3,... 6

我需要的是這樣的:

when '#{message}' 
day = message.to_i 
page = Nokogiri::HTML(open("http://rating.ivpek.ru/timetable/timetable/show?gid=222&date=2017-10-#{day}")) 

但它不起作用

+0

你能補充你的問題嗎?你使用哪個庫?你使用webhooks還是輪詢? –

+0

我正在使用telegram-bot-ruby-0.8.4 lib,我不使用webhooks和polling – rafulin

+0

到目前爲止你有什麼?你如何檢索信息?信息如何顯示?您是否閱讀過文檔?你能展示一些代碼嗎? – Stefan

回答

0

這應該工作:

num = message.text.to_i 

你並不需要整個case聲明如果用戶在發送給您的唯一有效的就是一個數字。

如果您確實使用了case語句,則應該使用else關鍵字使用默認情況,而不是您在消息與自身匹配時嘗試執行的奇怪事情。

0

我做得很好:而不是做#to_i方法我做了#to_s,它的工作! 下面的代碼:

when "#{message}" 
    day = message.to_s 
    page = page = Nokogiri::HTML(open("http://rating.ivpek.ru/timetable/timetable/show?gid=222&date=2017-10-#{day}")) 

    date = page.css('span')[1].text 

    discipline1_name = page.css('tr[2] .redips-drag').text 
    ... 
    discipline6_name = page.css('tr[7] .redips-drag').text 

    discipline1_cab = page.css('tr[2] td#cabinet').text 
    ... 
    discipline6_cab = page.css('tr[7] td#cabinet').text 

    bot.api.sendMessage(chat_id: message.chat.id, text: "#{date}") 
if discipline1_name.size == 0 && discipline2_name.size == 0 && discipline3_name.size == 0 && discipline4_name.size == 0 && discipline5_name.size == 0 && discipline6_name.size == 0 then 
    (bot.api.sendMessage(chat_id: message.chat.id, text: "Сегодня воскресенье, можете отдыхать :)") 
    bot.api.sendMessage(chat_id: message.chat.id, text: "Чтобы узнать расписание на другой день, введите дату в формате /13 (13 - день).")) 
else 
    unless discipline1_name.size == 0 
    bot.api.sendMessage(chat_id: message.chat.id, text: "1. #{discipline1_name} в кабинете #{discipline1_cab}") 
    else 
    bot.api.sendMessage(chat_id: message.chat.id, text: "1. ===========") 
    end 
... 

    unless discipline6_name.size == 0 
    bot.api.sendMessage(chat_id: message.chat.id, text: "6. #{discipline6_name} в кабинете #{discipline6_cab}") 
    else 
    bot.api.sendMessage(chat_id: message.chat.id, text: "6. ===========") 
    end 
    bot.api.sendMessage(chat_id: message.chat.id, text: "Чтобы узнать расписание на другой день, введите дату в формате /13 (13 - день).") 
+0

您應該使用to_i或其他輸入驗證,以便人們不能將任意東西注入到您要提取的URL中;這可能是一個安全問題,因爲他們可以添加自己的屬性。 –

相關問題