Commit 6012c92d by lra

阿里大鱼短信接口添加

parent 3d3d058f
# Sms # Gemfile
gem 'sms', git: 'ssh://gitlab@gitlab.ikcrm.com:40022/ikcrm_server/ikcrm_sms.git'
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ikcrm_sms`. To experiment with that code, run `bin/console` for an interactive prompt. rake sms:create_default_config_file
\ No newline at end of file
TODO: Delete this and the text above, and describe your gem
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'sms'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install sms
## Usage
TODO: Write usage instructions here
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
1. Fork it ( https://github.com/[my-github-username]/ikcrm_sms/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
require "bundler/gem_tasks" require "bundler/gem_tasks"
load "tasks/sms.rake"
\ No newline at end of file load "tasks/sms.rake"
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << 'lib' << 'test'
t.pattern = "test/*_test.rb"
t.ruby_opts << "-r test_helper"
end
task :default => :test
\ No newline at end of file
...@@ -2,11 +2,13 @@ ...@@ -2,11 +2,13 @@
require "optparse" require "optparse"
require "pp" require "pp"
require 'rack'
$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
require 'sms' require 'sms'
options = {source: 'zucp', method: 'single'} options = {source: 'zucp', method: 'single'}
attributes = {}
OptionParser.new do |o| OptionParser.new do |o|
o.banner = <<-EOF o.banner = <<-EOF
...@@ -36,25 +38,31 @@ EOF ...@@ -36,25 +38,31 @@ EOF
options[:method] = x options[:method] = x
end end
o.on("-h", "--help", "Show help documentation") do |h| o.on("-a", "--a=attributes", String, "attributes: ()") do |x|
attributes = Rack::Utils.parse_nested_query(x)
end
o.on("-h", "--help", "Show help documentation") do |x|
puts o puts o
exit exit
end end
o.on("-v", "--version", "Show Sms version") do |ver| o.on("-v", "--version", "Show Sms version") do |x|
puts "Version: #{Sms::VERSION}" puts "Version: #{Sms::VERSION}"
exit exit
end end
end.parse! end.parse!
options.merge!(attributes)
begin begin
puts options.inspect puts options.inspect
if options[:phone].present? && options[:content].present? puts SmsSender.new(options).send("#{options[:method]}Send", options[:phone], options[:content])
puts SmsSender.new({source: options[:source]}).send("#{options[:method]}Send", options[:phone], options[:content])
end
rescue => e rescue => e
puts 'Uncaught exception' puts 'Uncaught exception'
puts e.message puts e.message
puts e.backtrace.join("\n") puts e.backtrace.join("\n")
end end
# sms -p 13262902619 -s taobao -a 'app_secret=5aff003ac3c7243da65adef1e7c9c363&app_key=23272925&sms_free_sign_name=爱客验证码&sms_template_code=SMS_2575137&sms_param[code]=xxx&sms_param[product]=ddd'
...@@ -20,7 +20,7 @@ end ...@@ -20,7 +20,7 @@ end
def load_config def load_config
if defined? ::Rails if defined? ::Rails
config = HashWithIndifferentAccess.new(YAML.load_file("#{::Rails.root}/config/sms.yml")[::Rails.env] || {}) @sms_config ||= HashWithIndifferentAccess.new(YAML.load_file("#{::Rails.root}/config/sms.yml")[::Rails.env] || {})
else else
{} {}
end end
...@@ -33,7 +33,7 @@ class SmsSender ...@@ -33,7 +33,7 @@ class SmsSender
attr_accessor :source attr_accessor :source
def initialize(options = {}) def initialize(options = {})
options[:source] = redis.get('sms:source') || SOURCES.first if options[:model].blank? options[:source] = redis.get('sms:source') || SOURCES.first if options[:source].blank?
return unless options[:source].in?(SOURCES) return unless options[:source].in?(SOURCES)
if load_config.present? && load_config[options[:source]].present? if load_config.present? && load_config[options[:source]].present?
...@@ -44,16 +44,29 @@ class SmsSender ...@@ -44,16 +44,29 @@ class SmsSender
@source = "Sms/#{options[:source]}".camelize.constantize.new(options) @source = "Sms/#{options[:source]}".camelize.constantize.new(options)
end end
def singleSend(number, content) def content_must_exits?
case source
when 'zucp'
true
else
false
end
end
def singleSend(number, content = nil)
return unless @source return unless @source
@source.singleSend(number, content) if number.present? && content.present? return unless number.present?
call = content_must_exits? ? content.present? : true
@source.singleSend(number, content) if call
end end
def batchSend(numbers, content) def batchSend(numbers, content = nil)
return unless @source return unless @source
_numbers = numbers.is_a?(String) ? numbers.split(',') : (numbers.is_a?(Array) ? numbers : []) _numbers = numbers.is_a?(String) ? numbers.split(',') : (numbers.is_a?(Array) ? numbers : [])
_numbers = _numbers.compact.uniq _numbers = _numbers.compact.uniq
@source.batchSend(_numbers, content) if _numbers.present? && content.present? return unless _numbers.present?
call = content_must_exits? ? content.present? : true
@source.batchSend(_numbers, content) if call
end end
end end
......
...@@ -56,7 +56,4 @@ module Sms ...@@ -56,7 +56,4 @@ module Sms
end end
end end
end end
\ No newline at end of file
# options = {app_secret: '5aff003ac3c7243da65adef1e7c9c363', app_key: '23272925', sms_free_sign_name: '爱客验证码', sms_template_code: 'SMS_2575137', sms_param: {code: 'xxx', product: 'ddd'}}
# puts Sms::Taobao.new(options).singleSend('13262902619')
\ No newline at end of file
namespace :sms do namespace :sms do
task :create_default_config_file do task :create_default_config_file do
if File.exist?("#{::Rails.root}/config/sms.yml") unless File.exist?("#{::Rails.root}/config/sms.yml")
else
FileUtils.cp(File.expand_path("../../templates/sms.yml", __FILE__), "#{::Rails.root}/config/sms.yml") FileUtils.cp(File.expand_path("../../templates/sms.yml", __FILE__), "#{::Rails.root}/config/sms.yml")
end end
end end
......
...@@ -11,7 +11,7 @@ Gem::Specification.new do |spec| ...@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
spec.summary = %q{sms send.} spec.summary = %q{sms send.}
spec.description = %q{sms send.} spec.description = %q{sms send.}
spec.homepage = "" spec.homepage = "http://gitlab.ikcrm.com/ikcrm_server/ikcrm_sms"
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "bin" spec.bindir = "bin"
...@@ -26,4 +26,5 @@ Gem::Specification.new do |spec| ...@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "activesupport", "~> 4.1.12" spec.add_development_dependency "activesupport", "~> 4.1.12"
spec.add_development_dependency 'redis', '~> 3.2', '>= 3.2.1' spec.add_development_dependency 'redis', '~> 3.2', '>= 3.2.1'
spec.add_development_dependency 'rack'
end end
class TaobaoTest < Minitest::Test
describe 'Sms::Taobao test' do
before do
@options = {source: "taobao", app_secret: '5aff003ac3c7243da65adef1e7c9c363', app_key: '23272925', sms_free_sign_name: '爱客验证码', sms_template_code: 'SMS_2575137', sms_param: {code: 'xxx', product: 'ddd'}, phone: '13262902619'}
end
it 'SmsSender singleSend' do
obj = SmsSender.new(@options)
assert_instance_of Sms::Taobao, obj.source
data = obj.singleSend(@options[:phone])
puts data.inspect
assert data.has_key? 'alibaba_aliqin_fc_sms_num_send_response'
assert_instance_of Hash, data['alibaba_aliqin_fc_sms_num_send_response']
assert data['alibaba_aliqin_fc_sms_num_send_response'].has_key? 'result'
result = data['alibaba_aliqin_fc_sms_num_send_response']['result']
assert_instance_of Hash, result
assert result.has_key? 'success'
assert result['success'].eql?(true)
assert result.has_key? 'err_code'
assert result['err_code'].to_i.zero?
end
end
end
\ No newline at end of file
$:.unshift File.expand_path('../../lib', __FILE__) # $:.unshift File.expand_path('../../lib', __FILE__)
require 'minitest/autorun' require 'minitest/autorun'
require 'sms' require 'sms'
\ No newline at end of file
# require_relative "test_helper"
# $:.unshift File.expand_path('../../test', __FILE__)
require 'test_helper'
class ZucpTest < Minitest::Test class ZucpTest < Minitest::Test
describe 'Sms::Zucp test' do describe 'Sms::Zucp test' do
before do before do
@options = {source: "zucp", phone: "13262902619", content: Time.now.to_i.to_s, sn: "SDK-WSS-010-05925x", pwd: "123456x", suffix: '[test]'} @options = {source: "zucp", phone: "13262902619", content: Time.now.to_i.to_s, sn: "SDK-WSS-010-059256", pwd: "1234567", suffix: '[test]'}
end end
it 'SmsSender singleSend' do it 'SmsSender singleSend' do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment