yunpian_sms 0.1.1 → 0.1.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/README.md +33 -2
- data/lib/yunpian_sms.rb +3 -2
- data/lib/yunpian_sms/batch_result.rb +23 -0
- data/lib/yunpian_sms/sender.rb +68 -0
- data/lib/yunpian_sms/template.rb +1 -1
- data/lib/yunpian_sms/version.rb +1 -1
- data/spec/yunpian_sms_spec.rb +65 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 592ae1d97ddb7d46310c7a6230466fb11951642f
|
4
|
+
data.tar.gz: 383e2e594d8c8366d0fc4fadd3d40352e3cc849d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84ffe34b6aa1abb362b2c5b0e5ae9dc6b8c79a9f76f9dcb8c44c9ba98d0ce84ef0823fa5d8e68e027419d45e8c61806d62f914dccf2f1e9ded31156c402165ef
|
7
|
+
data.tar.gz: 271b2a6979c0c00a84b65a5d74dac323db8255756462552af1def012a4a3a8f62bb4caf6518ce177e05239a0064dfb5e228ea8d1e014755d5e6df8195f8b5eb5
|
data/README.md
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
YunPian SMS Ruby SDK
|
2
2
|
=========
|
3
3
|
|
4
|
-
[云片短信](https://www.yunpian.com/doc/zh_CN/introduction/brief.html) Ruby SDK,
|
4
|
+
[云片短信](https://www.yunpian.com/doc/zh_CN/introduction/brief.html) Ruby SDK, 非官方,使用 v2 api, 支持以下功能:
|
5
|
+
|
6
|
+
* 获取账号所属所有短信模板
|
7
|
+
* 模板发送单条短信
|
8
|
+
* 模板群发短信并获取发送成功/失败列表以及总费用
|
5
9
|
|
6
10
|
Table of Contents
|
7
11
|
-----------------
|
@@ -32,7 +36,7 @@ require 'yunpian_sms'
|
|
32
36
|
|
33
37
|
YunPianSMS.api_key = 'your api key'
|
34
38
|
|
35
|
-
#
|
39
|
+
# 获取短信模板
|
36
40
|
result = YunPianSMS::Template.all
|
37
41
|
if result.successful
|
38
42
|
result.body.each do |element|
|
@@ -42,6 +46,33 @@ if result.successful
|
|
42
46
|
else
|
43
47
|
# raise error
|
44
48
|
end
|
49
|
+
|
50
|
+
# 模板单条发送短信
|
51
|
+
YunPianSMS.api_key = api_key
|
52
|
+
template_id = 'xxxxxx'
|
53
|
+
mobile_no = configuration['single_mobile_no']
|
54
|
+
# 【智能通勤】车辆变更提醒: 尊敬的乘客您好, 您所乘坐的#keyword1#由于#keyword2#, 车牌号变更为#keyword3#, 变更日期范围为#keyword4#, 请提前到乘车站点候车以免耽误您的行程,谢谢。
|
55
|
+
params = {
|
56
|
+
keyword1: '横琴号十号线',
|
57
|
+
keyword2: '车辆定期维修',
|
58
|
+
keyword3: '粤C66666',
|
59
|
+
keyword4: '2019-05-20 至 2019-05-22'
|
60
|
+
}
|
61
|
+
result = YunPianSMS::Sender.template_single_send(template_id, mobile_no, params)
|
62
|
+
|
63
|
+
# 模板群发短信
|
64
|
+
YunPianSMS.api_key = api_key
|
65
|
+
template_id = 'xxxxxx'
|
66
|
+
mobile_nos = configuration['batch_mobile_nos']
|
67
|
+
# 【智能通勤】车辆变更提醒: 尊敬的乘客您好, 您所乘坐的#keyword1#由于#keyword2#, 车牌号变更为#keyword3#, 变更日期范围为#keyword4#, 请提前到乘车站点候车以免耽误您的行程,谢谢。
|
68
|
+
params = {
|
69
|
+
keyword1: '横琴号十号线',
|
70
|
+
keyword2: '车辆定期维修',
|
71
|
+
keyword3: '粤C66666',
|
72
|
+
keyword4: '2019-05-20 至 2019-05-22'
|
73
|
+
}
|
74
|
+
start_time = Time.now
|
75
|
+
batch_result = YunPianSMS::Sender.template_batch_send(template_id, mobile_nos, params)
|
45
76
|
~~~
|
46
77
|
|
47
78
|
|
data/lib/yunpian_sms.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module YunPianSMS
|
2
2
|
|
3
3
|
@debug_mode = true
|
4
|
-
@server = "https://sms.yunpian.com
|
4
|
+
@server = "https://sms.yunpian.com"
|
5
5
|
|
6
6
|
class<< self
|
7
7
|
attr_accessor :logger, :debug_mode, :api_key
|
@@ -9,5 +9,6 @@ module YunPianSMS
|
|
9
9
|
end
|
10
10
|
|
11
11
|
require 'yunpian_sms/template'
|
12
|
-
|
12
|
+
require 'yunpian_sms/sender'
|
13
|
+
|
13
14
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class YunPianSMS::BatchResult
|
2
|
+
|
3
|
+
attr_reader :successful_count # 总发送成功条数, 一条短信超过 70 字会被拆分成多条
|
4
|
+
attr_reader :successful_people # 发送成功列表
|
5
|
+
attr_reader :failed_people # 发送失败列表
|
6
|
+
attr_reader :total_fee # 总花费金额
|
7
|
+
attr_reader :details # 发送明细
|
8
|
+
|
9
|
+
def initialize(
|
10
|
+
successful_count,
|
11
|
+
successful_people,
|
12
|
+
failed_people,
|
13
|
+
total_fee,
|
14
|
+
details)
|
15
|
+
|
16
|
+
@successful_count = successful_count
|
17
|
+
@successful_people = successful_people
|
18
|
+
@failed_people = failed_people
|
19
|
+
@total_fee = total_fee
|
20
|
+
@details = details
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
require 'yunpian_sms/result'
|
4
|
+
require 'yunpian_sms/batch_result'
|
5
|
+
|
6
|
+
class YunPianSMS::Sender
|
7
|
+
|
8
|
+
# 云片网群发 api 最多支持同时发 1000 个号码, 超过 1000 个需要分批发
|
9
|
+
MAX_RECIPIENT_NUMBER = 1000
|
10
|
+
|
11
|
+
def self.template_single_send template_id, mobile, params
|
12
|
+
uri = URI("#{YunPianSMS.server}/v2/sms/tpl_single_send.json")
|
13
|
+
send_internal uri, template_id, mobile, params
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.template_batch_send template_id, mobile_nos, params
|
17
|
+
successful_count = 0
|
18
|
+
successful_people = []
|
19
|
+
failed_people = []
|
20
|
+
total_fee = 0.0
|
21
|
+
uri = URI("#{YunPianSMS.server}/v2/sms/tpl_batch_send.json")
|
22
|
+
|
23
|
+
details = mobile_nos.each_slice(MAX_RECIPIENT_NUMBER).map do |sliced_mobile_nos|
|
24
|
+
mobile = sliced_mobile_nos.join(',')
|
25
|
+
result = send_internal(uri, template_id, mobile, params)
|
26
|
+
successful_count += result.body['total_count']
|
27
|
+
total_fee += result.body['total_fee'].to_f
|
28
|
+
result.body['data'].each do |data|
|
29
|
+
mobile = data['mobile']
|
30
|
+
if data['code'] == 0
|
31
|
+
successful_people << mobile
|
32
|
+
else
|
33
|
+
failed_people << mobile
|
34
|
+
end
|
35
|
+
end
|
36
|
+
result
|
37
|
+
end
|
38
|
+
|
39
|
+
YunPianSMS::BatchResult.new(successful_count, successful_people, failed_people, total_fee, details)
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def self.send_internal uri, template_id, mobile, params
|
45
|
+
res = Net::HTTP.post_form(
|
46
|
+
uri,
|
47
|
+
apikey: YunPianSMS.api_key,
|
48
|
+
tpl_id: template_id,
|
49
|
+
mobile: mobile,
|
50
|
+
tpl_value: encode(params)
|
51
|
+
)
|
52
|
+
if YunPianSMS.debug_mode && YunPianSMS.logger
|
53
|
+
YunPianSMS.logger.debug "Sms sending response code #{res.code} body #{res.body}"
|
54
|
+
end
|
55
|
+
json = JSON.parse(res.body)
|
56
|
+
successful = res.kind_of?(Net::HTTPSuccess)
|
57
|
+
YunPianSMS::Result.new(successful, json.to_hash)
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.encode params
|
61
|
+
encoded = params.map {|k, v| "##{URI::encode(k.to_s)}#=#{URI::encode(v)}"}.join('&')
|
62
|
+
if YunPianSMS.debug_mode && YunPianSMS.logger
|
63
|
+
YunPianSMS.logger.debug "Encoded tpl value #{encoded}"
|
64
|
+
end
|
65
|
+
encoded
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
data/lib/yunpian_sms/template.rb
CHANGED
@@ -15,7 +15,7 @@ class YunPianSMS::Template
|
|
15
15
|
private
|
16
16
|
|
17
17
|
def self.get_templates(params)
|
18
|
-
uri = URI.parse("#{YunPianSMS.server}/tpl/get.json")
|
18
|
+
uri = URI.parse("#{YunPianSMS.server}/v2/tpl/get.json")
|
19
19
|
if YunPianSMS.debug_mode && YunPianSMS.logger
|
20
20
|
YunPianSMS.logger.debug "Requesting uri #{uri}..."
|
21
21
|
end
|
data/lib/yunpian_sms/version.rb
CHANGED
data/spec/yunpian_sms_spec.rb
CHANGED
@@ -7,7 +7,8 @@ require 'logger'
|
|
7
7
|
describe YunPianSMS do
|
8
8
|
|
9
9
|
subject(:logger) { Logger.new STDOUT }
|
10
|
-
subject(:
|
10
|
+
subject(:configuration) { YAML.load_file('./spec/fixtures/configuration.yml') }
|
11
|
+
subject(:api_key) { configuration['api_key'] }
|
11
12
|
|
12
13
|
before do
|
13
14
|
YunPianSMS.debug_mode = true
|
@@ -31,7 +32,69 @@ describe YunPianSMS do
|
|
31
32
|
template_id = element["tpl_id"]
|
32
33
|
template_result = YunPianSMS::Template.find template_id
|
33
34
|
expect(template_result.successful).to be true
|
34
|
-
logger.debug "template #{template_result.
|
35
|
+
logger.debug "template #{template_result.inspect}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'messages' do
|
42
|
+
|
43
|
+
it 'should send single message with template 1 successfully' do
|
44
|
+
YunPianSMS.api_key = api_key
|
45
|
+
template_id = '2697618'
|
46
|
+
mobile_no = configuration['single_mobile_no']
|
47
|
+
# 【智能通勤】车辆变更提醒: 尊敬的乘客您好, 您所乘坐的#keyword1#由于#keyword2#, 车牌号变更为#keyword3#, 变更日期范围为#keyword4#, 请提前到乘车站点候车以免耽误您的行程,谢谢。
|
48
|
+
params = {
|
49
|
+
keyword1: '横琴号十号线',
|
50
|
+
keyword2: '车辆定期维修',
|
51
|
+
keyword3: '粤C66666',
|
52
|
+
keyword4: '2019-05-20 至 2019-05-22'
|
53
|
+
}
|
54
|
+
result = YunPianSMS::Sender.template_single_send(template_id, mobile_no, params)
|
55
|
+
# expect(result.successful).to be true
|
56
|
+
logger.debug "result #{result.inspect}"
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should send single message with template 2 successfully' do
|
60
|
+
YunPianSMS.api_key = api_key
|
61
|
+
template_id = '2834228'
|
62
|
+
mobile_no = configuration['single_mobile_no']
|
63
|
+
# 【智能通勤】您有一张车票 #reserved_method_ticket_no# 距乘车时间小于#reserved_value# 分钟,线路: #reserved_method_bus_line_full_name#,乘车时间: #reserved_method_departure_at_with_day#, 起点: #reserved_method_source_station_name#, 终点: #reserved_method_destination_station_name#, 车牌号: #reserved_method_bus_plate_number#, 请提前到乘车站点候车以免耽误您的出行。
|
64
|
+
params = {
|
65
|
+
reserved_method_ticket_no: 'L251201905291617ZFBGVQ11',
|
66
|
+
reserved_value: '30',
|
67
|
+
reserved_method_bus_line_full_name: '横琴号十号线',
|
68
|
+
reserved_method_departure_at_with_day: '2019-05-20 18:00',
|
69
|
+
reserved_method_source_station_name: '横琴创意谷',
|
70
|
+
reserved_method_destination_station_name: '西江月',
|
71
|
+
reserved_method_bus_plate_number: '粤C66666'
|
72
|
+
}
|
73
|
+
result = YunPianSMS::Sender.template_single_send(template_id, mobile_no, params)
|
74
|
+
# expect(result.successful).to be true
|
75
|
+
logger.debug "result #{result.inspect}"
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should batch send messages successfully' do
|
79
|
+
YunPianSMS.api_key = api_key
|
80
|
+
template_id = '2697618'
|
81
|
+
mobile_nos = configuration['batch_mobile_nos']
|
82
|
+
# 【智能通勤】车辆变更提醒: 尊敬的乘客您好, 您所乘坐的#keyword1#由于#keyword2#, 车牌号变更为#keyword3#, 变更日期范围为#keyword4#, 请提前到乘车站点候车以免耽误您的行程,谢谢。
|
83
|
+
params = {
|
84
|
+
keyword1: '横琴号十号线',
|
85
|
+
keyword2: '车辆定期维修',
|
86
|
+
keyword3: '粤C66666',
|
87
|
+
keyword4: '2019-05-20 至 2019-05-22'
|
88
|
+
}
|
89
|
+
start_time = Time.now
|
90
|
+
batch_result = YunPianSMS::Sender.template_batch_send(template_id, mobile_nos, params)
|
91
|
+
elapsed_time = Time.now - start_time
|
92
|
+
logger.debug "elapsed_time #{elapsed_time.to_i}"
|
93
|
+
logger.debug batch_result.inspect
|
94
|
+
batch_result.details.each do |result|
|
95
|
+
result.body['data'].each do |data|
|
96
|
+
logger.debug "#{data['code']} #{data['msg']}"
|
97
|
+
end
|
35
98
|
end
|
36
99
|
end
|
37
100
|
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yunpian_sms
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- James Wang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
@@ -33,7 +33,9 @@ files:
|
|
33
33
|
- LICENSE.md
|
34
34
|
- README.md
|
35
35
|
- lib/yunpian_sms.rb
|
36
|
+
- lib/yunpian_sms/batch_result.rb
|
36
37
|
- lib/yunpian_sms/result.rb
|
38
|
+
- lib/yunpian_sms/sender.rb
|
37
39
|
- lib/yunpian_sms/template.rb
|
38
40
|
- lib/yunpian_sms/version.rb
|
39
41
|
- spec/yunpian_sms_spec.rb
|