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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2030f453ffa2e384f6d766656948f31f4d021c2cb96a6da5643115f09f8297f
4
- data.tar.gz: 4eea7d97ec5ac0a7ea3b20669b64fa8af4e47bb2d2edbe96ea5a9ba87dc72d25
3
+ metadata.gz: 257e57680f67a6b05f24836a5f765fac25c6d6effaa4ae7e88843bc2df4e861c
4
+ data.tar.gz: 0f81c2bdd6e4a3cf560fa530b78366e11d2df0f36208f159afc4736724fb3975
5
5
  SHA512:
6
- metadata.gz: b84e308a70eaf69998a4af529a128f036d9a1d643e63c1866e4c978f1d075ad247c779731b4b3b18d4dd7624fa944377cb1f7c3974c1063e53560529397c39ce
7
- data.tar.gz: 8dbdee672500a5faf4987fdc41da32c13d2ab4b6afa5108d38b70af17087f9c07af946ea7f8ca8fa3c3db860a4c164e20b41cc8394c1a1dc4f239df6f6a98e40
6
+ metadata.gz: d4aedc5c5524d3552cb99789a27513e7693eda443aa10f2ac7f19b6118024891b80019aa37a6eaa461750ca20bbea93cb331a561be8914f88f0794e0e45d102a
7
+ data.tar.gz: 152aec5445a385cc3f5fc543616c14fac8b459da4085fb6f6f001711da0d2055fc82766f561488f4dd7e31f1c771ffd483e48094357899377d6343816cba61b8
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"
data/lib/feishu/config.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Feishu
2
4
  class Config
3
5
  class << self
@@ -1,21 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
1
5
  module Feishu
2
- class << self
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
- USER_INFO = "https://open.feishu.cn/open-apis/contact/v1/user/batch_get"
6
- GROUP_LIST = "https://open.feishu.cn/open-apis/chat/v4/list"
7
- GROUP_INFO = "https://open.feishu.cn/open-apis/chat/v4/info"
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
- app_id = "cli_a2b7a1fa17b8d00c"
16
- app_secret = "9FIkmECaknhRryyWCH2bngErcbbvQg4G"
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
- def batch_user_info(open_ids)
29
- res = request.get(USER_INFO, params: { open_ids: open_ids })
30
- users = JSON.parse(res.body.readpartial)
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
- users["data"]
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 = 200, page = nil)
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
- group_content = JSON.parse(res.body.readpartial)
41
-
42
- group_content["data"]
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
- group_users_content = JSON.parse(res.body.readpartial)
51
-
52
- group_users_content["data"]["members"]
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
- # def send_alert(chat_id)
91
- # data = {
92
- # "chat_id": chat_id,
93
- # "msg_type": "post",
94
- # "content": {
95
- # "post": {
96
- # "zh_cn": {
97
- # "title": title,
98
- # "content": [
99
- # [
100
- # {
101
- # "tag": "text",
102
- # "un_escape": True,
103
- # "text": content
104
- # },
105
- # {
106
- # "tag": "at",
107
- # "user_id": self.user_id
108
- #
109
- # },
110
- # ],
111
- # [
112
- # {
113
- # "tag": "img",
114
- # "image_key": self.image_key,
115
- # "width": 1000,
116
- # "height": 600
117
- # }
118
- # ]
119
- # ]
120
- # }
121
- # }
122
- # }
123
- # }
124
- # end
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Feishu
2
- VERSION = "0.1.1"
4
+ VERSION = "0.1.4"
3
5
  end
data/lib/feishu-rails.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "feishu/version"
2
4
  require "feishu/config"
3
5
  require "feishu/connector"
@@ -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.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-26 00:00:00.000000000 Z
11
+ date: 2022-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails