Class: Lita::Adapters::TelegramPlus

Inherits:
Adapter
  • Object
show all
Defined in:
lib/lita/adapters/telegram_plus.rb

Instance Method Summary collapse

Constructor Details

#initialize(robot) ⇒ TelegramPlus

Returns a new instance of TelegramPlus.



8
9
10
11
12
# File 'lib/lita/adapters/telegram_plus.rb', line 8

def initialize(robot)
  super
  @client = Telegram::Bot::Client.new(config.token, logger: ::Logger.new($stdout))
  robot.trigger(:connected)
end

Instance Method Details

#runObject

Connect to Telegram and listen to incoming messages



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lita/adapters/telegram_plus.rb', line 15

def run
  @client.listen do |message|
    user = Lita::User.find_by_id(message.from.id)
    user = Lita::User.create(id: message.from.id, name: message.from.first_name) unless user

    if user.name == user.id
      user = Lita::User.create(id: message.from.id, name: message.from.first_name)
    end

    chat = Lita::Room.new(message.chat.id)
    source = Lita::Source.new(user: user, room: chat)

    message.text ||= ''
    message = Lita::Message.new(robot, message.text, source)

    robot.receive(message)
  end
end

#send_messages(target, messages) ⇒ Object

Send messages to the Telegram bot api



35
36
37
38
39
# File 'lib/lita/adapters/telegram_plus.rb', line 35

def send_messages(target, messages)
  messages.each do |message|
    @client.api.send_message(chat_id: target.room.to_i, text: message)
  end
end

#shutdownObject

Disconnect from Telegram



42
43
# File 'lib/lita/adapters/telegram_plus.rb', line 42

def shutdown
end