feishu-rails 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c9ffbd5349b1725eff7ac29f5822779d3566dd3a3dca6bb7577845d5b62015cf
4
- data.tar.gz: 787d901f41edd64c8b5e19e5f2b2aa27066fa35840a356d055a994bfbee12ad5
3
+ metadata.gz: 257e57680f67a6b05f24836a5f765fac25c6d6effaa4ae7e88843bc2df4e861c
4
+ data.tar.gz: 0f81c2bdd6e4a3cf560fa530b78366e11d2df0f36208f159afc4736724fb3975
5
5
  SHA512:
6
- metadata.gz: 636d3abd7ce1658069e20f818fd176d48311900d5e559a2281ba460fd2c9a349d5cb553820ece62e7367875684758c0c01cb6d182161329ad746848b776b75ee
7
- data.tar.gz: bb5dbb5d2049ba3fec04bcccbe0a4723b7072b8319bd65c098a74edc1ee1f7acdb6ff02ed0888a71e04d80a5bf29fbb328df0c194222006de1068dd3ef3637bf
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,20 +1,25 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/concern"
2
4
 
3
5
  module Feishu
4
6
  extend ActiveSupport::Concern
5
7
  module Connector
6
8
  # 定义常用的 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"
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"
13
13
  MESSAGE_URL = "https://open.feishu.cn/open-apis/im/v1/messages"
14
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
+
15
19
  # 获取飞书 TOKEN 并缓存
16
20
  def access_token
17
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 })
18
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
@@ -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,18 +91,6 @@ module Feishu
60
91
  # member_ids = group_users_content["data"]
61
92
  # end
62
93
 
63
- # 上传图片到飞书后台
64
- def upload_image(image_path)
65
- # 读取图片
66
- data = HTTP::FormData::File.new image_path
67
-
68
- # 请求后端
69
- res = request.post(IMAGE_URL, form: { image_type: "message", image: data })
70
- ret = JSON.parse res.body.readpartial
71
- # 返回上传后的序列号
72
- ret["data"]["image_key"]
73
- end
74
-
75
94
  # 发送文本消息
76
95
  def send_text(open_id, text)
77
96
  # 请求后端
@@ -89,40 +108,85 @@ module Feishu
89
108
  end
90
109
 
91
110
  # 发生富文本消息
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
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
127
191
  end
128
192
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Feishu
2
- VERSION = "0.1.3"
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.3
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