Class: Qbot::Adapter::Mattermost

Inherits:
Driver
  • Object
show all
Defined in:
lib/qbot/adapter/mattermost.rb

Instance Method Summary collapse

Methods inherited from Driver

build, inherited, #run

Constructor Details

#initialize(url: nil, username: nil, password: nil) ⇒ Mattermost

Returns a new instance of Mattermost.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/qbot/adapter/mattermost.rb', line 12

def initialize(url: nil, username: nil, password: nil)
  @mm_url = url || ENV['QBOT_MATTERMOST_URL']
  @server = URI.join(@mm_url, '/').to_s

  resp = api_call(:post, '/users/login', :body => {
    login_id: username || ENV['QBOT_MATTERMOST_USERNAME'],
    password: password || ENV['QBOT_MATTERMOST_PASSWORD'],
  })

  access_token(resp.headers['token'])
  raise 'Login failed' unless @token
end

Instance Method Details

#access_token(token) ⇒ Object



25
26
27
# File 'lib/qbot/adapter/mattermost.rb', line 25

def access_token(token)
  @token = token
end

#closeObject



33
34
35
# File 'lib/qbot/adapter/mattermost.rb', line 33

def close
  EM.stop
end

#listen(&block) ⇒ Object



29
30
31
# File 'lib/qbot/adapter/mattermost.rb', line 29

def listen(&block)
  EM.run { start_connection(&block) }
end

#post(text, **options) ⇒ Object



37
38
39
40
# File 'lib/qbot/adapter/mattermost.rb', line 37

def post(text, **options)
  resp = api_call(:post, "/posts", body: options.merge(message: text))
  Qbot.app.logger.info("#{self.class} - Post message: #{resp.status} - '#{text}'")
end

#reply_to(message, text, **options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/qbot/adapter/mattermost.rb', line 42

def reply_to(message, text, **options)
  if options[:channel_id]
    channel_id = options[:channel_id]
  elsif options[:channel_name]
    channel = channel(options[:channel_name])
    channel_id = channel['id'] if channel
  end

  channel_id ||= message.data['channel_id'] if message
  return unless channel_id

  post(text, **options.merge(channel_id: channel_id))
end