feishu-rails 0.1.3 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c9ffbd5349b1725eff7ac29f5822779d3566dd3a3dca6bb7577845d5b62015cf
4
- data.tar.gz: 787d901f41edd64c8b5e19e5f2b2aa27066fa35840a356d055a994bfbee12ad5
3
+ metadata.gz: 7b49a7ba4343fbd7ae4e4d35d00e999cbdd54a568cff97634bcbf6fdda102862
4
+ data.tar.gz: 2b1117e10d4c5b2cd6f6097dd3f8c66313fac00c2058cd138af6e661cc4836d7
5
5
  SHA512:
6
- metadata.gz: 636d3abd7ce1658069e20f818fd176d48311900d5e559a2281ba460fd2c9a349d5cb553820ece62e7367875684758c0c01cb6d182161329ad746848b776b75ee
7
- data.tar.gz: bb5dbb5d2049ba3fec04bcccbe0a4723b7072b8319bd65c098a74edc1ee1f7acdb6ff02ed0888a71e04d80a5bf29fbb328df0c194222006de1068dd3ef3637bf
6
+ metadata.gz: 41c6b52e05e546e709f3993cec155858be0d898069dafc83e9e64c576343045e1f3cda5fcc940c685b0a3793abe5b73d75f871c4444ee6ffa4b2c1bc3df7d6cd
7
+ data.tar.gz: c040c72e826d829ccee8bd2cfb6773c05cc28156659abc0fd5a3a82b022b74f1637b1f7deb38f085a85993e270131c55b577481bf0cdbc2ff51a7756b633ae35
data/README.md CHANGED
@@ -1,28 +1,55 @@
1
- # Feishu
2
- Short description and motivation.
1
+ # feishu-Rails
2
+
3
+ 对接飞书机器人,实现告警推送
3
4
 
4
5
  ## Usage
5
- How to use my plugin.
6
+
7
+ - include Feishu::Connector 到模块或者类对象;
8
+ - 主要实现的方法:[:user_id, :send_text, :group_members, :request, :send_alert, :access_token, :chat_id, :upload_image, :send_message, :group_list]
9
+ - 群内推送 send_alert(chat_id, title, content, image_path)
10
+ - 上传图片到飞书 upload_image(path) ,建议使用图片绝对路径
6
11
 
7
12
  ## Installation
13
+
8
14
  Add this line to your application's Gemfile:
9
15
 
10
16
  ```ruby
11
- gem "feishu"
17
+ gem "feishu-rails"
12
18
  ```
13
19
 
14
20
  And then execute:
21
+
15
22
  ```bash
16
23
  $ bundle
17
24
  ```
18
25
 
19
26
  Or install it yourself as:
27
+
20
28
  ```bash
21
- $ gem install feishu
29
+ $ gem install feishu-rails
30
+ ```
31
+
32
+ add feishu_rails.rb in config/initializers
33
+
34
+ ```ruby
35
+ Feishu.config do |f|
36
+ f.app_id = "xxx"
37
+ f.app_secret = "yyy"
38
+ f.encrypt_key = "zzz"
39
+ end
40
+ ```
41
+
42
+ 推送群组告警消息:
43
+
44
+ ```ruby
45
+ include Feishu::Connector
46
+
47
+ Feishu::Connector.send_alert("oc_xxx", "title", "content", "/home/xxx/Codes/xxx.png")
48
+
22
49
  ```
23
50
 
24
51
  ## Contributing
25
- Contribution directions go here.
26
52
 
27
53
  ## License
28
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
54
+
55
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/setup"
2
4
 
3
5
  require "bundler/gem_tasks"
@@ -1,21 +1,27 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/concern"
2
4
 
3
5
  module Feishu
4
6
  extend ActiveSupport::Concern
7
+
5
8
  module Connector
6
9
  # 定义常用的 URL
7
- AUTH_URL = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/"
8
- USER_INFO = "https://open.feishu.cn/open-apis/contact/v1/user/batch_get"
9
- GROUP_LIST = "https://open.feishu.cn/open-apis/chat/v4/list"
10
- GROUP_INFO = "https://open.feishu.cn/open-apis/chat/v4/info"
11
- SEND_TEXT = "https://open.feishu.cn/open-apis/message/v4/send/"
12
- IMAGE_URL = "https://open.feishu.cn/open-apis/im/v1/images"
10
+ AUTH_URL = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
11
+ USER_ID = "https://open.feishu.cn/open-apis/user/v1/batch_get_id?mobiles="
12
+ CHAT_ID = "https://open.feishu.cn/open-apis/chat/v4/list?page_size=20"
13
+ IMAGE_ID = "https://open.feishu.cn/open-apis/im/v1/images"
13
14
  MESSAGE_URL = "https://open.feishu.cn/open-apis/im/v1/messages"
