Commit 6f119a48abddc32e32af5d466bcf62794b3d51b7
1 parent
c91307a1
Exists in
master
添加自定义菜单API
Showing
4 changed files
with
96 additions
and
0 deletions
Show diff stats
README.md
... | ... | @@ -5,3 +5,17 @@ |
5 | 5 | ``` |
6 | 6 | gem "qy_wechat_api", git: "https://github.com/lanrion/qy_wechat_api.git" |
7 | 7 | ``` |
8 | + | |
9 | +**暂未对access_token做缓存处理,为了确保在开发过程不会出现token过期问题,请确保不要使用全局变量做client** | |
10 | + | |
11 | +## 基本用法 | |
12 | +```ruby | |
13 | +client = QyWechatApi::Client.new(QyWechatApi.corpid, QyWechatApi.corpsecret) | |
14 | + | |
15 | +# 创建自定义菜单 | |
16 | +# menu_json的生成方法请参考: https://github.com/lanrion/weixin_rails_middleware/wiki/DIY-menu | |
17 | +client.menu.create(menu_json, agent_id) | |
18 | + | |
19 | +``` | |
20 | + | |
21 | + | ... | ... |
lib/qy_wechat_api/api.rb
... | ... | @@ -0,0 +1,77 @@ |
1 | +# encoding: utf-8 | |
2 | +module QyWechatApi | |
3 | + module Api | |
4 | + class Menu < Base | |
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 | + def create(menu, agent_id) | |
57 | + menu = JSON.load(menu) if menu.is_a?(String) | |
58 | + http_post("create", menu, {agentid: agent_id}) | |
59 | + end | |
60 | + | |
61 | + def delete(agent_id) | |
62 | + http_get("delete", {agentid: agent_id}) | |
63 | + end | |
64 | + | |
65 | + def get(agent_id) | |
66 | + http_get("get", {agentid: agent_id}) | |
67 | + end | |
68 | + | |
69 | + private | |
70 | + | |
71 | + def base_url | |
72 | + "/menu" | |
73 | + end | |
74 | + | |
75 | + end | |
76 | + end | |
77 | +end | ... | ... |
lib/qy_wechat_api/client.rb