Class: Kanal::Interfaces::Telegram::TelegramInterface

Inherits:
Core::Interfaces::Interface
  • Object
show all
Defined in:
lib/kanal/interfaces/telegram/telegram_interface.rb

Overview

This interface helps working with telegram

Instance Method Summary collapse

Constructor Details

#initialize(core, bot_token) ⇒ TelegramInterface

Returns a new instance of TelegramInterface.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kanal/interfaces/telegram/telegram_interface.rb', line 15

def initialize(core, bot_token)
  super(core)

  @bot_token = bot_token

  @bot = ::Telegram::Bot::Client.new(bot_token)

  @core.register_plugin Kanal::Plugins::Batteries::BatteriesPlugin.new
  @core.register_plugin Kanal::Interfaces::Telegram::Plugins::TelegramIntegrationPlugin.new

  @link_parser = Kanal::Interfaces::Telegram::Helpers::TelegramLinkParser.new
end

Instance Method Details

#consume_output(output) ⇒ Object



36
37
38
# File 'lib/kanal/interfaces/telegram/telegram_interface.rb', line 36

def consume_output(output)
  send_output @bot, output
end

#create_input(message) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kanal/interfaces/telegram/telegram_interface.rb', line 40

def create_input(message)
  input = @core.create_input

  if message.attributes.key?(:data)
    # Inline button pressed
    input.tg_button_pressed = message.data
    input.tg_chat_id = message.from.id
    input.tg_username = message.from.username
  else
    # Regular message received
    input.tg_chat_id = message.chat.id
    input.tg_username = message.chat.username || message.from.username

    if !message.text.nil?
      input.tg_text = message.text
    end

    if !message.photo.nil?
      # Array of images contains thumbnails, we take 3rd element to get the high-res image
      input.tg_image_link = @link_parser.get_file_link message.photo.last.file_id, @bot, @bot_token
    end

    if !message.audio.nil?
      input.tg_audio_link = @link_parser.get_file_link message.audio.file_id, @bot, @bot_token
    end

    if !message.video.nil?
      input.tg_video_link = @link_parser.get_file_link message.video.file_id, @bot, @bot_token
    end

    if !message.document.nil?
      input.tg_document_link = @link_parser.get_file_link message.document.file_id, @bot, @bot_token
    end
  end

  input
end

#startObject



28
29
30
31
32
33
34
# File 'lib/kanal/interfaces/telegram/telegram_interface.rb', line 28

def start
  ::Telegram::Bot::Client.run(@bot_token) do |bot|
    bot.listen do |message|
      router.consume_input create_input message
    end
  end
end