feishu-rails 0.1.4 → 0.1.5

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: 257e57680f67a6b05f24836a5f765fac25c6d6effaa4ae7e88843bc2df4e861c
4
- data.tar.gz: 0f81c2bdd6e4a3cf560fa530b78366e11d2df0f36208f159afc4736724fb3975
3
+ metadata.gz: 7dc4e1b6b4f87a681cf692808792450bdf9bb47fc81cfcefe34161e481c6f8db
4
+ data.tar.gz: 0e3a7e30aa7c294e1b61d3419e0d04c41c091c8982124bdfbadedc14e5504c08
5
5
  SHA512:
6
- metadata.gz: d4aedc5c5524d3552cb99789a27513e7693eda443aa10f2ac7f19b6118024891b80019aa37a6eaa461750ca20bbea93cb331a561be8914f88f0794e0e45d102a
7
- data.tar.gz: 152aec5445a385cc3f5fc543616c14fac8b459da4085fb6f6f001711da0d2055fc82766f561488f4dd7e31f1c771ffd483e48094357899377d6343816cba61b8
6
+ metadata.gz: cc71e1a0b20353a715308e50167f9e3424260ca35904beb9aad2f457ec82e15131b0c762edd3afef96dd44181b93e88e1ed5783e94262ff8ec182608dfd5a41f
7
+ data.tar.gz: 72af82dd959521f7abc12761d5f68383c3da8616bcc7ef480f1ead2f5aa9946943dedbe11e002b52bd320636fc0d096ac8a3c37e1ef7d1acd5dc957d5067fe05
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).
@@ -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"
@@ -18,9 +19,9 @@ module Feishu
18
19
 
19
20
  # 获取飞书 TOKEN 并缓存
20
21
  def access_token
21
- Rails.cache.fetch("feishu_token", expires_in: 2.hours) do
22
+ Rails.cache.fetch("feishu_token", expires_in: 30.minutes) do
22
23
  # 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
+ res = HTTP.post(AUTH_URL, json: { app_id: Feishu.app_id, app_secret: Feishu.app_secret })
24
25
  JSON.parse(res.body.readpartial)["tenant_access_token"]
25
26
  end
26
27
  end
@@ -37,18 +38,20 @@ module Feishu
37
38
  res = request.get(url)
38
39
  # 序列化
39
40
  ret = JSON.parse(res.body.readpartial)
41
+
40
42
  # 返回数据
41
43
  ret["data"]["mobile_users"].try(:[], mobile).try(:[], 0).try(:[], "user_id")
42
44
  end
43
45
 
44
- # 获取用户 CHAT_ID
45
- def chat_id
46
+ # 获取群组的 chat_id
47
+ def chat_id(name)
46
48
  # 请求后端
47
49
  res = request.get(CHAT_ID)
48
50
  # 序列化
49
51
  ret = JSON.parse(res.body.readpartial)
52
+
50
53
  # 返回数据
51
- ret["data"]["groups"].try(:[], 0).try(:[], "chat_id")
54
+ ret["data"]["groups"].select { |i| i["name"] == name }.try(:[], 0).try(:[], "chat_id")
52
55
  end
53
56
 
54
57
  # 上传图片到飞书后台
@@ -59,6 +62,7 @@ module Feishu
59
62
  # 请求后端
60
63
  res = request.post(IMAGE_ID, form: { image_type: "message", image: data })
61
64
  ret = JSON.parse res.body.readpartial
65
+
62
66
  # 返回上传后的序列号
63
67
  ret["data"]["image_key"]
64
68
  end
@@ -74,7 +78,7 @@ module Feishu
74
78
  end
75
79
 
76
80
  # 获取群信息
77
- def group_info(chat_id)
81
+ def group_members(chat_id)
78
82
  # 请求后端
79
83
  res = request.get(GROUP_INFO, params: { chat_id: chat_id })
80
84
  # 序列化
@@ -83,14 +87,6 @@ module Feishu
83
87
  ret["data"]["members"]
84
88
  end
85
89
 
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
90
  # 发送文本消息
95
91
  def send_text(open_id, text)
96
92
  # 请求后端
@@ -100,93 +96,55 @@ module Feishu
100
96
  end
101
97
 
102
98
  # 使用推荐的消息接口
103
- def send_message(receive_type, receive_id, msg_type, content)
99
+ def send_message(receive_type = "chat_id", receive_id, msg_type, content)
104
100
  # 请求后端
105
101
  res = request.post(MESSAGE_URL, params: { receive_id_type: receive_type }, json: { receive_id: receive_id, msg_type: msg_type, content: content })
106
102
  # 序列化
107
103
  JSON.parse(res.body.readpartial)
108
104
  end
109
105
 
110
- # 发生富文本消息
111
- def send_alert(chat_id)
106
+ # 向特定群组发生消息
107
+ def send_alert(chat_id, title, content, image_path)
112
108
  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
- ]
109
+ chat_id: chat_id,
110
+ msg_type: "post",
111
+ content: {
112
+ post: {
113
+ zh_cn: {
114
+ title: title,
115
+ content: [
116
+ [
117
+ {
118
+ tag: "text",
119
+ un_escape: true,
120
+ text: content
121
+ },
122
+ {
123
+ tag: "at",
124
+ user_id: user_id("15673443571")
125
+
126
+ },
127
+ ],
128
+ [
129
+ {
130
+ tag: "img",
131
+ image_key: upload_image(image_path),
132
+ width: 1000,
133
+ height: 600
134
+ }
140
135
  ]
136
+ ]
141
137
  }
142
138
  }
143
139
  }
144
140
  }
145
141
  # 请求后端
146
142
  res = request.post(SEND_TEXT, json: data)
147
- # 序列化
148
- JSON.parse(res.body.readpartial)
149
- end
150
143
 
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
144
  # 序列化
189
- JSON.parse(res.body.readpartial)
145
+ JSON.parse(res.body.readpartial).to_query
146
+
147
+ alias_method :feishu_alert, :send_alert
190
148
  end
191
149
  end
192
150
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Feishu
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.5"
5
5
  end
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
- def configure(&block)
10
- Config.configure(&block)
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
4
+ version: 0.1.5
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-28 00:00:00.000000000 Z
11
+ date: 2022-04-03 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,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Feishu
4
- class Config
5
- class << self
6
- attr_accessor :app_id, :app_secret, :encrypt_key
7
-
8
- def configure
9
- yield self
10
- end
11
- end
12
- end
13
- end