Commit de8ef9b8c09f3c7c3fbc81f785a54acc02f2db4a
1 parent
aea6f101
Exists in
master
添加企业消息接口
Showing
6 changed files
with
146 additions
and
5 deletions
Show diff stats
README.md
... | ... | @@ -292,6 +292,19 @@ class QyServicesController < ApplicationController |
292 | 292 | end |
293 | 293 | ``` |
294 | 294 | |
295 | +### 企业号消息接口 | |
296 | + | |
297 | +Wiki: http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E | |
298 | + | |
299 | +```ruby | |
300 | +group_client.chat.send_single_text(sender, user_id, msg) | |
301 | +group_client.chat.send_single_image(sender, user_id, media_id) | |
302 | +group_client.chat.send_single_file(sender, user_id, media_id) | |
303 | +group_client.chat.send_group_text(sender, chat_id, msg) | |
304 | +group_client.chat.send_group_image(sender, chat_id, media_id) | |
305 | +group_client.chat.send_group_file(sender, chat_id, media_id) | |
306 | +``` | |
307 | + | |
295 | 308 | ## 捐赠支持 |
296 | 309 | |
297 | 310 | 如果你觉得我的gem对你有帮助,欢迎打赏支持,:smile: | ... | ... |
lib/qy_wechat_api/api/base.rb
... | ... | @@ -0,0 +1,102 @@ |
1 | +# encoding: utf-8 | |
2 | +# 企业号消息接口说明 | |
3 | +module QyWechatApi | |
4 | + module Api | |
5 | + class Chat < Base | |
6 | + | |
7 | + # 创建会话 | |
8 | + def create(chat_id, name, owner, users) | |
9 | + option = { | |
10 | + chatid: chat_id, | |
11 | + name: name, | |
12 | + owner: owner, | |
13 | + userlist: Array[users] | |
14 | + } | |
15 | + http_post("create", option) | |
16 | + end | |
17 | + | |
18 | + # 获取会话 | |
19 | + def get(chat_id) | |
20 | + http_get("get", agentid: chat_id) | |
21 | + end | |
22 | + | |
23 | + # 修改会话信息 | |
24 | + # option: add_user_list, del_user_list, name, owner | |
25 | + def update(chat_id, operater, option={}) | |
26 | + http_post("update", {chatid: chat_id, op_user: operater}.merge!(option)) | |
27 | + end | |
28 | + | |
29 | + # 退出会话 | |
30 | + def quit(chat_id, operater) | |
31 | + http_post("quit", {chatid: chat_id, op_user: operater}) | |
32 | + end | |
33 | + | |
34 | + # 清除会话未读状态 | |
35 | + def clear_notify(operater, chat_type, chat_value) | |
36 | + http_post("clearnotify", { | |
37 | + op_user: operater, chat: {type: chat_type, id: chat_value} | |
38 | + }) | |
39 | + end | |
40 | + | |
41 | + # 发消息 | |
42 | + # 注意:如果receiver是单聊,发送对象为userid,否则为chatid | |
43 | + # 分别生成如下几个方法 | |
44 | + # :send_single_text, | |
45 | + # :send_single_image, | |
46 | + # :send_single_file, | |
47 | + # :send_group_text, | |
48 | + # :send_group_image, | |
49 | + # :send_group_file | |
50 | + RECEIVE_TYPES = ["single", "group"].freeze | |
51 | + MSG_TYPES = ["text", "image", "file"].freeze | |
52 | + RECEIVE_TYPES.each do |receive_type| | |
53 | + MSG_TYPES.each do |type| | |
54 | + define_method "send_#{receive_type}_#{type}" do |sender, receiver_id, msg| | |
55 | + http_post("send", { | |
56 | + receiver: { | |
57 | + type: receive_type, | |
58 | + id: receiver_id | |
59 | + }, | |
60 | + sender: sender, | |
61 | + msgtype: type, | |
62 | + type => msg_struct(type, msg) | |
63 | + }) | |
64 | + end | |
65 | + end | |
66 | + end | |
67 | + | |
68 | + # 设置成员新消息免打扰 | |
69 | + # [ | |
70 | + # { | |
71 | + # "userid": "zhangsan", | |
72 | + # "status": 0 | |
73 | + # }, | |
74 | + # { | |
75 | + # "userid": "lisi", | |
76 | + # "status": 1 | |
77 | + # } | |
78 | + # ] | |
79 | + def set_mute(mute_users) | |
80 | + http_post("setmute", {user_mute_list: mute_users}) | |
81 | + end | |
82 | + | |
83 | + private | |
84 | + | |
85 | + # e.g.: text, text_msg | |
86 | + def msg_struct(type, msg) | |
87 | + case type | |
88 | + when "text" | |
89 | + {content: msg} | |
90 | + else | |
91 | + # image, file都为发送media_id | |
92 | + {media_id: msg} | |
93 | + end | |
94 | + end | |
95 | + | |
96 | + def base_url | |
97 | + "/chat" | |
98 | + end | |
99 | + | |
100 | + end | |
101 | + end | |
102 | +end | ... | ... |
lib/qy_wechat_api/api/material.rb
lib/qy_wechat_api/client.rb
... | ... | @@ -6,12 +6,12 @@ module QyWechatApi |
6 | 6 | attr_accessor :access_token, :redis_key, :storage, :custom_access_token |
7 | 7 | |
8 | 8 | def initialize(corp_id, group_secret, options={}) |
9 | - redis_key = options[:redis_key] | |
10 | 9 | @custom_access_token = options[:access_token] |
11 | - @corp_id = corp_id | |
10 | + redis_key = options[:redis_key] | |
12 | 11 | @group_secret = group_secret |
13 | - @redis_key = security_redis_key((redis_key || "qy_#{group_secret}")) | |
14 | - @storage = Storage.init_with(self) | |
12 | + @corp_id = corp_id | |
13 | + @redis_key = security_redis_key((redis_key || "qy_#{group_secret}")) | |
14 | + @storage = Storage.init_with(self) | |
15 | 15 | end |
16 | 16 | |
17 | 17 | # return token |
... | ... | @@ -75,6 +75,10 @@ module QyWechatApi |
75 | 75 | Api::Agent.new(get_access_token) |
76 | 76 | end |
77 | 77 | |
78 | + def chat | |
79 | + Api::Chat.new(get_access_token) | |
80 | + end | |
81 | + | |
78 | 82 | private |
79 | 83 | |
80 | 84 | def security_redis_key(key) | ... | ... |
spec/spec_helper.rb
... | ... | @@ -40,6 +40,28 @@ end |
40 | 40 | |
41 | 41 | $client = QyWechatApi::Client.new(corpid, corpsecret) |
42 | 42 | |
43 | +module Rails | |
44 | + class Logger | |
45 | + def info(msg) | |
46 | + puts msg | |
47 | + end | |
48 | + end | |
49 | + | |
50 | + def self.logger | |
51 | + Logger.new | |
52 | + end | |
53 | + | |
54 | + class Cache | |
55 | + def fetch(key, options={}, &block) | |
56 | + yield block | |
57 | + end | |
58 | + end | |
59 | + | |
60 | + def self.cache | |
61 | + Cache.new | |
62 | + end | |
63 | +end | |
64 | + | |
43 | 65 | RSpec.configure do |config| |
44 | 66 | # rspec-expectations config goes here. You can use an alternate |
45 | 67 | # assertion/expectation library such as wrong or the stdlib/minitest | ... | ... |