<commit_before>require 'base64'
require 'net/http'
require 'open-uri'

namespace :check do
  namespace :whatsapp do

    def call_whatsapp_api(endpoint, path, payload, token, verb = 'Post', auth_type = 'Bearer', content_type = 'application/json')
      uri = URI("#{endpoint}/#{path}")
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = uri.scheme == 'https'
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      req = "Net::HTTP::#{verb}".constantize.new(uri.request_uri, 'Content-Type' => content_type, 'Authorization' => "#{auth_type} #{token}")
      if ['Post', 'Patch'].include?(verb)
        if content_type == 'application/json'
          req.body = payload.to_json
        else
          req.body = payload
        end
      end
      response = http.request(req)
      raise "Request to WhatsApp API failed with HTTP response code #{response.code} and body #{response.body}" if response.code.to_i >= 400
      JSON.parse(response.body)
    end

    def call_facebook_api(path, verb, params = {})
      params_string = []
      params.each do |key, value|
        value = value.to_json unless value.is_a?(String)
        params_string << "#{key}=#{value}"
      end
      url = "https://graph.facebook.com/v14.0/#{path}?#{params_string.join('&')}"
      puts "Calling #{url}..."
      uri = URI(url)
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      req = "Net::HTTP::#{verb}".constantize.new(uri.request_uri)
      response = http.request(req)
      raise "Request to Facebook API failed with HTTP response code #{response.code} and body #{response.body}" if response.code.to_i >= 400
      JSON.parse(response.body)
    end

    # Generate token (valid for 7 days)
    # Each instance / phone number / tipline has its own endpoint, user and password
    desc "Generate a token for WhatsApp API which is valid for 7 days, for example: $ bundle exec rake 'check:whatsapp:generate_token[endpoint,user,password,team_slug]'. If a team slug is passed, the token is saved automatically."
    task generate_token: :environment do |_t, args|
      endpoint, user, token, team_slug = args.to_a
      raise 'Please pass the endpoint URL, user and token as parameters' if endpoint.blank? || user.blank? || token.blank?
      auth = Base64.encode64("#{user}:#{token}").chomp
      response = call_whatsapp_api(endpoint, 'v1/users/login', {}, auth, 'Post', 'Basic')
      token = response.dig('users', 0, 'token')
      puts token<commit_msg>Remove any newlines from WhatsApp Base 64 authentication.<commit_after>require 'base64'
require 'net/http'
require 'open-uri'

namespace :check do
  namespace :whatsapp do

    def call_whatsapp_api(endpoint, path, payload, token, verb = 'Post', auth_type = 'Bearer', content_type = 'application/json')
      uri = URI("#{endpoint}/#{path}")
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = uri.scheme == 'https'
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      req = "Net::HTTP::#{verb}".constantize.new(uri.request_uri, 'Content-Type' => content_type, 'Authorization' => "#{auth_type} #{token}")
      if ['Post', 'Patch'].include?(verb)
        if content_type == 'application/json'
          req.body = payload.to_json
        else
          req.body = payload
        end
      end
      response = http.request(req)
      raise "Request to WhatsApp API failed with HTTP response code #{response.code} and body #{response.body}" if response.code.to_i >= 400
      JSON.parse(response.body)
    end

    def call_facebook_api(path, verb, params = {})
      params_string = []
      params.each do |key, value|
        value = value.to_json unless value.is_a?(String)
        params_string << "#{key}=#{value}"
      end
      url = "https://graph.facebook.com/v14.0/#{path}?#{params_string.join('&')}"
      puts "Calling #{url}..."
      uri = URI(url)
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      req = "Net::HTTP::#{verb}".constantize.new(uri.request_uri)
      response = http.request(req)
      raise "Request to Facebook API failed with HTTP response code #{response.code} and body #{response.body}" if response.code.to_i >= 400
      JSON.parse(response.body)
    end

    # Generate token (valid for 7 days)
    # Each instance / phone number / tipline has its own endpoint, user and password
    desc "Generate a token for WhatsApp API which is valid for 7 days, for example: $ bundle exec rake 'check:whatsapp:generate_token[endpoint,user,password,team_slug]'. If a team slug is passed, the token is saved automatically."
    task generate_token: :environment do |_t, args|
      endpoint, user, token, team_slug = args.to_a
      raise 'Please pass the endpoint URL, user and token as parameters' if endpoint.blank? || user.blank? || token.blank?
      auth = Base64.encode64("#{user}:#{token}").gsub("\n", '')
      response = call_whatsapp_api(endpoint, 'v1/users/login', {}, auth, 'Post', 'Basic')
      token = response.dig('users', 0, 'token')
      puts token
{'fraction_non_alphanumeric': 0.13899383575263471, 'fraction_numerical': 0.007953867568104992, 'mean_word_length': 3.2519019442096364, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3370731', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1551, 'n_words': 432}