Class: Telegram::Rabbit::ClientAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/telegram/rabbit/client_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ClientAdapter

Returns a new instance of ClientAdapter.



13
14
15
16
17
# File 'lib/telegram/rabbit/client_adapter.rb', line 13

def initialize opts={}
  @options      = default_options.merge(opts)
  @logger       = get_option :logger_instance
  @logger.level = get_option :log_level
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



10
11
12
# File 'lib/telegram/rabbit/client_adapter.rb', line 10

def client
  @client
end

Instance Method Details

#startObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/telegram/rabbit/client_adapter.rb', line 20

def start
  logger.debug "Starting ClientAdapter"
  @connection = Bunny.new(get_option :bunny).start
  at_exit { @connection.close }

  #queues
  send_queue_name    = "#{queue_prefix}.messages"
  receive_queue_name = "#{queue_prefix}.commands"

  @channel       = @connection.create_channel
  @send_queue    = @channel.queue send_queue_name
  @receive_queue = @channel.queue receive_queue_name

  logger.debug "Created queues:"
  logger.debug "  send:    #{send_queue_name}"
  logger.debug "  receive: #{receive_queue_name}"

  @receive_queue.subscribe do |info,,raw_data|
    data = Marshal.load(raw_data)
    command, payload = extract_command(data)
    logger.debug "Received command: #{command}"
    logger.debug payload.inspect
    exec_api_command command, payload
  end

  logger.debug "Starting bot client"

  @client = client || Telegram::Bot::Client.new(get_option :bot, :token)
  @client.run do |bot|
    logger.debug "Bot client started"
    bot.listen do |message|
      logger.debug "Received message:"
      logger.debug message.inspect
      send_to_app message
    end
  end

end