feishu-rails 0.1.4 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -7
- data/lib/feishu/connector.rb +61 -93
- data/lib/feishu/version.rb +1 -1
- data/lib/feishu-rails.rb +4 -3
- metadata +2 -3
- data/lib/feishu/config.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6804988e9d3dcce71102da60cdf71cac5e9b1e1627835f022c082e9e0585310f
|
4
|
+
data.tar.gz: 9dfe63a9194bcb9c6f8bfd4f75bc5bd76d4112748c9fe792d0e7f5e9a4f4e610
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b633d47d579b2a9f9fb3539880976fc4084bbeb6c8141f852bfd99c6886823d4b1ca851fb17bcb263d3d3d47b8dd4d96e9821edcb4287016955ab53d8ae3f303
|
7
|
+
data.tar.gz: 9f1819889b23fec6163fef128db9cf30640eeac0decc881f036cf9f3e44872e8193b56e9470f0cbbe0ebf36917d317d14cc19f584ede94d297bbcb8a85b6858f
|
data/README.md
CHANGED
@@ -1,28 +1,55 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# feishu-Rails
|
2
|
+
|
3
|
+
对接飞书机器人,实现告警推送
|
3
4
|
|
4
5
|
## Usage
|
5
|
-
|
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
|
-
|
54
|
+
|
55
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/feishu/connector.rb
CHANGED
@@ -4,6 +4,7 @@ require "active_support/concern"
|
|
4
4
|
|
5
5
|
module Feishu
|
6
6
|
extend ActiveSupport::Concern
|
7
|
+
|
7
8
|
module Connector
|
8
9
|
# 定义常用的 URL
|
9
10
|
AUTH_URL = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
|
@@ -16,18 +17,9 @@ module Feishu
|
|
16
17
|
GROUP_INFO = "https://open.feishu.cn/open-apis/chat/v4/info"
|
17
18
|
SEND_TEXT = "https://open.feishu.cn/open-apis/message/v4/send"
|
18
19
|
|
19
|
-
# 获取飞书 TOKEN 并缓存
|
20
|
-
def access_token
|
21
|
-
Rails.cache.fetch("feishu_token", expires_in: 2.hours) do
|
22
|
-
# res = HTTP.post(AUTH_URL, json: { app_id: app_id, app_secret: app_secret })
|
23
|
-
res = HTTP.post(AUTH_URL, json: { app_id: Config.app_id, app_secret: Config.app_secret })
|
24
|
-
JSON.parse(res.body.readpartial)["tenant_access_token"]
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
20
|
# 认证成功后,后续请求都携带 TOKEN
|
29
21
|
def request
|
30
|
-
HTTP.headers(Authorization: "Bearer #{
|
22
|
+
HTTP.headers(Authorization: "Bearer #{feishu_token}")
|
31
23
|
end
|
32
24
|
|
33
25
|
# 根据手机号码查询用户的 USER_ID
|
@@ -37,18 +29,20 @@ module Feishu
|
|
37
29
|
res = request.get(url)
|
38
30
|
# 序列化
|
39
31
|
ret = JSON.parse(res.body.readpartial)
|
32
|
+
|
40
33
|
# 返回数据
|
41
34
|
ret["data"]["mobile_users"].try(:[], mobile).try(:[], 0).try(:[], "user_id")
|
42
35
|
end
|
43
36
|
|
44
|
-
#
|
45
|
-
def chat_id
|
37
|
+
# 获取群组的 chat_id
|
38
|
+
def chat_id(name)
|
46
39
|
# 请求后端
|
47
40
|
res = request.get(CHAT_ID)
|
48
41
|
# 序列化
|
49
42
|
ret = JSON.parse(res.body.readpartial)
|
43
|
+
|
50
44
|
# 返回数据
|
51
|
-
ret["data"]["groups"].try(:[], 0).try(:[], "chat_id")
|
45
|
+
ret["data"]["groups"].select { |i| i["name"] == name }.try(:[], 0).try(:[], "chat_id")
|
52
46
|
end
|
53
47
|
|
54
48
|
# 上传图片到飞书后台
|
@@ -59,8 +53,11 @@ module Feishu
|
|
59
53
|
# 请求后端
|
60
54
|
res = request.post(IMAGE_ID, form: { image_type: "message", image: data })
|
61
55
|
ret = JSON.parse res.body.readpartial
|
56
|
+
|
62
57
|
# 返回上传后的序列号
|
63
58
|
ret["data"]["image_key"]
|
59
|
+
rescue => e
|
60
|
+
Rails.logger("上传图片期间捕捉到异常:#{e}")
|
64
61
|
end
|
65
62
|
|
66
63
|
# 获取机器人所在的群列表
|
@@ -74,7 +71,7 @@ module Feishu
|
|
74
71
|
end
|
75
72
|
|
76
73
|
# 获取群信息
|
77
|
-
def
|
74
|
+
def group_members(chat_id)
|
78
75
|
# 请求后端
|
79
76
|
res = request.get(GROUP_INFO, params: { chat_id: chat_id })
|
80
77
|
# 序列化
|
@@ -83,14 +80,6 @@ module Feishu
|
|
83
80
|
ret["data"]["members"]
|
84
81
|
end
|
85
82
|
|
86
|
-
# 获取群成员信息
|
87
|
-
# def member_list(chat_id)
|
88
|
-
# res = request.get("https://open.feishu.cn/open-apis/chat/v4/members", :params => {:chat_id=> chat_id})
|
89
|
-
# group_users_content = JSON.parse(res.body.readpartial)
|
90
|
-
|
91
|
-
# member_ids = group_users_content["data"]
|
92
|
-
# end
|
93
|
-
|
94
83
|
# 发送文本消息
|
95
84
|
def send_text(open_id, text)
|
96
85
|
# 请求后端
|
@@ -100,93 +89,72 @@ module Feishu
|
|
100
89
|
end
|
101
90
|
|
102
91
|
# 使用推荐的消息接口
|
103
|
-
def send_message(receive_type, receive_id, msg_type, content)
|
92
|
+
def send_message(receive_type = "chat_id", receive_id, msg_type, content)
|
104
93
|
# 请求后端
|
105
94
|
res = request.post(MESSAGE_URL, params: { receive_id_type: receive_type }, json: { receive_id: receive_id, msg_type: msg_type, content: content })
|
106
95
|
# 序列化
|
107
96
|
JSON.parse(res.body.readpartial)
|
108
97
|
end
|
109
98
|
|
110
|
-
#
|
111
|
-
def send_alert(chat_id)
|
112
|
-
|
113
|
-
|
114
|
-
"msg_type": "post",
|
115
|
-
"content": {
|
116
|
-
"post": {
|
117
|
-
"zh_cn": {
|
118
|
-
"title": "title",
|
119
|
-
"content": [
|
120
|
-
[
|
121
|
-
{
|
122
|
-
"tag": "text",
|
123
|
-
"un_escape": true,
|
124
|
-
"text": "content"
|
125
|
-
},
|
126
|
-
{
|
127
|
-
"tag": "at",
|
128
|
-
"user_id": user_id("15673443571")
|
129
|
-
|
130
|
-
},
|
131
|
-
],
|
132
|
-
[
|
133
|
-
{
|
134
|
-
"tag": "img",
|
135
|
-
"image_key": upload_image("/home/careline/Codes/mojo.png"),
|
136
|
-
"width": 1000,
|
137
|
-
"height": 600
|
138
|
-
}
|
139
|
-
]
|
140
|
-
]
|
141
|
-
}
|
142
|
-
}
|
143
|
-
}
|
144
|
-
}
|
145
|
-
# 请求后端
|
146
|
-
res = request.post(SEND_TEXT, json: data)
|
147
|
-
# 序列化
|
148
|
-
JSON.parse(res.body.readpartial)
|
149
|
-
end
|
99
|
+
# 向特定群组发生消息
|
100
|
+
def send_alert(chat_id, title, content, image_path)
|
101
|
+
# 获取上传文件路径
|
102
|
+
uploaded_image_path = upload_image(image_path)
|
150
103
|
|
151
|
-
|
152
|
-
def send_recovery_message(title, content)
|
104
|
+
# 初始化数据结构
|
153
105
|
data = {
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
}
|
180
|
-
]
|
106
|
+
chat_id: chat_id,
|
107
|
+
msg_type: "post",
|
108
|
+
content: {
|
109
|
+
post: {
|
110
|
+
zh_cn: {
|
111
|
+
title: title,
|
112
|
+
content: [
|
113
|
+
[
|
114
|
+
{
|
115
|
+
tag: "text",
|
116
|
+
un_escape: true,
|
117
|
+
text: content
|
118
|
+
},
|
119
|
+
{
|
120
|
+
tag: "at",
|
121
|
+
user_id: "all"
|
122
|
+
},
|
123
|
+
],
|
124
|
+
[
|
125
|
+
{
|
126
|
+
tag: "img",
|
127
|
+
image_key: upload_image(image_path),
|
128
|
+
width: 1000,
|
129
|
+
height: 600
|
130
|
+
}
|
181
131
|
]
|
132
|
+
]
|
182
133
|
}
|
183
134
|
}
|
184
135
|
}
|
185
136
|
}
|
137
|
+
# 如果图片不存在则移除相关属性
|
138
|
+
data[:content][:post][:zh_cn][:content].pop if uploaded_image_path.blank?
|
139
|
+
|
186
140
|
# 请求后端
|
187
141
|
res = request.post(SEND_TEXT, json: data)
|
142
|
+
|
188
143
|
# 序列化
|
189
|
-
JSON.parse(res.body.readpartial)
|
144
|
+
JSON.parse(res.body.readpartial).to_query
|
145
|
+
|
146
|
+
rescue => e
|
147
|
+
Rails.logger("群发消息期间捕捉到异常:#{e}")
|
190
148
|
end
|
191
149
|
end
|
150
|
+
|
151
|
+
private
|
152
|
+
def feishu_token
|
153
|
+
# 获取飞书 TOKEN 并缓存
|
154
|
+
Rails.cache.fetch("feishu_token", expires_in: 30.minutes) do
|
155
|
+
# res = HTTP.post(AUTH_URL, json: { app_id: app_id, app_secret: app_secret })
|
156
|
+
res = HTTP.post(AUTH_URL, json: { app_id: Feishu.app_id, app_secret: Feishu.app_secret })
|
157
|
+
JSON.parse(res.body.readpartial)["tenant_access_token"]
|
158
|
+
end
|
159
|
+
end
|
192
160
|
end
|
data/lib/feishu/version.rb
CHANGED
data/lib/feishu-rails.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "feishu/version"
|
4
|
-
require "feishu/config"
|
5
4
|
require "feishu/connector"
|
6
5
|
|
7
6
|
module Feishu
|
8
7
|
class << self
|
9
|
-
|
10
|
-
|
8
|
+
attr_accessor :app_id, :app_secret, :encrypt_key
|
9
|
+
|
10
|
+
def config
|
11
|
+
yield self
|
11
12
|
end
|
12
13
|
end
|
13
14
|
end
|
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.
|
4
|
+
version: 0.1.7
|
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-
|
11
|
+
date: 2022-05-24 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
|