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/'
FILE_BASE =
'http://file.api.weixin.qq.com/cgi-bin/'
OAUTH2_BASE =
'https://api.weixin.qq.com/sns/oauth2/'

Constants inherited from ApiBase

Wechat::ApiBase::MP_BASE

Instance Attribute Summary collapse

Attributes inherited from ApiBase

#access_token, #client

Instance Method Summary collapse

Methods inherited from ApiBase

#callbackip, #qrcode

Constructor Details

#initialize(appid, secret, token_file, skip_verify_ssl, jsapi_ticket_file = '/var/tmp/wechat_jsapi_ticket') ⇒ Api

Returns a new instance of Api.



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

def initialize(appid, secret, token_file, skip_verify_ssl, jsapi_ticket_file = '/var/tmp/wechat_jsapi_ticket')
  @client = Client.new(API_BASE, skip_verify_ssl)
  @access_token = AccessToken.new(@client, appid, secret, token_file)
  @jsapi_ticket = JsapiTicket.new(@client, @access_token, jsapi_ticket_file)
end

Instance Attribute Details

#jsapi_ticketObject (readonly)

Returns the value of attribute jsapi_ticket.



8
9
10
# File 'lib/wechat/api.rb', line 8

def jsapi_ticket
  @jsapi_ticket
end

Instance Method Details

#custom_message_send(message) ⇒ Object



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

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

#group_create(group_name) ⇒ Object



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

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

#group_delete(groupid) ⇒ Object



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

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

#group_update(groupid, new_group_name) ⇒ Object



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

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

#groupsObject



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

def groups
  get 'groups/get'
end

#material(media_id) ⇒ Object



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

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

#material_add(type, file) ⇒ Object



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

def material_add(type, file)
  post 'material/add_material', { upload: { media: file } }, params: { type: type }, base: FILE_BASE
end

#material_countObject



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

def material_count
  get 'material/get_materialcount'
end

#material_delete(media_id) ⇒ Object



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

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

#material_list(type, offset, count) ⇒ Object



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

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

#media(media_id) ⇒ Object



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

def media(media_id)
  get 'media/get', params: { media_id: media_id }, base: FILE_BASE, as: :file
end

#media_create(type, file) ⇒ Object



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

def media_create(type, file)
  post 'media/upload', { upload: { media: file } }, params: { type: type }, base: FILE_BASE
end


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

def menu
  get 'menu/get'
end


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

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


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

def menu_delete
  get 'menu/delete'
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

#template_message_send(message) ⇒ Object



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

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

#user(openid) ⇒ Object



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

def user(openid)
  get 'user/info', params: { openid: openid }
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



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

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

#web_access_token(code) ⇒ Object

mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html 第二步:通过code换取网页授权access_token



125
126
127
128
129
130
131
132
133
# File 'lib/wechat/api.rb', line 125

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