<commit_before># encoding: utf-8

require_relative 'models/message'
require_relative 'utils'

module RecastAI
  module Message
    def handle_message(request, &block)
      request.body.rewind if request.body.respond_to?(:rewind)
      body = request.body.is_a?(String) ? request.body : request.body.read
      message = Msg.new(body)

      yield(message)
    end

    def send_message(payload, conversation_id, token: nil)
      token ||= @token
      raise RecastError.new('Token is missing') if token.nil?

      response = HTTParty.post(
        "#{Utils::CONVERSATION_ENDPOINT}#{conversation_id}/messages",
        body: { messages: payload },
        headers: { 'Authorization' => "Token #{token}" }
      )
      raise RecastError.new(JSON.parse(response.body)['message']) if response.code != 201

      response
    end

    def broadcast_message(payload, token: nil)
      token ||= @token
      raise RecastError.new('Token is missing') if token.nil?

      response = HTTParty.post(
        Utils::MESSAGE_ENDPOINT,
        body: { messages: payload },
        headers: { 'Authorization' => "Token #{token}" }
      )
      raise RecastError.new(JSON.parse(response.body)['message']) if response.code != 201

      response
    end
  end
end<commit_msg>fix(connect) jsonify the payload before sending the message to bot connector<commit_after># encoding: utf-8

require_relative 'models/message'
require_relative 'utils'

module RecastAI
  module Message
    def handle_message(request, &block)
      request.body.rewind if request.body.respond_to?(:rewind)
      body = request.body.is_a?(String) ? request.body : request.body.read
      message = Msg.new(body)

      yield(message)
    end

    def send_message(payload, conversation_id, token: nil)
      token ||= @token
      raise RecastError.new('Token is missing') if token.nil?

      response = HTTParty.post(
        "#{Utils::CONVERSATION_ENDPOINT}#{conversation_id}/messages",
        body: { messages: payload.to_json },
        headers: { 'Authorization' => "Token #{token}" }
      )
      raise RecastError.new(JSON.parse(response.body)['message']) if response.code != 201

      response
    end

    def broadcast_message(payload, token: nil)
      token ||= @token
      raise RecastError.new('Token is missing') if token.nil?

      response = HTTParty.post(
        Utils::MESSAGE_ENDPOINT,
        body: { messages: payload.to_json },
        headers: { 'Authorization' => "Token #{token}" }
      )
      raise RecastError.new(JSON.parse(response.body)['message']) if response.code != 201

      response
    end
  end
end
{'fraction_non_alphanumeric': 0.12111920275967804, 'fraction_numerical': 0.005366040628593331, 'mean_word_length': 3.0590979782270606, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4608981', 'n_tokens_mistral': 796, 'n_tokens_neox': 774, 'n_words': 181}