Class: Wechat::CorpApi

Inherits:
ApiBase show all
Defined in:
lib/wechat/corp_api.rb

Constant Summary collapse

API_BASE =
'https://qyapi.weixin.qq.com/cgi-bin/'.freeze

Constants inherited from ApiBase

ApiBase::MP_BASE

Instance Attribute Summary collapse

Attributes inherited from ApiBase

#access_token, #client, #jsapi_ticket

Instance Method Summary collapse

Methods inherited from ApiBase

#callbackip, #media, #media_create, #media_uploadimg, #qrcode

Constructor Details

#initialize(appid, secret, token_file, agentid, timeout, skip_verify_ssl, jsapi_ticket_file) ⇒ CorpApi

Returns a new instance of CorpApi.



13
14
15
16
17
18
# File 'lib/wechat/corp_api.rb', line 13

def initialize(appid, secret, token_file, agentid, timeout, skip_verify_ssl, jsapi_ticket_file)
  @client = HttpClient.new(API_BASE, timeout, skip_verify_ssl)
  @access_token = Token::CorpAccessToken.new(@client, appid, secret, token_file)
  @agentid = agentid
  @jsapi_ticket = Ticket::CorpJsapiTicket.new(@client, @access_token, jsapi_ticket_file)
end

Instance Attribute Details

#agentidObject (readonly)

Returns the value of attribute agentid.



9
10
11
# File 'lib/wechat/corp_api.rb', line 9

def agentid
  @agentid
end

Instance Method Details

#agent(agentid) ⇒ Object



24
25
26
# File 'lib/wechat/corp_api.rb', line 24

def agent(agentid)
  get 'agent/get', params: { agentid: agentid }
end

#agent_listObject



20
21
22
# File 'lib/wechat/corp_api.rb', line 20

def agent_list
  get 'agent/list'
end

#batch_job_result(jobid) ⇒ Object



63
64
65
# File 'lib/wechat/corp_api.rb', line 63

def batch_job_result(jobid)
  get 'batch/getresult', params: { jobid: jobid }
end

#batch_replaceparty(media_id) ⇒ Object



67
68
69
# File 'lib/wechat/corp_api.rb', line 67

def batch_replaceparty(media_id)
  post 'batch/replaceparty', JSON.generate(media_id: media_id)
end

#batch_replaceuser(media_id) ⇒ Object



75
76
77
# File 'lib/wechat/corp_api.rb', line 75

def batch_replaceuser(media_id)
  post 'batch/replaceuser', JSON.generate(media_id: media_id)
end

#batch_syncuser(media_id) ⇒ Object



71
72
73
# File 'lib/wechat/corp_api.rb', line 71

def batch_syncuser(media_id)
  post 'batch/syncuser', JSON.generate(media_id: media_id)
end

#convert_to_openid(userid) ⇒ Object



43
44
45
# File 'lib/wechat/corp_api.rb', line 43

def convert_to_openid(userid)
  post 'user/convert_to_openid', JSON.generate(userid: userid, agentid: agentid)
end

#custom_message_send(message) ⇒ Object



168
169
170
# File 'lib/wechat/corp_api.rb', line 168

def custom_message_send(message)
  post 'message/send', message.agent_id(agentid).to_json, content_type: :json
end

#department(departmentid = 1) ⇒ Object



91
92
93
# File 'lib/wechat/corp_api.rb', line 91

def department(departmentid = 1)
  get 'department/list', params: { id: departmentid }
end

#department_create(name, parentid) ⇒ Object



79
80
81
# File 'lib/wechat/corp_api.rb', line 79

def department_create(name, parentid)
  post 'department/create', JSON.generate(name: name, parentid: parentid)
end

#department_delete(departmentid) ⇒ Object



83
84
85
# File 'lib/wechat/corp_api.rb', line 83

def department_delete(departmentid)
  get 'department/delete', params: { id: departmentid }
end

#department_update(departmentid, name = nil, parentid = nil, order = nil) ⇒ Object



87
88
89
# File 'lib/wechat/corp_api.rb', line 87

def department_update(departmentid, name = nil, parentid = nil, order = nil)
  post 'department/update', JSON.generate({ id: departmentid, name: name, parentid: parentid, order: order }.reject { |_k, v| v.nil? })
end

#getuserinfo(code) ⇒ Object



32
33
34
# File 'lib/wechat/corp_api.rb', line 32

def getuserinfo(code)
  get 'user/getuserinfo', params: { code: code }
end

#invite_user(userid) ⇒ Object



47
48
49
# File 'lib/wechat/corp_api.rb', line 47

def invite_user(userid)
  post 'invite/send', JSON.generate(userid: userid)
end

#material(media_id) ⇒ Object



152
153
154
# File 'lib/wechat/corp_api.rb', line 152

def material(media_id)
  get 'material/get', params: { media_id: media_id, agentid: agentid }, as: :file
end

#material_add(type, file) ⇒ Object



156
157
158
# File 'lib/wechat/corp_api.rb', line 156

def material_add(type, file)
  post_file 'material/add_material', file, params: { type: type, agentid: agentid }
end

#material_countObject



144
145
146
# File 'lib/wechat/corp_api.rb', line 144

