Class: Qbot::Adapter::Slack
Constant Summary collapse
- SLACK_API_URL =
'https://slack.com/api'
Instance Method Summary collapse
- #access_token(token) ⇒ Object
- #close ⇒ Object
-
#initialize(api_token: nil) ⇒ Slack
constructor
A new instance of Slack.
- #listen(&block) ⇒ Object
- #post(text, **options) ⇒ Object
- #reply_to(message, text, **options) ⇒ Object
Methods inherited from Driver
Constructor Details
#initialize(api_token: nil) ⇒ Slack
Returns a new instance of Slack.
14 15 16 17 18 |
# File 'lib/qbot/adapter/slack.rb', line 14 def initialize(api_token: nil) @server = URI.join(SLACK_API_URL, '/').to_s access_token(api_token || ENV['QBOT_SLACK_API_TOKEN']) end |
Instance Method Details
#access_token(token) ⇒ Object
20 21 22 |
# File 'lib/qbot/adapter/slack.rb', line 20 def access_token(token) @token = token end |
#close ⇒ Object
28 29 30 |
# File 'lib/qbot/adapter/slack.rb', line 28 def close EM.stop end |
#listen(&block) ⇒ Object
24 25 26 |
# File 'lib/qbot/adapter/slack.rb', line 24 def listen(&block) EM.run { start_connection(&block) } end |
#post(text, **options) ⇒ Object
32 33 34 35 |
# File 'lib/qbot/adapter/slack.rb', line 32 def post(text, **) resp = api_call(:post, "/chat.postMessage", .merge(text: text)) Qbot.app.logger.info("#{self.class} - Post message: #{resp.status} - '#{text}'") end |
#reply_to(message, text, **options) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/qbot/adapter/slack.rb', line 37 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'] if return unless channel_id post(text, **.merge(channel: channel_id)) end |