Class: Qbot::Adapter::Mattermost
- Defined in:
- lib/qbot/adapter/mattermost.rb
Instance Method Summary collapse
- #access_token(token) ⇒ Object
- #close ⇒ Object
-
#initialize(url: nil, username: nil, password: nil) ⇒ Mattermost
constructor
A new instance of Mattermost.
- #listen(&block) ⇒ Object
- #post(text, **options) ⇒ Object
- #reply_to(message, text, **options) ⇒ Object
Methods inherited from Driver
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 |
#close ⇒ Object
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, **) resp = api_call(:post, "/posts", body: .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(, text, **) if [:channel_id] channel_id = [:channel_id] elsif [:channel_name] channel = channel([:channel_name]) channel_id = channel['id'] if channel end channel_id ||= .data['channel_id'] if return unless channel_id post(text, **.merge(channel_id: channel_id)) end |