Class: Kanal::Interfaces::Pachka::Helpers::PachkaApi

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/kanal/interfaces/pachka/helpers/pachka_api.rb

Overview

Class to work with Pachka public api

Defined Under Namespace

Classes: UploadedFile

Instance Method Summary collapse

Constructor Details

#initialize(access_token, verbose: false) ⇒ PachkaApi

Returns a new instance of PachkaApi.

Parameters:

  • access_token (String)

    access token for bot

  • verbose (Boolean) (defaults to: false)

    pass true to enable STDOUT logging of requests and responses



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/kanal/interfaces/pachka/helpers/pachka_api.rb', line 25

def initialize(access_token, verbose: false)
  @access_token = access_token

  @conn = Faraday.new(
    url: "https://api.pachca.com/api/shared/v1",
    headers: { "Authorization" => "Bearer #{@access_token}" }
  ) do |faraday|
    if verbose
      logger = ::Logger.new $stdout
      logger.level = ::Logger::DEBUG
      faraday.response :logger, logger, body: true, bodies: { request: true, response: true }
    end

    faraday.request :multipart
  end
end

Instance Method Details

#send_message(entity_id, entity_type, text, uploaded_files = []) ⇒ void

This method returns an undefined value.

Method sends text message via bot into the Pachka api with optionally sending files

Parameters:

  • entity_id (Integer)

    id of discussion obtained from input

  • entity_type (String)

    type of chat obtained from input

  • text (String)

    text to be sent

  • uploaded_files (Array) (defaults to: [])

    array of UploadedFile objects to send with message



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/kanal/interfaces/pachka/helpers/pachka_api.rb', line 53

def send_message(entity_id, entity_type, text, uploaded_files = [])
  payload = {
    message: {
      entity_type: entity_type,
      entity_id: entity_id,
      content: text
    }
  }

  unless uploaded_files.empty?
    files = []

    uploaded_files.each do |uploaded_file|
      files << {
        key: uploaded_file.file_key,
        name: uploaded_file.file_name,
        file_type: uploaded_file.file_type,
        size: uploaded_file.file_size
      }
    end

    payload[:message][:files] = files
  end

  @conn.post(
    "messages",
    payload.to_json,
    { "Content-Type" => "application/json" }
  )
rescue Exception => e
  logger.error "Cant send message to Pachka api! Error: #{e.full_message}"
  raise
end

#upload_file(filepath) ⇒ UploadedFile

Method uploads file at filepath to Pachka api and returns UploadedFile

Parameters:

  • filepath (String)

    local filepath to file

Returns:

  • (UploadedFile)

    object containing all needed info to send with message



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/kanal/interfaces/pachka/helpers/pachka_api.rb', line 94

def upload_file(filepath)
  # Obtaining all the needed fields for uploading a file
  res = @conn.post("uploads")

  raise "Problem with requesting info about uploads! More in api logs." unless res.success?

  first_response_body = JSON.parse(res.body)

  direct_url = first_response_body["direct_url"]

  file_key = first_response_body["key"]

  filename = File.basename filepath

  file_key = file_key.sub "${filename}", filename

  mime_type = MiniMime.lookup_by_filename(filename).content_type

  # filepath = File.absolute_path(filepath) if File.absolute_path(filepath) != filepath

  file = Faraday::Multipart::FilePart.new filepath, mime_type

  req_params = first_response_body

  req_params.delete "direct_url"

  req_params["key"] = file_key

  payload = req_params
  payload[:file] = file

  res = @conn.post(direct_url, payload)

  raise "Problem with uploading file to Pachka api! More in api logs." unless res.success?

  UploadedFile.new(
    file_key,
    filename,
    mime_type.include?("image") ? "image" : "file",
    File.size(filepath)
  )
rescue Exception => e
  logger.error "Error sending file to Pachka api! More info: #{e.full_message}"
end