2023-02-13 12:41:30 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
require 'json'
|
|
|
|
|
require 'net/http'
|
|
|
|
|
require 'net/https'
|
|
|
|
|
require 'uri'
|
|
|
|
|
|
2023-09-08 16:34:13 +02:00
|
|
|
class CdrSignalApi
|
2023-02-13 12:41:30 +00:00
|
|
|
def initialize(api_url, token)
|
|
|
|
|
@token = token
|
|
|
|
|
@last_update = 0
|
2023-09-08 16:34:13 +02:00
|
|
|
@api_url = api_url
|
2023-02-13 12:41:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def parse_hash(hash)
|
|
|
|
|
ret = {}
|
|
|
|
|
hash.map do |k, v|
|
|
|
|
|
ret[k] = CGI.encode(v.to_s.gsub('\\\'', '\''))
|
|
|
|
|
end
|
|
|
|
|
ret
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get(api)
|
2023-09-08 16:34:13 +02:00
|
|
|
url = "#{@api_url}/bots/#{@token}/#{api}"
|
|
|
|
|
JSON.parse(Faraday.get(url, { Accept: "application/json" }).body)
|
2023-02-13 12:41:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def post(api, params = {})
|
2023-09-08 16:34:13 +02:00
|
|
|
url = "#{@api_url}/bots/#{@token}/#{api}"
|
|
|
|
|
JSON.parse(Faraday.post(url, params.to_json, { "Content-Type": "application/json", Accept: "application/json" }).body)
|
2023-02-13 12:41:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def fetch_self
|
|
|
|
|
get('')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def send_message(recipient, text, options = {})
|
|
|
|
|
post('send', { phoneNumber: recipient.to_s, message: text }.merge(parse_hash(options)))
|
|
|
|
|
end
|
|
|
|
|
end
|