module SmartSMS::MessageService::ClassMethods

Class methods

Constants

DATETIME_FORMAT

Public Instance Methods

deliver(phone, content, options = {}) click to toggle source

发送短信到手机, 默认使用模板发送, 提供通用接口支持

  • phone: 需要接受短信的手机号码

  • content: 短信验证内容

Options:

  • method 如若要使用通用短信接口, 需要 :method => :general

  • tpl_id 选择发送短信的模板, 默认是2

# File lib/smart_sms/message_service.rb, line 21
def deliver(phone, content, options = {})
  if options[:method] == :general
    Request.post 'sms/send.json', mobile: phone, text: content, extend: options[:extend]
  else
    options[:code] = content
    message = parse_content options
    tpl_id = options[:tpl_id] || SmartSMS.config.template_id
    Request.post 'sms/tpl_send.json', tpl_id: tpl_id, mobile: phone, tpl_value: message
  end
end
find(options = {}) click to toggle source

参见 `find_messages` 方法

# File lib/smart_sms/message_service.rb, line 39
def find(options = {})
  find_messages 'sms/get.json', options
end
find_by_sid(sid) click to toggle source

根据sid来查询短信记录

# File lib/smart_sms/message_service.rb, line 34
def find_by_sid(sid)
  Request.post 'sms/get.json', sid: sid
end
get_black_word(text = '') click to toggle source

查询黑名单词语, 用于预先测试可能无法通过审核的模板

# File lib/smart_sms/message_service.rb, line 45
def get_black_word(text = '')
  Request.post 'sms/get_black_word.json', text: text
end
get_reply(options = {}) click to toggle source

查询用户回复的短信, 参见 `find_messages` 方法

# File lib/smart_sms/message_service.rb, line 50
def get_reply(options = {})
  find_messages 'sms/get_reply.json', options
end

Private Instance Methods

find_messages(api, options = {}) click to toggle source

批量查短信, 参数:

  • start_time: 短信提交开始时间

  • end_time: 短信提交结束时间

  • page_num: 页码,从1开始

  • page_size: 每页个数,最大100个

  • mobile: 接收短信的手机号

# File lib/smart_sms/message_service.rb, line 64
def find_messages(api, options = {})
  options[:end_time]   = Time.now if options[:end_time].blank?
  options[:start_time] = options[:end_time] - SmartSMS.config.default_interval if options[:start_time].blank?
  options[:end_time]   = parse_time(options[:end_time])
  options[:start_time] = parse_time(options[:start_time])
  options[:page_num]  ||= SmartSMS.config.page_num
  options[:page_size] ||= SmartSMS.config.page_size
  Request.post api, options
end
parse_content(options = {}) click to toggle source

生成信息发送内容

# File lib/smart_sms/message_service.rb, line 89
def parse_content(options = {})
  options[:code] ||= ''
  options[:company] ||= SmartSMS.config.company
  SmartSMS.config.template_value.map do |key|
    "##{key}#=#{options[key]}"
  end.join('&')
end
parse_time(time = '') click to toggle source

解析日期时间 格式可以是 `2014-05-01 08:40:20`, 也可以是Time类型

# File lib/smart_sms/message_service.rb, line 77
def parse_time(time = '')
  if time.present? && time.is_a?(Time)
    time.strftime DATETIME_FORMAT
  elsif time.is_a? String
    time
  else
    ''
  end
end