msg91-one-api 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/msg91/services/campaign_service.rb +149 -0
- data/lib/msg91.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 335a0947f6cfa51683cfce4f6ee0310a916a26d2fd815e57259e1e112c17e80b
|
4
|
+
data.tar.gz: 342bdda69d3ba0328873bd3249c7fe1c15ae25655fa63cb6b632760d88a3f2e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '049c0750cba709bf919c8775edb3c7a53130ee5b008faa76e5d55cbe83e3e057eb02942425bc0172652440f4770c9fa5e7d4d3667314c2a87e8ca4467daf68e6'
|
7
|
+
data.tar.gz: 96636cab49f461fa0e2f5de91151b7b12b02445ced7289fa11a0ccafe2e11d3bdcf304d0818358b76f4c1d1db3afa0eaac177831d60c876c2fa5cf24f68ab404
|
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'net/http' # Importing the net/http library for making HTTP requests
|
2
|
+
require 'uri' # Importing the uri library for parsing URIs
|
3
|
+
require 'json' # Importing the json library for JSON manipulation
|
4
|
+
|
5
|
+
module Msg91
|
6
|
+
module Services
|
7
|
+
class CampaignService
|
8
|
+
BASE_URL = 'https://control.msg91.com/api/v5/campaign/api/' # Base URL for Msg91 API
|
9
|
+
|
10
|
+
def initialize(auth_key)
|
11
|
+
@auth_key = auth_key # Initializing the Msg91 authentication key
|
12
|
+
end
|
13
|
+
|
14
|
+
# Method to run a campaign
|
15
|
+
def run_campaign(campaign_slug, input_data)
|
16
|
+
begin
|
17
|
+
verify_input_data(input_data) # Validate input data for the campaign
|
18
|
+
mapping_data = verify_and_get_campaign_mappings(campaign_slug) # Retrieve campaign mappings
|
19
|
+
send_campaign = { data: create_send_to_body(input_data, mapping_data) } # Prepare campaign payload
|
20
|
+
response = send_campaign_request(campaign_slug, send_campaign) # Send the campaign
|
21
|
+
rescue => e
|
22
|
+
raise "Exception: #{e.message}" # Handle exceptions
|
23
|
+
end
|
24
|
+
|
25
|
+
{
|
26
|
+
"message" => "Campaign Run Successfully",
|
27
|
+
"request_id" => response['request_id']
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
# Method to verify input data for the campaign
|
34
|
+
def verify_input_data(input_data)
|
35
|
+
raise "Must require a record to Run Campaign" if input_data.empty? # Raise error if input data is empty
|
36
|
+
raise "Record data limit exceeded: total limit 1000" if input_data['data'].size > 1000 # Raise error if input data exceeds limit
|
37
|
+
|
38
|
+
count = 0
|
39
|
+
input_data['data'].each do |data|
|
40
|
+
count += 1 if !data['to'].nil? && !data['to'].empty? # Count 'to' emails
|
41
|
+
count += 1 if !data['cc'].nil? && !data['cc'].empty? # Count 'cc' emails
|
42
|
+
count += 1 if !data['bcc'].nil? && !data['bcc'].empty? # Count 'bcc' emails
|
43
|
+
end
|
44
|
+
raise "Records data limit exceeded: total limit 1000 (including cc, bcc, and to). Current count: #{count}" if count > 1000 # Raise error if count exceeds 1000
|
45
|
+
end
|
46
|
+
|
47
|
+
# Method to verify and get campaign mappings
|
48
|
+
def verify_and_get_campaign_mappings(campaign_slug)
|
49
|
+
mapping_data = {}
|
50
|
+
operation = "campaigns/#{campaign_slug}/fields?source=launchCampaign" # API operation to fetch campaign mappings
|
51
|
+
campaign_mappings = make_api_call(operation) # Make API call to fetch mappings
|
52
|
+
|
53
|
+
raise "Invalid Campaign or no Node in Campaign" if campaign_mappings['mapping'].nil? # Raise error if no mappings found
|
54
|
+
|
55
|
+
mapping_data['mappings'] = campaign_mappings['mapping'].map { |mapping| mapping['name'] } # Extract mappings
|
56
|
+
mapping_data['variables'] = campaign_mappings['variables'] unless campaign_mappings['variables'].nil? # Extract variables
|
57
|
+
|
58
|
+
mapping_data # Return mapping data
|
59
|
+
end
|
60
|
+
|
61
|
+
# Method to create payload for sending the campaign
|
62
|
+
def create_send_to_body(input_data, mapping_data)
|
63
|
+
send_campaign = { 'sendTo' => [] } # Initialize payload
|
64
|
+
mappings = mapping_data['mappings'] # Get mappings
|
65
|
+
variables = mapping_data['variables'] # Get variables
|
66
|
+
|
67
|
+
# Iterate over input data
|
68
|
+
input_data['data'].each do |data|
|
69
|
+
temp = {} # Temporary hash for each data record
|
70
|
+
|
71
|
+
# Iterate over mappings
|
72
|
+
mappings.each do |map|
|
73
|
+
if data.key?(map)
|
74
|
+
case map
|
75
|
+
when 'to'
|
76
|
+
temp[map] = [{ 'email' => data[map] }] if valid_email?(data[map]) # Validate and set 'to' email
|
77
|
+
when 'mobiles'
|
78
|
+
temp['to'] ||= [{}]
|
79
|
+
temp['to'][0]['mobiles'] = data[map] if valid_mobile?(data[map]) # Validate and set 'mobiles'
|
80
|
+
when 'cc', 'bcc'
|
81
|
+
temp[map] = [{ 'email' => data[map] }] if valid_email?(data[map]) # Validate and set 'cc' or 'bcc'
|
82
|
+
when 'from_name'
|
83
|
+
temp[map] = data[map] # Set 'from_name'
|
84
|
+
when 'from_email'
|
85
|
+
temp[map] = data[map] if valid_email?(data[map]) # Set 'from_email'
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
if data.key?('to') && data.key?('name') && !data['name'].empty?
|
91
|
+
temp['to'][0]['name'] = data['name'] # Set 'name' if present
|
92
|
+
end
|
93
|
+
|
94
|
+
temp['variables'] = {} # Initialize variables
|
95
|
+
unless data['variables'].nil? # Check if variables present in data
|
96
|
+
variables.each do |var|
|
97
|
+
temp['variables'][var] = data['variables'][var] if data['variables'].key?(var) # Set variables
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
send_campaign['sendTo'] << temp # Add temporary data hash to send_to array
|
102
|
+
end
|
103
|
+
|
104
|
+
send_campaign['reply_to'] = input_data['reply_to'] if input_data.key?('reply_to') # Set 'reply_to'
|
105
|
+
send_campaign['attachments'] = input_data['attachments'] if input_data.key?('attachments') # Set 'attachments'
|
106
|
+
send_campaign # Return campaign payload
|
107
|
+
end
|
108
|
+
|
109
|
+
# Method to send the campaign
|
110
|
+
def send_campaign_request(campaign_slug, data)
|
111
|
+
operation = "campaigns/#{campaign_slug}/run" # API operation to run the campaign
|
112
|
+
make_api_call(operation, data, 'POST') # Make API call to run the campaign
|
113
|
+
end
|
114
|
+
|
115
|
+
# Method to make API call
|
116
|
+
def make_api_call(operation, input_data = {}, method = 'GET')
|
117
|
+
uri = URI(BASE_URL + operation) # Construct API URL
|
118
|
+
http = Net::HTTP.new(uri.host, uri.port) # Initialize HTTP connection
|
119
|
+
http.use_ssl = true # Use SSL for secure connection
|
120
|
+
|
121
|
+
request = case method
|
122
|
+
when 'POST'
|
123
|
+
req = Net::HTTP::Post.new(uri.path, { 'authkey' => @auth_key, 'Content-Type' => 'application/json' }) # Create POST request
|
124
|
+
req.body = input_data.to_json # Set request body
|
125
|
+
req # Return request
|
126
|
+
else
|
127
|
+
Net::HTTP::Get.new(uri.path, { 'authkey' => @auth_key }) # Create GET request
|
128
|
+
end
|
129
|
+
|
130
|
+
response = http.request(request) # Make HTTP request
|
131
|
+
raise "API call failed: #{response.body}" unless response.is_a?(Net::HTTPSuccess) # Raise error if API call failed
|
132
|
+
|
133
|
+
JSON.parse(response.body)['data'] # Parse and return response data
|
134
|
+
end
|
135
|
+
|
136
|
+
# Method to validate email format
|
137
|
+
def valid_email?(email)
|
138
|
+
/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i.match?(email) # Validate email format
|
139
|
+
end
|
140
|
+
|
141
|
+
# Method to validate mobile number format
|
142
|
+
def valid_mobile?(mobile)
|
143
|
+
/^\+?[0-9]{7,14}$/.match?(mobile) # Validate mobile number format
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
# Author: Prashant Patidar
|
data/lib/msg91.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: msg91-one-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Harsh Jaiswal
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-06-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: net-http
|
@@ -47,6 +47,7 @@ extensions: []
|
|
47
47
|
extra_rdoc_files: []
|
48
48
|
files:
|
49
49
|
- lib/msg91.rb
|
50
|
+
- lib/msg91/services/campaign_service.rb
|
50
51
|
- lib/msg91/services/otp_service.rb
|
51
52
|
homepage: https://github.com/Walkover-Web-Solution/msg91-ruby-plugins
|
52
53
|
licenses:
|