Class: YunPianSMS::Sender

Inherits:
Object
  • Object
show all
Defined in:
lib/yunpian_sms/sender.rb

Constant Summary collapse

MAX_RECIPIENT_NUMBER =

云片网群发 api 最多支持同时发 1000 个号码, 超过 1000 个需要分批发

1000

Class Method Summary collapse

Class Method Details

.template_batch_send(template_id, mobile_nos, params) ⇒ Object



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
# File 'lib/yunpian_sms/sender.rb', line 16

def self.template_batch_send template_id, mobile_nos, params
  successful_count = 0
  successful_people = []
  failed_people = []
  total_fee = 0.0
  uri = URI("#{YunPianSMS.server}/v2/sms/tpl_batch_send.json")

  details = mobile_nos.each_slice(MAX_RECIPIENT_NUMBER).map do |sliced_mobile_nos|
    mobile = sliced_mobile_nos.join(',')
    result = send_internal(uri, template_id, mobile, params)
    successful_count += result.body['total_count']
    total_fee += result.body['total_fee'].to_f
    result.body['data'].each do |data|
      mobile = data['mobile']
      if data['code'] == 0
        successful_people << mobile
      else
        failed_people << mobile
      end
    end
    result
  end

  YunPianSMS::BatchResult.new(successful_count, successful_people, failed_people, total_fee, details)
end

.template_single_send(template_id, mobile, params) ⇒ Object



11
12
13
14
# File 'lib/yunpian_sms/sender.rb', line 11

def self.template_single_send template_id, mobile, params
  uri = URI("#{YunPianSMS.server}/v2/sms/tpl_single_send.json")
  send_internal uri, template_id, mobile, params
end