2017-10-15 4 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を使用しています。私はwebhooksとポーリングを使用しません。 – 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に人が任意のものを挿入できないようにしてください。独自の属性を追加できるため、セキュリティ上の問題となる可能性があります。 –

関連する問題