Module: Wechat

Extended by:
Wechat
Included in:
Wechat
Defined in:
lib/ocean/wechat.rb

Overview

Wechat service, View more at: https://mp.weixin.qq.com/wiki example:

Wechat.notify(event: 'crash', first: 'Your website is not accessible', time: Time.now, reason: 'Connect fail!', remark: 'remark', redirect_url: 'www.baidu.com')

Constant Summary collapse

BASE_URL =
'https://api.weixin.qq.com/cgi-bin'
ACCESS_TOKEN_FILE =
'tmp/access_token.txt'
NOTIFY_RECIPIENT_FILE =
'config/wechat_notify.yml'
SUCCESS_CODE =
0
ACCESS_TOKEN_EXPIRED_CODE =
420_01
ACCESS_TOKEN_INVALID_CODE =
400_01
NOTIFY_DEFAULT_COLOR =
'#173177'
MAX_RETRY_NUM =
5

Instance Method Summary collapse

Instance Method Details

#access_token ⇒ Object



18
19
20
# File 'lib/ocean/wechat.rb', line 18

def access_token
  read_access_token || refresh_access_token
end

#add_tag_for_users(users, tag_id) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/ocean/wechat.rb', line 89

def add_tag_for_users(users, tag_id)
  params = {
    open_list: users,
    tagid: tag_id
  }

  request("#{BASE_URL}/tags/members/batchtagging",  params, :post)
end

#create_user_tag(name) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/ocean/wechat.rb', line 58

def create_user_tag(name)
  params = {
    tag: {
      name: name
    }
  }

  request("#{BASE_URL}/tags/create", params, :post)
end

#find_tag_id_by_name(name) ⇒ Object



85
86
87
# File 'lib/ocean/wechat.rb', line 85

def find_tag_id_by_name(name)
  user_tags[:tags].detect { |item| item['name'] == name }['id']
end

#find_users_by_tag(name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ocean/wechat.rb', line 73

def find_users_by_tag(name)
  res = request(
    "#{BASE_URL}/user/tag/get",
    {
      tagid: find_tag_id_by_name(name)
    },
    :post
  )

  res['count'] == 0 ? [] : res['data']['openid']
end

#notify(**args) ⇒ Object

Use wechat template to send message



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ocean/wechat.rb', line 23

def notify(**args)
  notify_block = lambda do |open_id|
    url = "#{BASE_URL}/message/template/send"
    params = {
      touser: open_id,
      template_id: wechat_notify(args[:event], 'template'),
      url: args[:redirect_url],
      data: {}
    }

    args.each { |k, v| params[:data][k] = { value: v, color: NOTIFY_DEFAULT_COLOR } }

    request(url, params, :post)
  end

  [].tap do |item|
    wechat_notify(args[:event], 'users').each do |open_id|
      item << {
        open_id: open_id,
        success: notify_block.call(open_id)[:errcode] == SUCCESS_CODE
      }
    end
  end
end

#notify_templates ⇒ Object

Get all notify template



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

def notify_templates
  request("#{BASE_URL}/template/get_all_private_template")
end

#user_tags ⇒ Object

Get all user tag



69
70
71
# File 'lib/ocean/wechat.rb', line 69

def user_tags
  request("#{BASE_URL}/tags/get")
end

#users ⇒ Object

Get all followers



54
55
56
# File 'lib/ocean/wechat.rb', line 54

def users
  request("#{BASE_URL}/user/get")
end