-
Notifications
You must be signed in to change notification settings - Fork 33
/
medias.rb
110 lines (94 loc) · 3.66 KB
/
medias.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# typed: strict
# frozen_string_literal: true
require "faraday"
require "faraday/multipart"
require_relative "request"
require_relative "response"
require_relative '../../../lib/whatsapp_sdk/api/responses/media_data_response'
require_relative '../../../lib/whatsapp_sdk/api/responses/success_response'
module WhatsappSdk
module Api
class Medias < Request
class FileNotFoundError < StandardError
extend T::Sig
sig { returns(String) }
attr_reader :file_path
sig { params(file_path: String).void }
def initialize(file_path)
@file_path = file_path
super("Couldn't find file_path: #{file_path}")
end
end
# Get Media by ID.
#
# @param media_id [String] Media Id.
# @return [WhatsappSdk::Api::Response] Response object.
sig { params(media_id: String).returns(WhatsappSdk::Api::Response) }
def media(media_id:)
response = send_request(
http_method: "get",
endpoint: "/#{media_id}"
)
WhatsappSdk::Api::Response.new(
response: response,
data_class_type: WhatsappSdk::Api::Responses::MediaDataResponse
)
end
# Download Media by URL.
#
# @param url URL.
# @param file_path [String] The file_path to download the media e.g. "tmp/downloaded_image.png".
# @return [WhatsappSdk::Api::Response] Response object.
sig { params(url: String, file_path: String).returns(WhatsappSdk::Api::Response) }
def download(url:, file_path:)
response = download_file(url, file_path)
response = if response.code.to_i == 200
{ "success" => true }
else
{ "error" => true, "status" => response.code }
end
WhatsappSdk::Api::Response.new(
response: response,
data_class_type: WhatsappSdk::Api::Responses::SuccessResponse,
error_class_type: WhatsappSdk::Api::Responses::ErrorResponse
)
end
# Upload a media.
# @param sender_id [Integer] Sender' phone number.
# @param file_path [String] Path to the file stored in your local directory. For example: "tmp/whatsapp.png".
# @param type [String] Media type e.g. text/plain, video/3gp, image/jpeg, image/png. For more information,
# see the official documentation https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#supported-media-types.
#
# @return [WhatsappSdk::Api::Response] Response object.
sig { params(sender_id: Integer, file_path: String, type: String).returns(WhatsappSdk::Api::Response) }
def upload(sender_id:, file_path:, type:)
raise FileNotFoundError, file_path unless File.file?(file_path)
params = {
messaging_product: "whatsapp",
file: Faraday::FilePart.new(file_path, type),
type: type
}
response = send_request(http_method: "post", endpoint: "#{sender_id}/media", params: params)
WhatsappSdk::Api::Response.new(
response: response,
data_class_type: WhatsappSdk::Api::Responses::MediaDataResponse
)
end
# Delete a Media by ID.
#
# @param media_id [String] Media Id.
# @return [WhatsappSdk::Api::Response] Response object.
sig { params(media_id: String).returns(WhatsappSdk::Api::Response) }
def delete(media_id:)
response = send_request(
http_method: "delete",
endpoint: "/#{media_id}"
)
WhatsappSdk::Api::Response.new(
response: response,
data_class_type: WhatsappSdk::Api::Responses::SuccessResponse
)
end
end
end
end