feishu-rails 0.1.1 → 0.1.4
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 +4 -4
- data/Rakefile +2 -0
- data/lib/feishu/config.rb +2 -0
- data/lib/feishu/connector.rb +134 -68
- data/lib/feishu/version.rb +3 -1
- data/lib/feishu-rails.rb +2 -0
- data/lib/tasks/feishu_tasks.rake +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 257e57680f67a6b05f24836a5f765fac25c6d6effaa4ae7e88843bc2df4e861c
|
4
|
+
data.tar.gz: 0f81c2bdd6e4a3cf560fa530b78366e11d2df0f36208f159afc4736724fb3975
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d4aedc5c5524d3552cb99789a27513e7693eda443aa10f2ac7f19b6118024891b80019aa37a6eaa461750ca20bbea93cb331a561be8914f88f0794e0e45d102a
|
7
|
+
data.tar.gz: 152aec5445a385cc3f5fc543616c14fac8b459da4085fb6f6f001711da0d2055fc82766f561488f4dd7e31f1c771ffd483e48094357899377d6343816cba61b8
|
data/Rakefile
CHANGED
data/lib/feishu/config.rb
CHANGED
data/lib/feishu/connector.rb
CHANGED
@@ -1,21 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/concern"
|
4
|
+
|
1
5
|
module Feishu
|
2
|
-
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
module Connector
|
3
8
|
# 定义常用的 URL
|
4
|
-
AUTH_URL = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
SEND_TEXT = "https://open.feishu.cn/open-apis/message/v4/send/"
|
9
|
-
IMAGE_URL = "https://open.feishu.cn/open-apis/im/v1/images"
|
9
|
+
AUTH_URL = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
|
10
|
+
USER_ID = "https://open.feishu.cn/open-apis/user/v1/batch_get_id?mobiles="
|
11
|
+
CHAT_ID = "https://open.feishu.cn/open-apis/chat/v4/list?page_size=20"
|
12
|
+
IMAGE_ID = "https://open.feishu.cn/open-apis/im/v1/images"
|
10
13
|
MESSAGE_URL = "https://open.feishu.cn/open-apis/im/v1/messages"
|
11
14
|
|
15
|
+
GROUP_LIST = "https://open.feishu.cn/open-apis/im/v1/chats"
|
16
|
+
GROUP_INFO = "https://open.feishu.cn/open-apis/chat/v4/info"
|
17
|
+
SEND_TEXT = "https://open.feishu.cn/open-apis/message/v4/send"
|
18
|
+
|
12
19
|
# 获取飞书 TOKEN 并缓存
|
13
20
|
def access_token
|
14
21
|
Rails.cache.fetch("feishu_token", expires_in: 2.hours) do
|
15
|
-
|
16
|
-
|
17
|
-
res = HTTP.post(AUTH_URL, json: { app_id: app_id, app_secret: app_secret })
|
18
|
-
# res = HTTP.post(AUTH_URL, json: { app_id: Config.app_id, app_secret: Config.app_secret })
|
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 })
|
19
24
|
JSON.parse(res.body.readpartial)["tenant_access_token"]
|
20
25
|
end
|
21
26
|
end
|
@@ -25,21 +30,47 @@ module Feishu
|
|
25
30
|
HTTP.headers(Authorization: "Bearer #{access_token}")
|
26
31
|
end
|
27
32
|
|
28
|
-
|
29
|
-
|
30
|
-
|
33
|
+
# 根据手机号码查询用户的 USER_ID
|
34
|
+
def user_id(mobile)
|
35
|
+
url = "#{USER_ID}#{mobile}"
|
36
|
+
# 请求后端
|
37
|
+
res = request.get(url)
|
38
|
+
# 序列化
|
39
|
+
ret = JSON.parse(res.body.readpartial)
|
40
|
+
# 返回数据
|
41
|
+
ret["data"]["mobile_users"].try(:[], mobile).try(:[], 0).try(:[], "user_id")
|
42
|
+
end
|
31
43
|
|
32
|
-
|
44
|
+
# 获取用户 CHAT_ID
|
45
|
+
def chat_id
|
46
|
+
# 请求后端
|
47
|
+
res = request.get(CHAT_ID)
|
48
|
+
# 序列化
|
49
|
+
ret = JSON.parse(res.body.readpartial)
|
50
|
+
# 返回数据
|
51
|
+
ret["data"]["groups"].try(:[], 0).try(:[], "chat_id")
|
52
|
+
end
|
53
|
+
|
54
|
+
# 上传图片到飞书后台
|
55
|
+
def upload_image(image_path)
|
56
|
+
# 读取图片
|
57
|
+
data = HTTP::FormData::File.new image_path
|
58
|
+
|
59
|
+
# 请求后端
|
60
|
+
res = request.post(IMAGE_ID, form: { image_type: "message", image: data })
|
61
|
+
ret = JSON.parse res.body.readpartial
|
62
|
+
# 返回上传后的序列号
|
63
|
+
ret["data"]["image_key"]
|
33
64
|
end
|
34
65
|
|
35
66
|
# 获取机器人所在的群列表
|
36
|
-
def group_list(page_size =
|
67
|
+
def group_list(page_size = 20, page = nil)
|
37
68
|
# 请求后端
|
38
69
|
res = request.get(GROUP_LIST, params: { page_size: page_size, page_token: page })
|
39
70
|
# 序列化
|
40
|
-
|
41
|
-
|
42
|
-
|
71
|
+
ret = JSON.parse(res.body.readpartial)
|
72
|
+
# 返回数据
|
73
|
+
ret["data"]
|
43
74
|
end
|
44
75
|
|
45
76
|
# 获取群信息
|
@@ -47,9 +78,9 @@ module Feishu
|
|
47
78
|
# 请求后端
|
48
79
|
res = request.get(GROUP_INFO, params: { chat_id: chat_id })
|
49
80
|
# 序列化
|
50
|
-
|
51
|
-
|
52
|
-
|
81
|
+
ret = JSON.parse(res.body.readpartial)
|
82
|
+
# 返回数据
|
83
|
+
ret["data"]["members"]
|
53
84
|
end
|
54
85
|
|
55
86
|
# 获取群成员信息
|
@@ -60,17 +91,6 @@ module Feishu
|
|
60
91
|
# member_ids = group_users_content["data"]
|
61
92
|
# end
|
62
93
|
|
63
|
-
def upload_image(image_path)
|
64
|
-
# 读取图片
|
65
|
-
data = HTTP::FormData::File.new image_path
|
66
|
-
|
67
|
-
# 请求后端
|
68
|
-
res = request.post(IMAGE_URL, form: { image_type: "message", image: data })
|
69
|
-
ret = JSON.parse res.body.readpartial
|
70
|
-
# 返回上传后的序列号
|
71
|
-
ret["data"]["image_key"]
|
72
|
-
end
|
73
|
-
|
74
94
|
# 发送文本消息
|
75
95
|
def send_text(open_id, text)
|
76
96
|
# 请求后端
|
@@ -87,40 +107,86 @@ module Feishu
|
|
87
107
|
JSON.parse(res.body.readpartial)
|
88
108
|
end
|
89
109
|
|
90
|
-
#
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
110
|
+
# 发生富文本消息
|
111
|
+
def send_alert(chat_id)
|
112
|
+
data = {
|
113
|
+
"chat_id": chat_id,
|
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
|
150
|
+
|
151
|
+
# 告警恢复提醒
|
152
|
+
def send_recovery_message(title, content)
|
153
|
+
data = {
|
154
|
+
"chat_id": self.chat_id,
|
155
|
+
"msg_type": "post",
|
156
|
+
"content": {
|
157
|
+
"post": {
|
158
|
+
"zh_cn": {
|
159
|
+
"title": title,
|
160
|
+
"content": [
|
161
|
+
[
|
162
|
+
{
|
163
|
+
"tag": "text",
|
164
|
+
"un_escape": true,
|
165
|
+
"text": content
|
166
|
+
},
|
167
|
+
{
|
168
|
+
"tag": "at",
|
169
|
+
"user_id": user_id("15673443571")
|
170
|
+
|
171
|
+
},
|
172
|
+
],
|
173
|
+
[
|
174
|
+
{
|
175
|
+
"tag": "img",
|
176
|
+
"image_key": upload_image("/home/careline/Codes/mojo.png"),
|
177
|
+
"width": 1000,
|
178
|
+
"height": 600
|
179
|
+
}
|
180
|
+
]
|
181
|
+
]
|
182
|
+
}
|
183
|
+
}
|
184
|
+
}
|
185
|
+
}
|
186
|
+
# 请求后端
|
187
|
+
res = request.post(SEND_TEXT, json: data)
|
188
|
+
# 序列化
|
189
|
+
JSON.parse(res.body.readpartial)
|
190
|
+
end
|
125
191
|
end
|
126
192
|
end
|
data/lib/feishu/version.rb
CHANGED
data/lib/feishu-rails.rb
CHANGED
data/lib/tasks/feishu_tasks.rake
CHANGED
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.4
|
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-
|
11
|
+
date: 2022-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|