2017-10-15 8 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

질문을 보완 할 수 있습니까? 어떤 라이브러리를 사용하고 있습니까?, 웹 훅 또는 폴링을 사용하고 있습니까? –

+0

전 telegram-bot-ruby-0.8.4 lib를 사용하고 있습니다. 웹 훅과 폴링을 사용하지 않습니다. – 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

사람들이 가져 오는 URL에 임의의 것을 삽입 할 수 없도록 to_i 또는 다른 입력 유효성 검사를 사용해야합니다. 자체 특성을 추가 할 수 있기 때문에 보안 문제 일 수 있습니다. –