14
15
 
16
+ GROUP_LIST = "https://open.feishu.cn/open-apis/im/v1/chats"
17
+ GROUP_INFO = "https://open.feishu.cn/open-apis/chat/v4/info"
18
+ SEND_TEXT = "https://open.feishu.cn/open-apis/message/v4/send"
19
+
15
20
  # 获取飞书 TOKEN 并缓存
16
21
  def access_token
17
- Rails.cache.fetch("feishu_token", expires_in: 2.hours) do
18
- res = HTTP.post(AUTH_URL, json: { app_id: Config.app_id, app_secret: Config.app_secret })
22
+ Rails.cache.fetch("feishu_token", expires_in: 30.minutes) do
23
+ # res = HTTP.post(AUTH_URL, json: { app_id: app_id, app_secret: app_secret })
24
+ res = HTTP.post(AUTH_URL, json: { app_id: Feishu.app_id, app_secret: Feishu.app_secret })
19
25
  JSON.parse(res.body.readpartial)["tenant_access_token"]
20
26
  end
21
27
  end
@@ -25,51 +31,62 @@ module Feishu
25
31
  HTTP.headers(Authorization: "Bearer #{access_token}")
26
32
  end
27
33
 
28
- def batch_user_info(open_ids)
29
- res = request.get(USER_INFO, params: { open_ids: open_ids })
30
- users = JSON.parse(res.body.readpartial)
31
-
32
- users["data"]
33
- end
34
-
35
- # 获取机器人所在的群列表
36
- def group_list(page_size = 200, page = nil)
34
+ # 根据手机号码查询用户的 USER_ID
35
+ def user_id(mobile)
36
+ url = "#{USER_ID}#{mobile}"
37
37
  # 请求后端
38
- res = request.get(GROUP_LIST, params: { page_size: page_size, page_token: page })
38
+ res = request.get(url)
39
39
  # 序列化
40
- group_content = JSON.parse(res.body.readpartial)
40
+ ret = JSON.parse(res.body.readpartial)
41
41
 
42
- group_content["data"]
42
+ # 返回数据
43
+ ret["data"]["mobile_users"].try(:[], mobile).try(:[], 0).try(:[], "user_id")
43
44
  end
44
45
 
45
- # 获取群信息
46
- def group_info(chat_id)
46
+ # 获取群组的 chat_id
47
+ def chat_id(name)
47
48
  # 请求后端
48
- res = request.get(GROUP_INFO, params: { chat_id: chat_id })
49
+ res = request.get(CHAT_ID)
49
50
  # 序列化
50
- group_users_content = JSON.parse(res.body.readpartial)
51
+ ret = JSON.parse(res.body.readpartial)
51
52
 
52
- group_users_content["data"]["members"]
53
+ # 返回数据
54
+ ret["data"]["groups"].select { |i| i["name"] == name }.try(:[], 0).try(:[], "chat_id")
53
55
  end
54
56
 
55
- # 获取群成员信息
56
- # def member_list(chat_id)
57
- # res = request.get("https://open.feishu.cn/open-apis/chat/v4/members", :params => {:chat_id=> chat_id})
58
- # group_users_content = JSON.parse(res.body.readpartial)
59
-
60
- # member_ids = group_users_content["data"]
61
- # end
62
-
63
57
  # 上传图片到飞书后台
64
58
  def upload_image(image_path)
65
59
  # 读取图片
66
60
  data = HTTP::FormData::File.new image_path
67
61
 
68
62
  # 请求后端
69
- res = request.post(IMAGE_URL, form: { image_type: "message", image: data })
63
+ res = request.post(IMAGE_ID, form: { image_type: "message", image: data })
70
64
  ret = JSON.parse res.body.readpartial
65
+
71
66
  # 返回上传后的序列号
72
67
  ret["data"]["image_key"]
