Class: Slack::Service
- Inherits:
-
Object
- Object
- Slack::Service
- Defined in:
- lib/utilities/slack/service.rb
Instance Method Summary collapse
-
#initialize ⇒ Service
constructor
A new instance of Service.
- #list_channels(limit: 100, cursor: nil, exclude_archived: true) ⇒ Object
- #post_message(channel:, text:, username: nil, thread_ts: nil) ⇒ Object
- #test_connection ⇒ Object
- #tool_definitions ⇒ Object
Constructor Details
#initialize ⇒ Service
Returns a new instance of Service.
9 10 11 12 13 14 15 16 |
# File 'lib/utilities/slack/service.rb', line 9 def initialize ensure_env! @client = ::Slack::Web::Client.new( token: Mcpeasy::Config.slack_bot_token, timeout: 10, # 10 second timeout open_timeout: 5 # 5 second connection timeout ) end |
Instance Method Details
#list_channels(limit: 100, cursor: nil, exclude_archived: true) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/utilities/slack/service.rb', line 71 def list_channels(limit: 100, cursor: nil, exclude_archived: true) params = { types: "public_channel,private_channel", limit: [limit.to_i, 1000].min, exclude_archived: exclude_archived } params[:cursor] = cursor if cursor && !cursor.to_s.strip.empty? response = @client.conversations_list(params) if response["ok"] channels = response["channels"].to_a.map do |channel| { name: channel["name"], id: channel["id"] } end { channels: channels, has_more: response.dig("response_metadata", "next_cursor") ? true : false, next_cursor: response.dig("response_metadata", "next_cursor") } else raise "Failed to list channels: #{response["error"]}" end rescue ::Slack::Web::Api::Errors::SlackError => e raise "Slack API Error: #{e.}" end |
#post_message(channel:, text:, username: nil, thread_ts: nil) ⇒ Object
18 19 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 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/utilities/slack/service.rb', line 18 def (channel:, text:, username: nil, thread_ts: nil) # Clean up parameters clean_channel = channel.to_s.sub(/^#/, "").strip clean_text = text.to_s.strip # Validate inputs raise "Channel cannot be empty" if clean_channel.empty? raise "Text cannot be empty" if clean_text.empty? # Build request parameters params = { channel: clean_channel, text: clean_text } params[:username] = username if username && !username.to_s.strip.empty? params[:thread_ts] = thread_ts if thread_ts && !thread_ts.to_s.strip.empty? # Retry logic for reliability max_retries = 3 retry_count = 0 begin response = @client.chat_postMessage(params) if response["ok"] response else raise "Failed to post message: #{response["error"]} (#{response.inspect})" end rescue ::Slack::Web::Api::Errors::TooManyRequestsError => e retry_count += 1 if retry_count <= max_retries sleep_time = e.retry_after || 1 sleep(sleep_time) retry else raise "Slack API Error: #{e.}" end rescue ::Slack::Web::Api::Errors::SlackError => e retry_count += 1 if retry_count <= max_retries && retryable_error?(e) sleep(0.5 * retry_count) # Exponential backoff retry else raise "Slack API Error: #{e.}" end rescue => e Mcpeasy::Config.ensure_config_dirs File.write(Mcpeasy::Config.log_file_path("slack", "error"), "#{Time.now}: Slack::Service error: #{e.class}: #{e.}\n#{e.backtrace.join("\n")}\n", mode: "a") raise e end end |
#test_connection ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/utilities/slack/service.rb', line 101 def test_connection response = @client.auth_test if response["ok"] response else raise "Authentication failed: #{response["error"]}" end rescue ::Slack::Web::Api::Errors::SlackError => e raise "Slack API Error: #{e.}" end |
#tool_definitions ⇒ Object
113 114 |
# File 'lib/utilities/slack/service.rb', line 113 def tool_definitions end |