Class: Wechat::Api

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

Constant Summary collapse

API_BASE =
'https://api.weixin.qq.com/cgi-bin/'.freeze
OAUTH2_BASE =
'https://api.weixin.qq.com/sns/'.freeze

Constants inherited from ApiBase

Wechat::ApiBase::MP_BASE

Instance Attribute Summary

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, timeout, skip_verify_ssl, jsapi_ticket_file) ⇒ Api



10
11
12
13
14
# File 'lib/wechat/api.rb', line 10

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

Instance Method Details

#custom_message_send(message) ⇒ Object



124
125
126
# File 'lib/wechat/api.rb', line 124

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

#customservice_getonlinekflistObject



132
133
134
# File 'lib/wechat/api.rb', line 132

def customservice_getonlinekflist
  get 'customservice/getonlinekflist'
end

#group_create(group_name) ⇒ Object



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

def group_create(group_name)
  post 'groups/create', JSON.generate(group: { name: group_name })
end

#group_delete(groupid) ⇒ Object



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

def group_delete(groupid)
  post 'groups/delete', JSON.generate(group: { id: groupid })
end

#group_update(groupid, new_group_name) ⇒ Object



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

def group_update(groupid, new_group_name)
  post 'groups/update', JSON.generate(group: { id: groupid, name: new_group_name })
end

#groupsObject



16
17
18
# File 'lib/wechat/api.rb', line 16

def groups
  get 'groups/get'
end

#material(media_id) ⇒ Object



104
105
106
# File 'lib/wechat/api.rb', line 104

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

#material_add(type, file) ⇒ Object



116
117
118
# File 'lib/wechat/api.rb', line 116

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

#material_countObject



108
109
110
# File 'lib/wechat/api.rb', line 108

def material_count
  get 'material/get_materialcount'
end

#material_delete(media_id) ⇒ Object



120
121
122
# File 'lib/wechat/api.rb', line 120

def material_delete(media_id)
  post 'material/del_material', media_id: media_id
end

#material_list(type, offset, count) ⇒ Object



112
113
114
# File 'lib/wechat/api.rb', line 112

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


78
79
80
# File 'lib/wechat/api.rb', line 78

def menu
  get 'menu/get'
end


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

def menu_addconditional(menu)
  # Wechat not accept 7bit escaped json(eg \uxxxx), must using UTF-8, possible security vulnerability?
  post 'menu/addconditional', JSON.generate(menu)
end


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

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


100
101
102
# File 'lib/wechat/api.rb', line 100

def menu_delconditional(menuid)
  post 'menu/delconditional', JSON.generate(menuid: menuid)
end


82
83
84
# File 'lib/wechat/api.rb', line 82

def menu_delete
  get 'menu/delete'
end


96
97
98
# File 'lib/wechat/api.rb', line 96

def menu_trymatch(user_id)
  post 'menu/trymatch', JSON.generate(user_id: user_id)
end

#qrcode_create_limit_scene(scene_id_or_str) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/wechat/api.rb', line 63

def qrcode_create_limit_scene(scene_id_or_str)
  case scene_id_or_str
  when Fixnum
    post 'qrcode/create', JSON.generate(action_name: 'QR_LIMIT_SCENE',
                                        action_info: { scene: { scene_id: scene_id_or_str } })
  else
    post 'qrcode/create', JSON.generate(action_name: 'QR_LIMIT_STR_SCENE',
                                        action_info: { scene: { scene_str: scene_id_or_str } })
  end
end

#qrcode_create_scene(scene_id, expire_seconds = 604800) ⇒ Object



57
58
59
60
61
# File 'lib/wechat/api.rb', line 57

def qrcode_create_scene(scene_id, expire_seconds = 604800)
  post 'qrcode/create', JSON.generate(expire_seconds: expire_seconds,
                                      action_name: 'QR_SCENE',
                                      action_info: { scene: { scene_id: scene_id } })
end

#shorturl(long_url) ⇒ Object



74
75
76
# File 'lib/wechat/api.rb', line 74

def shorturl(long_url)
  post 'shorturl', JSON.generate(action: 'long2short', long_url: long_url)
end

#tag(tagid, next_openid = '') ⇒ Object



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

