50 lines
1.2 KiB
Ruby
50 lines
1.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'json'
|
|
require 'net/http'
|
|
require 'net/https'
|
|
require 'uri'
|
|
|
|
class CdrSignalApi
|
|
def initialize(api_url, token)
|
|
@token = token
|
|
@last_update = 0
|
|
@api_url = api_url
|
|
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)
|
|
url = "#{@api_url}/bots/#{@token}/#{api}"
|
|
JSON.parse(Faraday.get(url, { Accept: "application/json" }).body)
|
|
end
|
|
|
|
def post(api, params = {})
|
|
url = "#{@api_url}/bots/#{@token}/#{api}"
|
|
JSON.parse(Faraday.post(url, params.to_json, { "Content-Type": "application/json", Accept: "application/json" }).body)
|
|
end
|
|
|
|
def fetch_self
|
|
get('')
|
|
end
|
|
|
|
def send_message(recipient, text, options = {})
|
|
# Don't encode conversationId with CGI
|
|
params = { to: recipient.to_s, message: text }
|
|
if options[:conversationId]
|
|
params[:conversationId] = options[:conversationId]
|
|
options.delete(:conversationId)
|
|
end
|
|
if options[:attachments]
|
|
params[:attachments] = options[:attachments]
|
|
options.delete(:attachments)
|
|
end
|
|
post('send', params.merge(parse_hash(options)))
|
|
end
|
|
end
|