Class: Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/telegram_imdb_bot/bot.rb

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Bot

Returns a new instance of Bot.



7
8
9
# File 'lib/telegram_imdb_bot/bot.rb', line 7

def initialize(token)
  @token = token
end

Instance Method Details

#get_movie(query) ⇒ Object



11
12
13
# File 'lib/telegram_imdb_bot/bot.rb', line 11

def get_movie(query)
  Imdb::Search.new(query).movies.first
end


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/telegram_imdb_bot/bot.rb', line 15

def print_movie(movie)
  s = []
  s << "Title: #{movie.title}"
  s << "Director: #{movie.director.first}"
  s << "Cast: #{movie.cast_members[0..2].join(', ')}"
  s << "Rating: #{movie.rating}"
  s << ''
  s << movie.plot
  s << movie.url
  s.join("\n")
end

#startObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/telegram_imdb_bot/bot.rb', line 27

def start
  Telegram::Bot::Client.run(@token) do |bot|
    bot.listen do |message|
      case message.text
      when /^\/imdb\s+(.*)$/
        bot.api.sendChatAction(chat_id: message.chat.id, action: 'typing')
        movie = get_movie($1)
        if movie
          poster = movie.poster
          if poster
            f = open(poster)
            filename = "#{f.path}.jpg"
            `mv '#{f.path}' '#{filename}'`
            bot.api.sendPhoto(chat_id: message.chat.id, photo: File.new(filename)) if File.exist?(filename)
          end
          bot.api.sendMessage(chat_id: message.chat.id, text: print_movie(movie), disable_web_page_preview: true)
        end
      end
    end
  end
end