Commit 214c4e275124053a133e306a180b80093328bafc

Authored by lanrion
1 parent 9d365b99
Exists in master

添加 多媒体资料管理API、发送消息API

lib/qy_wechat_api.rb
1 # encoding: utf-8 1 # encoding: utf-8
2 2
3 require "rest-client" 3 require "rest-client"
  4 +
  5 +require "carrierwave"
  6 +require "qy_wechat_api/carrierwave/qy_wechat_api_uploader"
  7 +
4 require 'yajl/json_gem' 8 require 'yajl/json_gem'
5 9
6 require "qy_wechat_api/client" 10 require "qy_wechat_api/client"
@@ -13,29 +17,15 @@ module QyWechatApi @@ -13,29 +17,15 @@ module QyWechatApi
13 OK_CODE = 0.freeze 17 OK_CODE = 0.freeze
14 18
15 class << self 19 class << self
16 - # for test  
17 - def corpid  
18 - "wxb9ce1d023fe6eb69"  
19 - end  
20 -  
21 - # for test  
22 - def corpsecret  
23 - "UOofFIah4PVLmkG8xMH3lpDxj6NTnQSKMrFt-HubiPB4kjB09EmTVcUjgNeermps"  
24 - end  
25 20
26 def http_get_without_token(url, params={}) 21 def http_get_without_token(url, params={})
27 get_api_url = ENDPOINT_URL + url 22 get_api_url = ENDPOINT_URL + url
28 - puts get_api_url  
29 - puts params  
30 load_json(RestClient.get(get_api_url, params: params)) 23 load_json(RestClient.get(get_api_url, params: params))
31 end 24 end
32 25
33 def http_post_without_token(url, payload={}, params={}) 26 def http_post_without_token(url, payload={}, params={})
34 post_api_url = ENDPOINT_URL + url 27 post_api_url = ENDPOINT_URL + url
35 - puts post_api_url  
36 - puts payload  
37 - puts params  
38 - payload = JSON.dump(payload) 28 + payload = JSON.dump(payload) if !payload[:media].is_a?(File)
39 load_json(RestClient.post(post_api_url, payload, params: params)) 29 load_json(RestClient.post(post_api_url, payload, params: params))
40 end 30 end
41 31
lib/qy_wechat_api/api/media.rb
@@ -4,12 +4,16 @@ module QyWechatApi @@ -4,12 +4,16 @@ module QyWechatApi
4 module Api 4 module Api
5 class Media < Base 5 class Media < Base
6 6
7 - def upload  
8 - 7 + # 媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file)
  8 + # media: 支持传路径或者文件实例
  9 + def upload(media, media_type)
  10 + file = process_file(media)
  11 + http_post("upload", {media: file}, {type: media_type})
9 end 12 end
10 13
  14 + # 返回一个URL,请开发者自行使用此url下载
11 def get_media_by_id(media_id) 15 def get_media_by_id(media_id)
12 - 16 + http_get("get", {media_id: media_id})
13 end 17 end
14 18
15 private 19 private
@@ -18,6 +22,62 @@ module QyWechatApi @@ -18,6 +22,62 @@ module QyWechatApi
18 "/media" 22 "/media"
19 end 23 end
20 24
  25 + def process_file(media)
  26 + return media if media.is_a?(File) && jpep?(media)
  27 +
  28 + media_url = media
  29 + uploader = QyWechatApiUploader.new
  30 +
  31 + if http?(media_url) # remote
  32 + uploader.download!(media_url.to_s)
  33 + else # local
  34 + media_file = media.is_a?(File) ? media : File.new(media_url)
  35 + uploader.cache!(media_file)
  36 + end
  37 + file = process_media(uploader)
  38 + CarrierWave.clean_cached_files! # clear last one day cache
  39 + file
  40 + end
  41 +
  42 + def process_media(uploader)
  43 + uploader = covert(uploader)
  44 + uploader.file.to_file
  45 + end
  46 +
  47 + # JUST ONLY FOR JPG IMAGE
  48 + def covert(uploader)
  49 + # image process
  50 + unless (uploader.file.content_type =~ /image/).nil?
  51 + if !jpep?(uploader.file)
  52 + require "mini_magick"
  53 + # covert to jpeg
  54 + image = MiniMagick::Image.open(uploader.path)
  55 + image.format("jpg")
  56 + uploader.cache!(File.open(image.path))
  57 + image.destroy! # remove /tmp from MinMagick generate
  58 + end
  59 + end
  60 + uploader
  61 + end
  62 +
  63 + def http?(uri)
  64 + return false if !uri.is_a?(String)
  65 + uri = URI.parse(uri)
  66 + uri.scheme =~ /^https?$/
  67 + end
  68 +
  69 + def jpep?(file)
  70 + content_type = if file.respond_to?(:content_type)
  71 + file.content_type
  72 + else
  73 + content_type(file.path)
  74 + end
  75 + !(content_type =~ /jpeg/).nil?
  76 + end
  77 +
  78 + def content_type(media_path)
  79 + MIME::Types.type_for(media_path).first.content_type
  80 + end
21 end 81 end
22 end 82 end
23 end 83 end
lib/qy_wechat_api/api/message.rb
@@ -5,37 +5,93 @@ module QyWechatApi @@ -5,37 +5,93 @@ module QyWechatApi
5 class Message < Base 5 class Message < Base
6 6
7 # 发送文本 7 # 发送文本
8 - def send_text 8 + def send_text(users, parties, tags, agent_id, content, safe=0)
  9 + params = common_params("text", agent_id, users, parties, tags, safe)
  10 + params.merge!({text: {content: content}})
  11 + http_post("send", params)
