Commit bf57d968e3e2a33dd83a14ba2e4571947a7cc7df
1 parent
214c4e27
Exists in
master
添加多媒体管理、自定义菜单、发送消息 rspec 测试
Showing
7 changed files
with
191 additions
and
50 deletions
Show diff stats
lib/qy_wechat_api/api/menu.rb
... | ... | @@ -3,56 +3,6 @@ module QyWechatApi |
3 | 3 | module Api |
4 | 4 | class Menu < Base |
5 | 5 | |
6 | - # for example: | |
7 | - MENU = '{ | |
8 | - "button": [ | |
9 | - { | |
10 | - "name": "扫码", | |
11 | - "sub_button": [ | |
12 | - { | |
13 | - "type": "scancode_waitmsg", | |
14 | - "name": "扫码带提示", | |
15 | - "key": "rselfmenu_0_0", | |
16 | - "sub_button": [ ] | |
17 | - }, | |
18 | - { | |
19 | - "type": "scancode_push", | |
20 | - "name": "扫码推事件", | |
21 | - "key": "rselfmenu_0_1", | |
22 | - "sub_button": [ ] | |
23 | - } | |
24 | - ] | |
25 | - }, | |
26 | - { | |
27 | - "name": "发图", | |
28 | - "sub_button": [ | |
29 | - { | |
30 | - "type": "pic_sysphoto", | |
31 | - "name": "系统拍照发图", | |
32 | - "key": "rselfmenu_1_0", | |
33 | - "sub_button": [ ] | |
34 | - }, | |
35 | - { | |
36 | - "type": "pic_photo_or_album", | |
37 | - "name": "拍照或者相册发图", | |
38 | - "key": "rselfmenu_1_1", | |
39 | - "sub_button": [ ] | |
40 | - }, | |
41 | - { | |
42 | - "type": "pic_weixin", | |
43 | - "name": "微信相册发图", | |
44 | - "key": "rselfmenu_1_2", | |
45 | - "sub_button": [ ] | |
46 | - } | |
47 | - ] | |
48 | - }, | |
49 | - { | |
50 | - "name": "发送位置", | |
51 | - "type": "location_select", | |
52 | - "key": "rselfmenu_2_0" | |
53 | - } | |
54 | - ] | |
55 | - }' | |
56 | 6 | def create(menu, agent_id) |
57 | 7 | menu = JSON.load(menu) if menu.is_a?(String) |
58 | 8 | http_post("create", menu, {agentid: agent_id}) | ... | ... |
... | ... | @@ -0,0 +1,72 @@ |
1 | +require "spec_helper" | |
2 | + | |
3 | +describe QyWechatApi::Api::Media do | |
4 | + | |
5 | + let(:image_jpg_path) do | |
6 | + "#{File.dirname(__FILE__)}/medias/ruby-logo.jpg" | |
7 | + end | |
8 | + | |
9 | + let(:image_ico_path) do | |
10 | + "#{File.dirname(__FILE__)}/medias/favicon.ico" | |
11 | + end | |
12 | + | |
13 | + let(:image_jpg_file) do | |
14 | + File.new(image_jpg_path) | |
15 | + end | |
16 | + | |
17 | + let(:image_ico_file) do | |
18 | + File.new(image_ico_path) | |
19 | + end | |
20 | + | |
21 | + let(:remote_png_path) do | |
22 | + "https://ruby-china-files.b0.upaiyun.com/user/big_avatar/273.jpg" | |
23 | + end | |
24 | + | |
25 | + let(:remote_jpg_path) do | |
26 | + "http://g.hiphotos.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=ce55457e4334970a537e187df4a3baad/03087bf40ad162d99455ef4d13dfa9ec8b13632762d0ed14.jpg" | |
27 | + end | |
28 | + | |
29 | + it "can upload a jpg File image" do | |
30 | + response = $client.media.upload(image_jpg_file, "image") | |
31 | + expect(response.code).to eq(QyWechatApi::OK_CODE) | |
32 | + expect(response.result.keys).to eq(["type", "media_id", "created_at"]) | |
33 | + end | |
34 | + | |
35 | + it "can upload a ico File image" do | |
36 | + response = $client.media.upload(image_ico_file, "image") | |
37 | + expect(response.code).to eq(QyWechatApi::OK_CODE) | |
38 | + expect(response.result.keys).to eq(["type", "media_id", "created_at"]) | |
39 | + end | |
40 | + | |
41 | + it "can upload a local image" do | |
42 | + response = $client.media.upload(image_jpg_path, "image") | |
43 | + expect(response.code).to eq(QyWechatApi::OK_CODE) | |
44 | + expect(response.result.keys).to eq(["type", "media_id", "created_at"]) | |
45 | + end | |
46 | + | |
47 | + it "can upload a local ico image" do | |
48 | + response = $client.media.upload(image_ico_path, "image") | |
49 | + expect(response.code).to eq(QyWechatApi::OK_CODE) | |
50 | + expect(response.result.keys).to eq(["type", "media_id", "created_at"]) | |
51 | + end | |
52 | + | |
53 | + it "can upload a remote png image" do | |
54 | + response = $client.media.upload(remote_png_path, "image") | |
55 | + expect(response.code).to eq(QyWechatApi::OK_CODE) | |
56 | + expect(response.result.keys).to eq(["type", "media_id", "created_at"]) | |
57 | + end | |
58 | + | |
59 | + it "can upload a remote jpg image" do | |
60 | + response = $client.media.upload(remote_jpg_path, "image") | |
61 | + expect(response.code).to eq(QyWechatApi::OK_CODE) | |
62 | + expect(response.result.keys).to eq(["type", "media_id", "created_at"]) | |
63 | + end | |
64 | + | |
65 | + # it "#download_media_url return a String url" do | |
66 | + # image = $client.media.upload(image_ico_path, "image") | |
67 | + # media_id = image.result["media_id"] | |
68 | + # image_url = $client.download_media_url(media_id) | |
69 | + # expect(image_url.class).to eq(String) | |
70 | + # end | |
71 | + | |
72 | +end | ... | ... |
No preview for this file type
7.24 KB
... | ... | @@ -0,0 +1,60 @@ |
1 | +require "spec_helper" | |
2 | + | |
3 | +describe QyWechatApi::Api::Menu do | |
4 | + let(:menu_string) do | |
5 | + '{ | |
6 | + "button": [ | |
7 | + { | |
8 | + "name": "扫码", | |
9 | + "sub_button": [ | |
10 | + { | |
11 | + "type": "scancode_waitmsg", | |
12 | + "name": "扫码带提示", | |
13 | + "key": "rselfmenu_0_0", | |
14 | + "sub_button": [ ] | |
15 | + }, | |
16 | + { | |
17 | + "type": "scancode_push", | |
18 | + "name": "扫码推事件", | |
19 | + "key": "rselfmenu_0_1", | |
20 | + "sub_button": [ ] | |
21 | + } | |
22 | + ] | |
23 | + }, | |
24 | + { | |
25 | + "name": "发图", | |
26 | + "sub_button": [ | |
27 | + { | |
28 | + "type": "pic_sysphoto", | |
29 | + "name": "系统拍照发图", | |
30 | + "key": "rselfmenu_1_0", | |
31 | + "sub_button": [ ] | |
32 | + }, | |
33 | + { | |
34 | + "type": "pic_photo_or_album", | |
35 | + "name": "拍照或者相册发图", | |
36 | + "key": "rselfmenu_1_1", | |
37 | + "sub_button": [ ] | |
38 | + }, | |
39 | + { | |
40 | + "type": "pic_weixin", | |
41 | + "name": "微信相册发图", | |
42 | + "key": "rselfmenu_1_2", | |
43 | + "sub_button": [ ] | |
44 | + } | |
45 | + ] | |
46 | + }, | |
47 | + { | |
48 | + "name": "发送位置", | |
49 | + "type": "location_select", | |
50 | + "key": "rselfmenu_2_0" | |
51 | + } | |
52 | + ] | |
53 | + }' | |
54 | + end | |
55 | + | |
56 | + it "#create" do | |
57 | + response = $client.menu.create(menu_string, 1) | |
58 | + expect(response.code).to eq(QyWechatApi::OK_CODE) | |
59 | + end | |
60 | +end | ... | ... |
... | ... | @@ -0,0 +1,50 @@ |
1 | +require "spec_helper" | |
2 | +describe QyWechatApi::Api::Message do | |
3 | + let(:text_message) do | |
4 | + "text message" | |
5 | + end | |
6 | + | |
7 | + let(:image_path) do | |
8 | + "#{File.dirname(__FILE__)}/medias/ruby-logo.jpg" | |
9 | + end | |
10 | + | |
11 | + let(:image_file) do | |
12 | + File.new(image_path) | |
13 | + end | |
14 | + | |
15 | + it "#send_text_message" do | |
16 | + response = $client.message.send_text("@all", "@all", "@all", 1, text_message) | |
17 | + expect(response.code).to eq(QyWechatApi::OK_CODE) | |
18 | + end | |
19 | + | |
20 | + it "#send_image_message" do | |
21 | + response = $client.media.upload(image_path, "image").result | |
22 | + response = $client.message.send_image("@all", "@all", "@all", 1, response["media_id"]) | |
23 | + expect(response.code).to eq(QyWechatApi::OK_CODE) | |
24 | + end | |
25 | + | |
26 | + | |
27 | + it "#send_news" do | |
28 | + articles = [{ | |
29 | + "title" => "Happy Day", | |
30 | + "description" => "Is Really A Happy Day", | |
31 | + "url" => "http://www.baidu.com", | |
32 | + "picurl" => "http://www.baidu.com/img/bdlogo.gif" | |
33 | + }, | |
34 | + { | |
35 | + "title" => "Happy Day", | |
36 | + "description" => "Is Really A Happy Day", | |
37 | + "url" => "http://www.baidu.com", | |
38 | + "picurl"=> "http://www.baidu.com/img/bdlogo.gif" | |
39 | + }] | |
40 | + response = $client.message.send_news("@all", "@all", "@all", 1, articles) | |
41 | + expect(response.code).to eq(QyWechatApi::OK_CODE) | |
42 | + end | |
43 | + | |
44 | + it "#send_file" do | |
45 | + response = $client.media.upload(image_path, "image").result | |
46 | + response = $client.message.send_file("@all", "@all", "@all", 1, response["media_id"]) | |
47 | + expect(response.code).to eq(QyWechatApi::OK_CODE) | |
48 | + end | |
49 | + | |
50 | +end | ... | ... |
spec/spec_helper.rb
... | ... | @@ -14,6 +14,15 @@ |
14 | 14 | # users commonly want. |
15 | 15 | # |
16 | 16 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration |
17 | +require "qy_wechat_api" | |
18 | +require "pry-rails" | |
19 | + | |
20 | +corpid = "wxb9ce1d023fe6eb69" | |
21 | +corpsecret = "UOofFIah4PVLmkG8xMH3lpDxj6NTnQSKMrFt-HubiPB4kjB09EmTVcUjgNeermps" | |
22 | + | |
23 | + | |
24 | +$client = QyWechatApi::Client.new(corpid, corpsecret) | |
25 | + | |
17 | 26 | RSpec.configure do |config| |
18 | 27 | # rspec-expectations config goes here. You can use an alternate |
19 | 28 | # assertion/expectation library such as wrong or the stdlib/minitest | ... | ... |