def tag(tagid, next_openid = '')
  post 'user/tag/get', JSON.generate(tagid: tagid, next_openid: next_openid)
end

#tag_add_user(tagid, openids) ⇒ Object



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

def tag_add_user(tagid, openids)
  post 'tags/members/batchtagging', JSON.generate(openid_list: openids, tagid: tagid)
end

#tag_create(tag_name) ⇒ Object



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

def tag_create(tag_name)
  post 'tags/create', JSON.generate(tag: { name: tag_name })
end

#tag_del_user(tagid, openids) ⇒ Object



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

def tag_del_user(tagid, openids)
  post 'tags/members/batchuntagging', JSON.generate(openid_list: openids, tagid: tagid)
end

#tag_delete(tagid) ⇒ Object



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

def tag_delete(tagid)
  post 'tags/delete', JSON.generate(tag: { id: tagid })
end

#tag_update(tagid, new_tag_name) ⇒ Object



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

def tag_update(tagid, new_tag_name)
  post 'tags/update', JSON.generate(tag: { id: tagid, name: new_tag_name })
end

#tagsObject



136
137
138
# File 'lib/wechat/api.rb', line 136

def tags
  get 'tags/get'
end

#template_message_send(message) ⇒ Object



128
129
130
# File 'lib/wechat/api.rb', line 128

def template_message_send(message)
  post 'message/template/send', message.to_json, content_type: :json
end

#user(openid) ⇒ Object



37
38
39
# File 'lib/wechat/api.rb', line 37

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

#user_batchget(openids, lang = 'zh-CN') ⇒ Object



41
42
43
# File 'lib/wechat/api.rb', line 41

def user_batchget(openids, lang = 'zh-CN')
  post 'user/info/batchget', JSON.generate(user_list: openids.collect { |v| { openid: v, lang: lang } })
end

#user_change_group(openid, to_groupid) ⇒ Object



49
50
51
# File 'lib/wechat/api.rb', line 49

def user_change_group(openid, to_groupid)
  post 'groups/members/update', JSON.generate(openid: openid, to_groupid: to_groupid)
end

#user_group(openid) ⇒ Object



45
46
47
# File 'lib/wechat/api.rb', line 45

def user_group(openid)
  post 'groups/getid', JSON.generate(openid: openid)
end

#user_update_remark(openid, remark) ⇒ Object



53
54
55
# File 'lib/wechat/api.rb', line 53

def user_update_remark(openid, remark)
  post 'user/info/updateremark', JSON.generate(openid: openid, remark: remark)
end

#users(nextid = nil) ⇒ Object



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

def users(nextid = nil)
  params = { params: { next_openid: nextid } } if nextid.present?
  get('user/get', params || {})
end

#web_access_token(code) ⇒ Object



166
167
168
169
170
171
172
173
174
# File 'lib/wechat/api.rb', line 166

def web_access_token(code)
  params = {
    appid: access_token.appid,
    secret: access_token.secret,
    code: code,
    grant_type: 'authorization_code'
  }
  client.get 'oauth2/access_token', params: params, base: OAUTH2_BASE
end

#web_auth_access_token(web_access_token, openid) ⇒ Object



176
177
178
# File 'lib/wechat/api.rb', line 176

def web_auth_access_token(web_access_token, openid)
  client.get 'auth', params: { access_token: web_access_token, openid: openid }, base: OAUTH2_BASE
end

#web_refresh_access_token(user_refresh_token) ⇒ Object



180
181
182
183
184
185
186
187
# File 'lib/wechat/api.rb', line 180

def web_refresh_access_token(user_refresh_token)
  params = {
    appid: access_token.appid,
    grant_type: 'refresh_token',
    refresh_token: user_refresh_token
  }
  client.get 'oauth2/refresh_token', params: params, base: OAUTH2_BASE
end

#web_userinfo(web_access_token, openid, lang = 'zh_CN') ⇒ Object



189
190
191
# File 'lib/wechat/api.rb', line 189

def web_userinfo(web_access_token, openid, lang = 'zh_CN')
  client.get 'userinfo', params: { access_token: web_access_token, openid: openid, lang: lang }, base: OAUTH2_BASE
end