def material_count
  get 'material/get_count', params: { agentid: agentid }
end

#material_delete(media_id) ⇒ Object



160
161
162
# File 'lib/wechat/corp_api.rb', line 160

def material_delete(media_id)
  get 'material/del', params: { media_id: media_id, agentid: agentid }
end

#material_list(type, offset, count) ⇒ Object



148
149
150
# File 'lib/wechat/corp_api.rb', line 148

def material_list(type, offset, count)
  post 'material/batchget', JSON.generate(type: type, agentid: agentid, offset: offset, count: count)
end


131
132
133
# File 'lib/wechat/corp_api.rb', line 131

def menu
  get 'menu/get', params: { agentid: agentid }
end


139
140
141
142
# File 'lib/wechat/corp_api.rb', line 139

def menu_create(menu)
  # 微信不接受7bit escaped json(eg \uxxxx), 中文必须UTF-8编码, 这可能是个安全漏洞
  post 'menu/create', JSON.generate(menu), params: { agentid: agentid }
end


135
136
137
# File 'lib/wechat/corp_api.rb', line 135

def menu_delete
  get 'menu/delete', params: { agentid: agentid }
end

#message_send(userid, message) ⇒ Object



164
165
166
# File 'lib/wechat/corp_api.rb', line 164

def message_send(userid, message)
  post 'message/send', Message.to(userid).text(message).agent_id(agentid).to_json, content_type: :json
end

#oauth2_url(redirect_uri, appid) ⇒ Object



36
37
38
39
40
41
# File 'lib/wechat/corp_api.rb', line 36

def oauth2_url(redirect_uri, appid)
  ActiveSupport::Deprecation.warn('oauth2_url is deprecated. use wechat_oauth2 instead.')

  redirect_uri = CGI.escape(redirect_uri)
  "https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{appid}&redirect_uri=#{redirect_uri}&response_type=code&scope=snsapi_base#wechat_redirect"
end

#tag(tagid) ⇒ Object



119
120
121
# File 'lib/wechat/corp_api.rb', line 119

def tag(tagid)
  get 'tag/get', params: { tagid: tagid }
end

#tag_add_user(tagid, userids = nil, departmentids = nil) ⇒ Object



123
124
125
# File 'lib/wechat/corp_api.rb', line 123

def tag_add_user(tagid, userids = nil, departmentids = nil)
  post 'tag/addtagusers', JSON.generate(tagid: tagid, userlist: userids, partylist: departmentids)
end

#tag_create(tagname, tagid = nil) ⇒ Object



103
104
105
# File 'lib/wechat/corp_api.rb', line 103

def tag_create(tagname, tagid = nil)
  post 'tag/create', JSON.generate(tagname: tagname, tagid: tagid)
end

#tag_del_user(tagid, userids = nil, departmentids = nil) ⇒ Object



127
128
129
# File 'lib/wechat/corp_api.rb', line 127

def tag_del_user(tagid, userids = nil, departmentids = nil)
  post 'tag/deltagusers', JSON.generate(tagid: tagid, userlist: userids, partylist: departmentids)
end

#tag_delete(tagid) ⇒ Object



111
112
113
# File 'lib/wechat/corp_api.rb', line 111

def tag_delete(tagid)
  get 'tag/delete', params: { tagid: tagid }
end

#tag_update(tagid, tagname) ⇒ Object



107
108
109
# File 'lib/wechat/corp_api.rb', line 107

def tag_update(tagid, tagname)
  post 'tag/update', JSON.generate(tagid: tagid, tagname: tagname)
end

#tagsObject



115
116
117
# File 'lib/wechat/corp_api.rb', line 115

def tags
  get 'tag/list'
end

#user(userid) ⇒ Object



28
29
30
# File 'lib/wechat/corp_api.rb', line 28

def user(userid)
  get 'user/get', params: { userid: userid }
end

#user_auth_success(userid) ⇒ Object



51
52
53
# File 'lib/wechat/corp_api.rb', line 51

def user_auth_success(userid)
  get 'user/authsucc', params: { userid: userid }
end

#user_batchdelete(useridlist) ⇒ Object



59
60
61
# File 'lib/wechat/corp_api.rb', line 59

def user_batchdelete(useridlist)
  post 'user/batchdelete', JSON.generate(useridlist: useridlist)
end

#user_delete(userid) ⇒ Object



55
56
57
# File 'lib/wechat/corp_api.rb', line 55

def user_delete(userid)
  get 'user/delete', params: { userid: userid }
end

#user_list(department_id, fetch_child = 0, status = 0) ⇒ Object



99
100
101
# File 'lib/wechat/corp_api.rb', line 99

def user_list(department_id, fetch_child = 0, status = 0)
  get 'user/list', params: { department_id: department_id, fetch_child: fetch_child, status: status }
end

#user_simplelist(department_id, fetch_child = 0, status = 0) ⇒ Object



95
96
97
# File 'lib/wechat/corp_api.rb', line 95

def user_simplelist(department_id, fetch_child = 0, status = 0)
  get 'user/simplelist', params: { department_id: department_id, fetch_child: fetch_child, status: status }
end