feishu_bot 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 43b894eaaac4339c249d125218118d333b0b5926da78bb39da07c559be1c93ce
4
+ data.tar.gz: b14d83e1c50d9eccd33f25fbd44248f8b2ccbee801dcfadacafd0cf6451a2bc6
5
+ SHA512:
6
+ metadata.gz: 728aaa72792e7850332a6acdd7459c130ba18327f9175a36e7b16a02de73cc7971a94c691233aec32af541af85818fd8eb66b1f86596308c7a63323f5bd36b24
7
+ data.tar.gz: 4b6a7fd0075d8890134b0ff991eae981f9954e9e0160db16d178af4d75e31bd75d9901b1739581fa8c6a9cd804549a08aaa438fb0ed66853186ec9a88c16c76b
@@ -0,0 +1,20 @@
1
+ Copyright 2020 anke1460
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ # FeishuBot
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'feishu_bot'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+
18
+ $ rails feishu_bot:install:migrations
19
+ ```
20
+
21
+ add feishu.rb in config/initializers
22
+ ```
23
+ # FeishuBot.configure do |config|
24
+ # config.app_id = ''
25
+ # config.app_secret = ''
26
+ # end
27
+ ```
28
+ Or install it yourself as:
29
+ ```bash
30
+ $ gem install feishu_bot
31
+ ```
32
+
33
+ ## Contributing
34
+ Contribution directions go here.
35
+
36
+ ## License
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'FeishuBot'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/feishu_bot .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,5 @@
1
+ module FeishuBot
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+ require_dependency "feishu_bot/application_controller"
2
+
3
+ module FeishuBot
4
+ class FeishuMessagesController < ApplicationController
5
+ skip_before_action :verify_authenticity_token
6
+
7
+ def create
8
+ return render json: {challenge: params[:challenge]} if params[:challenge]
9
+
10
+ if params[:event][:type] == 'message'
11
+ user = FeishuUser.find_or_initialize_by(uid: params[:event][:open_id])
12
+ user.save
13
+ message = FeishuMessage.find_or_initialize_by(uuid: params[:uuid]) do |d|
14
+ d.uid = params[:event][:open_id]
15
+ d.feishu_user_id = user.id
16
+ if params[:event][:msg_type] == 'image'
17
+ d.key = params[:event][:image_key]
18
+ elsif params[:event][:msg_type] == 'file'
19
+ d.key = params[:event][:file_key]
20
+ end
21
+ d.clazz = params[:event][:msg_type]
22
+ d.content = params[:event][:text]
23
+ end
24
+ message.save
25
+ end
26
+
27
+ render json: {success: true}
28
+ end
29
+ end
30
+ end
31
+
32
+
@@ -0,0 +1,4 @@
1
+ module FeishuBot
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module FeishuBot
2
+ module FeishuMessagesHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module FeishuBot
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module FeishuBot
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module FeishuBot
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module FeishuBot
2
+ class FeishuMessage < ApplicationRecord
3
+ enum clazz: {
4
+ 'text': 1,
5
+ 'image': 2,
6
+ 'file': 3
7
+ }
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module FeishuBot
2
+ class FeishuUser < ApplicationRecord
3
+ end
4
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Feishu bot</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "feishu_bot/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
@@ -0,0 +1,4 @@
1
+ # FeishuBot.configure do |config|
2
+ # config.app_id = 'cli_9f685879b330d00c'
3
+ # config.app_secret = 'VH4qgPfAqxYSx3WBl5aWoc2ezZTKOrVA'
4
+ # end
@@ -0,0 +1,5 @@
1
+ FeishuBot::Engine.routes.draw do
2
+
3
+ resources :feishu_messages do
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ class CreateFeishuBotFeishuUsers < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :feishu_bot_feishu_users do |t|
4
+ t.string :uid
5
+ t.string :name
6
+
7
+ t.timestamps
8
+ end
9
+ add_index :feishu_bot_feishu_users, :uid
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ class CreateFeishuBotFeishuMessages < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :feishu_bot_feishu_messages do |t|
4
+ t.string :content
5
+ t.integer :clazz
6
+ t.integer :feishu_user_id
7
+ t.string :key
8
+ t.string :uid
9
+ t.string :uuid
10
+ t.boolean :is_read, default: false
11
+
12
+ t.timestamps
13
+ end
14
+ add_index :feishu_bot_feishu_messages, :uuid
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ require "feishu_bot/engine"
2
+ require "feishu_bot/config"
3
+ require "feishu_bot/api"
4
+
5
+ require 'http'
6
+
7
+ module FeishuBot
8
+ # Your code goes here...
9
+ class << self
10
+ def configure(&block)
11
+ Config.configure(&block)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,68 @@
1
+ module FeishuBot
2
+ class << self
3
+ def access_token
4
+ Rails.cache.fetch("feishu_access_token", expires_in: 2.hours) do
5
+ res = HTTP.post("https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/", :json => { :app_id => Config.app_id, :app_secret=> Config.app_secret })
6
+ JSON.parse(res.body.readpartial)['tenant_access_token']
7
+ end
8
+ end
9
+
10
+ def batch_user_info(open_ids)
11
+ res = request.get('https://open.feishu.cn/open-apis/contact/v1/user/batch_get', params: {:open_ids => open_ids})
12
+ users = JSON.parse(res.body.readpartial)
13
+
14
+ users['data']
15
+ end
16
+
17
+ # 获取机器人所在的群列表
18
+ def group_list(page_size = 200, page = nil)
19
+ res = request.get('https://open.feishu.cn/open-apis/chat/v4/list', params: {page_size: page_size, page_token: page})
20
+ group_content = JSON.parse(res.body.readpartial)
21
+
22
+ group_content['data']
23
+ end
24
+
25
+
26
+ # 获取群信息
27
+ def group_info(chat_id)
28
+ res = request.get("https://open.feishu.cn/open-apis/chat/v4/info", :params => {:chat_id=> chat_id})
29
+ group_users_content = JSON.parse(res.body.readpartial)
30
+
31
+ member_ids = group_users_content["data"]["members"]
32
+ end
33
+
34
+ # 获取群成员信息
35
+ # def member_list(chat_id)
36
+ # res = request.get("https://open.feishu.cn/open-apis/chat/v4/members", :params => {:chat_id=> chat_id})
37
+ # group_users_content = JSON.parse(res.body.readpartial)
38
+
39
+ # member_ids = group_users_content["data"]
40
+ # end
41
+
42
+ def image_data(key)
43
+ res = request.get('https://open.feishu.cn/open-apis/image/v4/get', params: {image_key: key})
44
+ res.body.readpartial
45
+ end
46
+
47
+ def send_text(open_id, text)
48
+ res = request.post('https://open.feishu.cn/open-apis/message/v4/send', json: {
49
+ open_id: open_id,
50
+ msg_type: 'text',
51
+ content: {
52
+ text: text
53
+ }
54
+ })
55
+ end
56
+
57
+
58
+ def send_text(open_id, text)
59
+ send_msg = request.post("https://open.feishu.cn/open-apis/message/v4/send/", :json => { :open_id => open_id, :msg_type => 'text', :content => { :text=> text} })
60
+ send_res = JSON.parse(send_msg.body.readpartial)
61
+ end
62
+
63
+ def request
64
+ HTTP.headers(:Authorization=> "Bearer #{self.access_token}")
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,12 @@
1
+ module FeishuBot
2
+ class Config
3
+ class << self
4
+ attr_accessor :app_id, :app_secret
5
+
6
+ def configure
7
+ yield self
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module FeishuBot
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace FeishuBot
4
+
5
+ initializer('feishu', after: :load_config_initializers) do |app|
6
+ Rails.application.routes.prepend do
7
+ mount FeishuBot::Engine, at: '/feishu'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module FeishuBot
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :feishu_bot do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: feishu_bot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - anke1460
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-12-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: http
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 4.4.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 4.4.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Description of FeishuBot.
56
+ email:
57
+ - zuosjob@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - app/assets/config/feishu_bot_manifest.js
66
+ - app/assets/stylesheets/feishu_bot/application.css
67
+ - app/assets/stylesheets/feishu_bot/feishu_messages.css
68
+ - app/controllers/feishu_bot/application_controller.rb
69
+ - app/controllers/feishu_bot/feishu_messages_controller.rb
70
+ - app/helpers/feishu_bot/application_helper.rb
71
+ - app/helpers/feishu_bot/feishu_messages_helper.rb
72
+ - app/jobs/feishu_bot/application_job.rb
73
+ - app/mailers/feishu_bot/application_mailer.rb
74
+ - app/models/feishu_bot/application_record.rb
75
+ - app/models/feishu_bot/feishu_message.rb
76
+ - app/models/feishu_bot/feishu_user.rb
77
+ - app/views/layouts/feishu_bot/application.html.erb
78
+ - config/initializers/config.rb
79
+ - config/routes.rb
80
+ - db/migrate/20201225092449_create_feishu_bot_feishu_users.rb
81
+ - db/migrate/20201225092452_create_feishu_bot_feishu_messages.rb
82
+ - lib/feishu_bot.rb
83
+ - lib/feishu_bot/api.rb
84
+ - lib/feishu_bot/config.rb
85
+ - lib/feishu_bot/engine.rb
86
+ - lib/feishu_bot/version.rb
87
+ - lib/tasks/feishu_bot_tasks.rake
88
+ homepage: https://github.com/anke1460/feishu_bot
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.7.7
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Summary of FeishuBot.
112
+ test_files: []