Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'telegram/bot'
- require 'accuweather'
- require 'yandex-translator'
- token = '***'
- class Float
- def to_cel
- (self-32)*5/9
- end
- end
- module Answer
- TRANSLATOR = Yandex::Translator.new('***')
- RE = /(\/\w*)(\s)/
- def self.command_extraction message
- match1 = message.text.match RE
- begin
- match1[1]
- rescue
- nil
- end
- end
- def self.content_extraction message
- message.text.sub RE, ""
- end
- @translation_of = lambda do |word|
- ans = TRANSLATOR.translate word, to: 'ru'
- end
- @weather_of = lambda do |word|
- city = Accuweather.city_search(name: word).first
- if city
- current_weather = Accuweather.get_conditions(location_id: city.id).current
- temp = current_weather.temperature.to_f.to_cel.round
- sky = current_weather.weather_text
- real_feel = current_weather.real_feel.to_f.to_cel.round
- ans = "#{temp} C, #{sky}, real_feel: #{real_feel}"
- end
- end
- ACTION = {'/trans' => @translation_of, '/weather' => @weather_of}
- def answer message
- command = command_extraction message
- action = ACTION.fetch(command, nil)
- if action
- action.call content_extraction message
- else
- nil
- end
- end
- module_function :answer
- class << self
- private :command_extraction, :content_extraction
- end
- end
- Telegram::Bot::Client.run(token) do |bot|
- bot.listen do |message|
- answer = Answer.answer message
- begin
- bot.api.sendMessage(chat_id: message.chat.id, text: answer)
- rescue
- nil
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment