├── .gitignore ├── .rspec ├── .travis.yml ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── alidayu-ruby.gemspec ├── bin ├── console └── setup ├── lib ├── alidayu.rb └── alidayu │ └── version.rb └── spec ├── alidayu_spec.rb └── spec_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.2.2 4 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in alidayu-ruby.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 david 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Alidayu::Ruby 2 | 3 | 阿里大于发送短信ruby sdk 4 | 5 | ## Installation 6 | 7 | Add this line to your application's Gemfile: 8 | 9 | ```ruby 10 | gem 'alidayu-ruby', require: 'alidayu' 11 | ``` 12 | 13 | And then execute: 14 | 15 | $ bundle 16 | 17 | Or install it yourself as: 18 | 19 | $ gem install alidayu-ruby 20 | 21 | ## Usage 22 | 23 | ```ruby 24 | # 配置 appkey 和 appsecret 25 | # 如果使用rails 可以放到 config/initializers/alidayu.rb 26 | 27 | Alidayu.appkey = "" 28 | Alidayu.appsecret = "" 29 | 30 | # 发短信 31 | # 必须参数 32 | # template_id 模版ID String 33 | # params 模版参数 Hash 34 | # phones 电话号码 String or String[] 35 | # sign_name 签名 36 | 37 | # 具体文档 http://open.taobao.com/doc2/apiDetail?apiId=25450 38 | 39 | Alidayu.send_sms({ 40 | template_id: "SMS_0000000", 41 | sign_name: "身份验证", 42 | params: { 43 | code: '', 44 | product: '', 45 | }, 46 | phones: "填写电话号码" 47 | }) 48 | 49 | # 发语音 50 | # 必须参数 51 | # template_id 模版ID String 52 | # params 模版参数 Hash 53 | # phone 电话号码 String 54 | # show_phone 显示的电话号码 String 55 | 56 | # 具体文档 https://api.alidayu.com/doc2/apiDetail.htm?spm=a3142.8062830.3.3.ZAq95Z&apiId=25444 57 | 58 | Alidayu.send_voice({ 59 | template_id: "SMS_0000000", 60 | params: { 61 | code: '', 62 | name: '' 63 | }, 64 | phone: "填写电话号码", 65 | show_phone: "填写显示的电话号码" 66 | }) 67 | 68 | ``` 69 | 70 | ## Development 71 | 72 | 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. 73 | 74 | 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). 75 | 76 | ## Contributing 77 | 78 | 1. Fork it ( https://github.com/davidqhr/alidayu-ruby/fork ) 79 | 2. Create your feature branch (`git checkout -b my-new-feature`) 80 | 3. Commit your changes (`git commit -am 'Add some feature'`) 81 | 4. Push to the branch (`git push origin my-new-feature`) 82 | 5. Create a new Pull Request 83 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | 3 | require 'rspec/core/rake_task' 4 | RSpec::Core::RakeTask.new(:spec) 5 | -------------------------------------------------------------------------------- /alidayu-ruby.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'alidayu/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "alidayu-ruby" 8 | spec.version = Alidayu::VERSION 9 | spec.authors = ["david"] 10 | spec.email = ["davidqhr@gmail.com"] 11 | 12 | spec.summary = %q{阿里大鱼发送短信ruby sdk} 13 | spec.description = %q{阿里大鱼发送短信ruby sdk} 14 | spec.homepage = "https://github.com/davidqhr/alidayu-ruby" 15 | spec.license = "MIT" 16 | 17 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 18 | spec.bindir = "exe" 19 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 20 | spec.require_paths = ["lib"] 21 | 22 | spec.add_development_dependency "bundler", "~> 1.9" 23 | spec.add_development_dependency "rake", "~> 10.0" 24 | spec.add_development_dependency 'rspec' 25 | end 26 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "alidayu" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | IRB.start 15 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | 5 | bundle install 6 | 7 | # Do any other automated setup that you need to do here 8 | -------------------------------------------------------------------------------- /lib/alidayu.rb: -------------------------------------------------------------------------------- 1 | require "alidayu/version" 2 | require 'json' 3 | require 'net/https' 4 | require 'uri' 5 | 6 | module Alidayu 7 | class << self 8 | attr_accessor :appkey, :appsecret 9 | 10 | # 必须参数 11 | # template_id 模版ID String 12 | # params 模版参数 Hash 13 | # phones 电话号码 String or String[] 14 | # sign_name 签名 15 | def send_sms params 16 | params = build_params(params) 17 | request(uri, params) 18 | end 19 | 20 | # 必须参数 21 | # template_id 模版ID String 22 | # params 模版参数 Hash 23 | # phone 电话号码 String 24 | # show_phone 显示的电话号码 String 25 | def send_voice params 26 | params = build_voice_params(params) 27 | request(uri, params) 28 | end 29 | 30 | def uri 31 | @uri ||= URI.parse('https://eco.taobao.com/router/rest') 32 | end 33 | 34 | private 35 | 36 | def request(uri, params) 37 | http = Net::HTTP.new(uri.host, uri.port) 38 | http.use_ssl = true 39 | http.verify_mode = OpenSSL::SSL::VERIFY_NONE 40 | 41 | request = Net::HTTP::Post.new(uri.path) 42 | request.add_field('Content-Type', 'application/json') 43 | request.set_form_data(params) 44 | response = http.request(request) 45 | res_body = JSON.parse response.body 46 | res_body 47 | end 48 | 49 | def build_params params 50 | alidayu_params = { 51 | :method => 'alibaba.aliqin.fc.sms.num.send', 52 | :app_key => appkey, 53 | :timestamp => Time.new.strftime("%Y-%m-%d %T"), 54 | :format => 'json', 55 | :v => '2.0', 56 | :sign_method => 'md5', 57 | :sms_type => 'normal', 58 | } 59 | 60 | # 电话号码 61 | if params[:phones].is_a? String 62 | alidayu_params[:rec_num] = params[:phones] 63 | elsif params[:phones].is_a? Array 64 | alidayu_params[:rec_num] = params[:phones].join(',') 65 | else 66 | raise 'phones格式错误' 67 | end 68 | 69 | # 模版ID 70 | alidayu_params[:sms_template_code] = params[:template_id] 71 | 72 | # 模版参数 73 | alidayu_params[:sms_param] = JSON(params[:params]) 74 | 75 | # 签名 76 | alidayu_params[:sms_free_sign_name] = params[:sign_name] || sign_name 77 | 78 | sign = sign alidayu_params 79 | alidayu_params.merge!({:sign => sign}) 80 | alidayu_params 81 | end 82 | 83 | def build_voice_params params 84 | alidayu_params = { 85 | :method => 'alibaba.aliqin.fc.tts.num.singlecall', 86 | :app_key => appkey, 87 | :timestamp => Time.new.strftime("%Y-%m-%d %T"), 88 | :format => 'json', 89 | :v => '2.0', 90 | :sign_method => 'md5', 91 | :sms_type => 'normal', 92 | } 93 | 94 | # 电话号码 95 | alidayu_params[:called_num] = params[:phone] 96 | 97 | # 模版ID 98 | alidayu_params[:tts_code] = params[:template_id] 99 | 100 | # 模版参数 101 | alidayu_params[:tts_param] = JSON(params[:params]) 102 | 103 | # 显示号码 104 | alidayu_params[:called_show_num] = params[:show_phone] 105 | 106 | sign = sign alidayu_params 107 | alidayu_params.merge!({:sign => sign}) 108 | alidayu_params 109 | end 110 | 111 | def sign params 112 | joind_string = params.sort_by{ |k,v| k}.to_h.map { |k, v| "#{k}#{v}" }.join('') 113 | Digest::MD5.hexdigest("#{appsecret}#{joind_string}#{appsecret}").upcase 114 | end 115 | end 116 | end -------------------------------------------------------------------------------- /lib/alidayu/version.rb: -------------------------------------------------------------------------------- 1 | module Alidayu 2 | VERSION = "0.1.2" 3 | end 4 | -------------------------------------------------------------------------------- /spec/alidayu_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Alidayu do 4 | before do 5 | Alidayu.appkey = "" 6 | Alidayu.appsecret = "" 7 | end 8 | 9 | it 'can send message' do 10 | Alidayu.send_sms({ 11 | template_id: "SMS_0000000", 12 | sign_name: "身份验证", 13 | params: { 14 | code: '', 15 | product: '', 16 | }, 17 | phones: "填写电话号码" 18 | }) 19 | end 20 | 21 | it 'can send voice' do 22 | Alidayu.send_voice({ 23 | template_id: "SMS_0000000", 24 | params: { 25 | code: '', 26 | name: '' 27 | }, 28 | phone: "填写电话号码", 29 | show_phone: "填写显示的电话号码" 30 | }) 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) 2 | require 'alidayu' 3 | --------------------------------------------------------------------------------