9 end 12 end
10 13
11 # 发送图片 14 # 发送图片
12 - def send_image  
13 - 15 + def send_image(users, parties, tags, agent_id, media_id, safe=0)
  16 + params = common_params("image", agent_id, users, parties, tags, safe)
  17 + params.merge!({image: {media_id: media_id}})
  18 + http_post("send", params)
14 end 19 end
15 20
16 # 发送语音 21 # 发送语音
17 - def send_voice  
18 - 22 + def send_voice(users, parties, tags, agent_id, media_id, safe=0)
  23 + params = common_params("voice", agent_id, users, parties, tags, safe)
  24 + params.merge!({voice: {media_id: media_id}})
  25 + http_post("send", params)
19 end 26 end
20 27
21 # 发送视频 28 # 发送视频
22 - def send_video  
23 - 29 + # media_options: {title: "title", description: "Description"}
  30 + def send_video(users, parties, tags, agent_id, media_id, media_options={}, safe=0)
  31 + params = common_params("video", agent_id, users, parties, tags, safe)
  32 + params.merge!({
  33 + video: {
  34 + media_id: media_id,
  35 + title: media_options["title"],
  36 + description: media_options["description"],
  37 + }
  38 + })
  39 + http_post("send", params)
24 end 40 end
25 41
26 # 文件信息 42 # 文件信息
27 - def send_file  
28 - 43 + def send_file(users, parties, tags, agent_id, media_id, safe=0)
  44 + params = common_params("file", agent_id, users, parties, tags, safe)
  45 + params.merge!({file: {media_id: media_id}})
  46 + http_post("send", params)
29 end 47 end
30 48
31 # news消息 49 # news消息
32 - def send_news  
33 - 50 + # "articles":[
  51 + # {
  52 + # "title": "Title",
  53 + # "description": "Description",
  54 + # "url": "URL",
  55 + # "picurl": "PIC_URL"
  56 + # },
  57 + # {
  58 + # "title": "Title",
  59 + # "description": "Description",
  60 + # "url": "URL",
  61 + # "picurl": "PIC_URL"
  62 + # }
  63 + # ]
  64 + def send_news(users, parties, tags, agent_id, articles, safe=0)
  65 + params = common_params("news", agent_id, users, parties, tags, safe)
  66 + params.merge!({news: {articles: articles}})
  67 + http_post("send", params)
34 end 68 end
35 69
36 # mpnews 70 # mpnews
37 - def send_mpnews  
38 - 71 + # articles":[
  72 + # {
  73 + # "title": "Title",
  74 + # "thumb_media_id": "id",
  75 + # "author": "Author",
  76 + # "content_source_url": "URL",
  77 + # "content": "Content",
  78 + # "digest": "Digest description",
  79 + # "show_cover_pic": "0"
  80 + # },
  81 + # {
  82 + # "title": "Title",
  83 + # "thumb_media_id": "id",
  84 + # "author": "Author",
  85 + # "content_source_url": "URL",
  86 + # "content": "Content",
  87 + # "digest": "Digest description",
  88 + # "show_cover_pic": "0"
  89 + # }
  90 + # ]
  91 + def send_mpnews(users, parties, tags, agent_id, articles, safe=0)
  92 + params = common_params("mpnews", agent_id, users, parties, tags, safe)
  93 + params.merge!({mpnews: {articles: articles}})
  94 + http_post("send", params)
39 end 95 end
40 96
41 private 97 private
@@ -44,6 +100,24 @@ module QyWechatApi @@ -44,6 +100,24 @@ module QyWechatApi
44 "/message" 100 "/message"
45 end 101 end
46 102
  103 + # 通用函数
  104 + def common_params(msg_type, agent_id, users=[], parties=[], tags=[], safe=0)
  105 + params = {
  106 + touser: join(users),
  107 + toparty: join(parties),
  108 + msgtype: msg_type,
  109 + agentid: agent_id,
  110 + totag: join(tags)
  111 + }
  112 + params.merge!({safe: safe}) if msg_type != "news"
  113 + params
  114 + end
  115 +
  116 + def join(array, split="|")
  117 + return array if array.is_a?(String)
  118 + array.join(split)
  119 + end
  120 +
47 end 121 end
48 end 122 end
49 end 123 end
lib/qy_wechat_api/client.rb
@@ -44,7 +44,7 @@ module QyWechatApi @@ -44,7 +44,7 @@ module QyWechatApi
44 44
45 private 45 private
46 def get_access_token 46 def get_access_token
47 - self.access_token ||= get_token.result[:access_token] 47 + self.access_token ||= get_token.result["access_token"]
48 end 48 end
49 49
50 # 获取token 50 # 获取token
qy_wechat_api.gemspec
@@ -24,6 +24,9 @@ Gem::Specification.new do |spec| @@ -24,6 +24,9 @@ Gem::Specification.new do |spec|
24 # https://github.com/brianmario/yajl-ruby 24 # https://github.com/brianmario/yajl-ruby
25 spec.add_dependency "yajl-ruby", "~> 1.2.0" 25 spec.add_dependency "yajl-ruby", "~> 1.2.0"
26 26
  27 + spec.add_dependency "carrierwave", "~> 0.10.0"
  28 + spec.add_dependency 'mini_magick', '~> 3.7.0'
  29 +
27 spec.add_development_dependency "bundler", "~> 1.6" 30 spec.add_development_dependency "bundler", "~> 1.6"
28 spec.add_development_dependency "rake" 31 spec.add_development_dependency "rake"
29 end 32 end