Class: Heathrow::Sources::Slack
- Inherits:
-
Object
- Object
- Heathrow::Sources::Slack
- Defined in:
- lib/heathrow/sources/slack.rb
Overview
Slack source implementation
Instance Method Summary collapse
- #fetch_messages ⇒ Object
-
#initialize(source) ⇒ Slack
constructor
A new instance of Slack.
- #mark_as_read(message_id, channel_id = nil) ⇒ Object
- #name ⇒ Object
- #send_message(recipient, content, metadata = {}) ⇒ Object
- #test_connection ⇒ Object
Constructor Details
#initialize(source) ⇒ Slack
Returns a new instance of Slack.
13 14 15 16 17 18 19 20 21 |
# File 'lib/heathrow/sources/slack.rb', line 13 def initialize(source) @source = source @base_url = 'https://slack.com/api' # Cache for user and channel names @users_cache = {} @channels_cache = {} @last_fetch = {} end |
Instance Method Details
#fetch_messages ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/heathrow/sources/slack.rb', line 44 def = [] channel_ids = @source.config['channel_ids'] || [] dm_user_ids = @source.config['dm_user_ids'] || [] # Fetch channel messages channel_ids.each do |channel_id| = (channel_id) .concat() end # Fetch direct messages dm_user_ids.each do |user_id| = (user_id) .concat() end # Fetch all channels if no specific ones configured if channel_ids.empty? && dm_user_ids.empty? all_channels = fetch_all_conversations all_channels.each do |channel| # Debug logging File.write('/tmp/slack_channels.log', "Channel: #{channel['name']} (#{channel['id']}), is_member: #{channel['is_member']}\n", mode: 'a') if ENV['DEBUG'] # Only fetch from channels we're a member of next unless channel['is_member'] if channel['is_im'] # Direct message = (channel['user'] || channel['id']) .concat() else # Any other type of channel (public, private, group) = (channel['id']) .concat() end end end end |
#mark_as_read(message_id, channel_id = nil) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/heathrow/sources/slack.rb', line 114 def mark_as_read(, channel_id = nil) return { success: false, error: 'Channel ID required for Slack' } unless channel_id # Determine the correct API endpoint based on channel type channel_type = detect_channel_type(channel_id) endpoint = case channel_type when 'channel' then 'channels.mark' when 'group' then 'groups.mark' when 'im' then 'im.mark' when 'mpim' then 'mpim.mark' else 'conversations.mark' end uri = URI("#{@base_url}/#{endpoint}") params = { channel: channel_id, ts: } response = make_api_request(uri, params) if response && response['ok'] { success: true } else error = response ? response['error'] : 'Unknown error' { success: false, error: error } end rescue => e { success: false, error: e. } end |
#name ⇒ Object
23 24 25 |
# File 'lib/heathrow/sources/slack.rb', line 23 def name 'slack' end |
#send_message(recipient, content, metadata = {}) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/heathrow/sources/slack.rb', line 87 def (recipient, content, = {}) channel = resolve_channel(recipient) uri = URI("#{@base_url}/chat.postMessage") params = { channel: channel, text: content, as_user: true } # Handle thread replies if [:thread_ts] params[:thread_ts] = [:thread_ts] end response = make_api_request(uri, params) if response && response['ok'] { success: true, message_id: response['ts'] } else error = response ? response['error'] : 'Unknown error' { success: false, error: error } end rescue => e { success: false, error: e. } end |
#test_connection ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/heathrow/sources/slack.rb', line 27 def test_connection uri = URI("#{@base_url}/auth.test") response = make_api_request(uri) if response && response['ok'] puts "Successfully connected to Slack workspace: #{response['team']}" true else error = response ? response['error'] : 'Unknown error' puts "Failed to connect: #{error}" false end rescue => e puts "Connection error: #{e.}" false end |