Class: Nanobot::Channels::Telegram

Inherits:
BaseChannel show all
Defined in:
lib/nanobot/channels/telegram.rb

Overview

Telegram channel integration using the telegram-bot-ruby gem. Listens for messages via long polling and shows typing indicators.

Instance Attribute Summary

Attributes inherited from BaseChannel

#bus, #config, #logger, #name

Instance Method Summary collapse

Methods inherited from BaseChannel

#allowed?, #initialize, #running?

Constructor Details

This class inherits a constructor from Nanobot::Channels::BaseChannel

Instance Method Details

#send(message) ⇒ Object

Send a reply to a Telegram chat, splitting at the 4096-char limit.

Parameters:



40
41
42
43
44
45
46
47
# File 'lib/nanobot/channels/telegram.rb', line 40

def send(message)
  return unless @bot

  stop_typing(message.chat_id)
  split_message(message.content, 4096).each do |chunk|
    @bot.api.send_message(chat_id: message.chat_id, text: chunk)
  end
end

#start ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nanobot/channels/telegram.rb', line 14

def start
  @running = true
  ::Telegram::Bot::Client.run(@config.token) do |bot|
    @bot = bot
    register_commands(bot)
    bot.listen do |message|
      break unless @running
      next unless message.is_a?(::Telegram::Bot::Types::Message)
      next unless message.text

      start_typing(message.chat.id.to_s)
      handle_message(
        sender_id: message.from.id.to_s,
        chat_id: message.chat.id.to_s,
        content: message.text
      )
    end
  end
end

#stop ⇒ Object



34
35
36
# File 'lib/nanobot/channels/telegram.rb', line 34

def stop
  @running = false
end