Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加 sendcloud 的短信发送 #27

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,18 @@ spec/reports
test/tmp
test/version_tmp
tmp

*.swo
*.swp

# Ignore IDE
.idea

# Ignore bundler config
.bundle

#mac os
.DS_Store
._.DS_Store

.project
11 changes: 11 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "china_sms"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

require 'irb'

IRB.start
1 change: 1 addition & 0 deletions china_sms.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.2"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "rspec-its"
spec.add_development_dependency "webmock"
end
96 changes: 96 additions & 0 deletions lib/china_sms/service/sendcloud.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# encoding: utf-8
module ChinaSMS
module Service
module Sendcloud
extend self

URL = 'http://www.sendcloud.net/smsapi/send'.freeze

MESSAGES = {
'500' => '发送失败, 手机空号',
'510' => '发送失败, 手机停机',
'550' => '发送失败, 该模板内容被投诉',
'580' => '发送失败, 手机关机',
'590' => '发送失败, 其他原因',
'200' => '请求成功',
'311' => '部分号码请求成功',
'312' => '全部请求失败',
'411' => '手机号不能为空',
'412' => '手机号格式错误',
'413' => '有重复的手机号',
'421' => '签名参数错误',
'422' => '签名错误',
'431' => '模板不存在',
'432' => '模板未提交审核或者未审核通过',
'433' => '模板ID不能为空',
'441' => '替换变量格式错误',
'451' => '定时发送时间的格式错误',
'452' => '定时发送时间早于服务器时间, 时间已过去',
'461' => '时间戳无效, 与服务器时间间隔大于6秒',
'471' => 'smsUser不存在',
'472' => 'smsUser不能为空',
'473' => '没有权限, 免费用户不能发送短信',
'474' => '用户不存在',
'481' => '手机号和替换变量不能为空',
'482' => '手机号和替换变量格式错误',
'483' => '替换变量长度不能超过32个字符',
'496' => '一分钟内给当前手机号发信太多',
'498' => '一天内给当前手机号发信太多',
'499' => '账户额度不够',
'501' => '服务器异常',
'601' => '你没有权限访问'
}

module MSG_TYPE
SMS = 0

MMS = 1
end

def get_signature(params)
param_str = []
password = params.delete 'password'
params.keys.sort.each do |key|
param_str << "#{key}=#{params[key]}"
end

sign_str = "#{password}&#{param_str.join('&')}&#{password}"

Digest::MD5.hexdigest sign_str
end

def to(phone, content = '', options = {})
options[:type] ||= MSG_TYPE::SMS

params = {
'phone' => phone,
'vars' => options[:content].to_json,
'msgType' => options[:type],
'smsUser' => options[:username],
'templateId' => options[:template_id],
'password' => options[:password]
}

params.merge! 'signature' => get_signature(params)

puts "Request params: #{params}"

res = Net::HTTP.post_form(URI.parse(URL), params)

puts "Response Body: #{res.body}"

result JSON.parse(res.body)
end

def result(response)
code = response['statusCode']

{
success: (code == 200),
code: code,
message: response['message']
}
end
end
end
end
38 changes: 38 additions & 0 deletions spec/service/sendcloud_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# encoding: utf-8
require 'spec_helper'

describe "Sendcloud" do
let(:username) { 'user' }
let(:password) { 'xxxxtemrejmijfeijixwewe' }

describe "#to" do
let(:url) { 'http://www.sendcloud.net/smsapi/send' }
let(:template_id) { 3126 }
let(:content) { {} }
subject { ChinaSMS::Service::Sendcloud.to phone, '', {content: content, template_id: template_id , username: username, password: password} }

describe 'single phone' do
let(:phone) { '13601647905' }

before do
body = {"phone"=> phone, "vars"=> content.to_json, "msgType"=> '0', "smsUser"=> username, "templateId"=>template_id, "signature"=>"16f795fd3514ed89bdd9fe577fb2601a" }
stub_request(:post, url).
with(body: URI.encode_www_form(body)).
to_return(body: "{\"info\":{\"successCount\":1,\"smsIds\":[\"1478579653941_49236_566_3126_clrsg5$13601647905\"]},\"statusCode\":200,\"message\":\"请求成功\",\"result\":true}")
end

its([:success]) { should eql true }
its([:code]) { should eql 200 }
its([:message]) { should eql "请求成功" }
end
end

describe "#get_signature" do
let(:phone) { "13601647905" }
let(:content) { {}.to_json }
let(:params) { {"phone"=> phone, "vars"=> content, "msgType"=>0, "smsUser"=> username, "templateId"=>3126, 'password'=> password} }
subject { ChinaSMS::Service::Sendcloud.get_signature params }

it { should eql "16f795fd3514ed89bdd9fe577fb2601a" }
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require 'webmock/rspec'
require 'china_sms'
require 'rspec/its'

#WebMock.allow_net_connect!
RSpec.configure do |config|
Expand Down