Method: ChatBot#say

Defined in:
lib/chatx.rb

#say(content, room_id, server: @default_server) ⇒ Object

Speaks in a room! Not much to say here, but much to say in the room that is passed!

If you’re trying to reply to a message, please use the Message#reply method.

Parameters:

  • room_id (#to_i)

    The ID of the room to be spoken in

  • content (String)

    The text of message to send

Returns:

  • A meaningless value

Keyword Arguments:

  • server (String)

    The server to send the messon on.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/chatx.rb', line 165

def say(content, room_id, server: @default_server)
  fkey = get_fkey(server, "/rooms/#{room_id}")
  if content.to_s.empty?
    @logger.warn "Message is empty, not posting: '#{content}'"
    return
  end
  @logger.warn "Message too long, truncating each line to 500 chars: #{content}" if content.to_s.split("\n").any? { |line| line.length > 500 }
  resp ||= nil
  loop do
    begin
      resp = @agent.post("https://chat.#{server}.com/chats/#{room_id}/messages/new", fkey: fkey, text: content.to_s.split("\n").map { |line| line[0...500] }.join("\n"))
    rescue Mechanize::ResponseCodeError
      @logger.error "Posting message to room #{room_id} failed. Retrying... #{content.to_s.split("\n").map { |line| line[0...500] }.join("\n")}"
      sleep 0.3 # A magic number I just chose for no reason
      retry
    end
    break unless JSON.parse(resp.content)["id"].nil?
    content = " #{content}"
  end
  return JSON.parse(resp.content)["id"].to_i
end