68
+ rescue => e
69
+ Rails.logger("上传图片期间捕捉到异常:#{e}")
70
+ end
71
+
72
+ # 获取机器人所在的群列表
73
+ def group_list(page_size = 20, page = nil)
74
+ # 请求后端
75
+ res = request.get(GROUP_LIST, params: { page_size: page_size, page_token: page })
76
+ # 序列化
77
+ ret = JSON.parse(res.body.readpartial)
78
+ # 返回数据
79
+ ret["data"]
80
+ end
81
+
82
+ # 获取群信息
83
+ def group_members(chat_id)
84
+ # 请求后端
85
+ res = request.get(GROUP_INFO, params: { chat_id: chat_id })
86
+ # 序列化
87
+ ret = JSON.parse(res.body.readpartial)
88
+ # 返回数据
89
+ ret["data"]["members"]
73
90
  end
74
91
 
75
92
  # 发送文本消息
@@ -81,48 +98,55 @@ module Feishu
81
98
  end
82
99
 
83
100
  # 使用推荐的消息接口
84
- def send_message(receive_type, receive_id, msg_type, content)
101
+ def send_message(receive_type = "chat_id", receive_id, msg_type, content)
85
102
  # 请求后端
86
103
  res = request.post(MESSAGE_URL, params: { receive_id_type: receive_type }, json: { receive_id: receive_id, msg_type: msg_type, content: content })
87
104
  # 序列化
88
105
  JSON.parse(res.body.readpartial)
89
106
  end
90
107
 
91
- # 发生富文本消息
92
- # def send_alert(chat_id)
93
- # data = {
94
- # "chat_id": chat_id,
95
- # "msg_type": "post",
96
- # "content": {
97
- # "post": {
98
- # "zh_cn": {
99
- # "title": title,
100
- # "content": [
101
- # [
102
- # {
103
- # "tag": "text",
104
- # "un_escape": True,
105
- # "text": content
106
- # },
107
- # {
108
- # "tag": "at",
109
- # "user_id": self.user_id
110
- #
111
- # },
112
- # ],
113
- # [
114
- # {
115
- # "tag": "img",
116
- # "image_key": self.image_key,
117
- # "width": 1000,
118
- # "height": 600
119
- # }
120
- # ]
121
- # ]
122
- # }
123
- # }
124
- # }
125
- # }
126
- # end
108
+ # 向特定群组发生消息
109
+ def send_alert(chat_id, title, content, image_path)
110
+ data = {
111
+ chat_id: chat_id,
112
+ msg_type: "post",
113
+ content: {
114
+ post: {
115
+ zh_cn: {
116
+ title: title,
117
+ content: [
118
+ [
119
+ {
120
+ tag: "text",
121
+ un_escape: true,
122
+ text: content
123
+ },
124
+ {
125
+ tag: "at",
126
+ user_id: "all"
127
+ },
128
+ ],
129
+ [
130
+ {
131
+ tag: "img",
132
+ image_key: upload_image(image_path),
133
+ width: 1000,
134
+ height: 600
135
+ }
136
+ ]
137
+ ]
138
+ }
139
+ }
140
+ }
141
+ }
142
+ # 请求后端
143
+ res = request.post(SEND_TEXT, json: data)
144
+
145
+ # 序列化
146
+ JSON.parse(res.body.readpartial).to_query
147
+
148
+ rescue => e
149
+ Rails.logger("群发消息期间捕捉到异常:#{e}")
150
+ end
127
151
  end
128
152
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Feishu
2
- VERSION = "0.1.3"
4
+ VERSION = "0.1.6"
3
5
  end
data/lib/feishu-rails.rb CHANGED
@@ -1,11 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "feishu/version"
2
- require "feishu/config"
3
4
  require "feishu/connector"
4
5
 
5
6
  module Feishu
6
7
  class << self
7
- def configure(&block)
8
- Config.configure(&block)
8
+ attr_accessor :app_id, :app_secret, :encrypt_key
9
+
10
+ def config
11
+ yield self
9
12
  end
10
13
  end
11
14
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # desc "Explaining what the task does"
2
3
  # task :feishu do
3
4
  # # Task goes here
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: feishu-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - WENWU.YAN
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-26 00:00:00.000000000 Z
11
+ date: 2022-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -34,7 +34,6 @@ files:
34
34
  - README.md
35
35
  - Rakefile
36
36
  - lib/feishu-rails.rb
37
- - lib/feishu/config.rb
38
37
  - lib/feishu/connector.rb
39
38
  - lib/feishu/version.rb
40
39
  - lib/tasks/feishu_tasks.rake
data/lib/feishu/config.rb DELETED
@@ -1,11 +0,0 @@
1
- module Feishu
2
- class Config
3
- class << self
4
- attr_accessor :app_id, :app_secret, :encrypt_key
5
-
6
- def configure
7
- yield self
8
- end
9
- end
10
- end
11
- end