grape-slack-bot 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,139 @@
1
+ require 'active_support/core_ext/object'
2
+
3
+ module SlackBot
4
+ class Interaction
5
+ SlackViewsReply = Data.define(:callback_id, :view_id)
6
+
7
+ def self.view(klass)
8
+ define_singleton_method(:view_klass) { klass }
9
+ end
10
+
11
+ def self.open_modal(
12
+ trigger_id:,
13
+ payload:,
14
+ class_name:,
15
+ method_name:,
16
+ user:,
17
+ channel_id:,
18
+ config: nil
19
+ )
20
+ callback =
21
+ Callback.create(
22
+ class_name: class_name,
23
+ method_name: method_name,
24
+ user: user,
25
+ channel_id: channel_id,
26
+ config: config
27
+ )
28
+
29
+ view = payload.merge({ type: "modal", callback_id: callback.id })
30
+ response =
31
+ SlackBot::ApiClient.new.views_open(trigger_id: trigger_id, view: view)
32
+
33
+ if !response.ok?
34
+ raise SlackBot::Errors::OpenModalError.new(response.error, data: response.data, payload: payload)
35
+ end
36
+
37
+ view_id = response.data.dig("view", "id")
38
+ callback.update(view_id: view_id) if view_id.present?
39
+
40
+ SlackViewsReply.new(callback.id, view_id)
41
+ end
42
+
43
+ def self.update_modal(
44
+ callback_id: nil,
45
+ view_id:,
46
+ payload:,
47
+ class_name: nil,
48
+ method_name: nil,
49
+ user: nil,
50
+ channel_id: nil,
51
+ config: nil
52
+ )
53
+ callback = Callback.find(callback_id, config: config) if callback_id.present?
54
+ callback ||=
55
+ Callback.create(
56
+ class_name: class_name,
57
+ method_name: method_name,
58
+ user: user,
59
+ channel_id: channel_id,
60
+ config: config
61
+ )
62
+
63
+ view = payload.merge({ type: "modal", callback_id: callback.id })
64
+ response =
65
+ SlackBot::ApiClient.new.views_update(view_id: view_id, view: view)
66
+
67
+ if !response.ok?
68
+ raise SlackBot::Errors::UpdateModalError.new(response.error, data: response.data, payload: payload)
69
+ end
70
+
71
+ view_id = response.data.dig("view", "id")
72
+ callback.update(view_id: view_id) if view_id.present?
73
+
74
+ SlackViewsReply.new(callback.id, view_id)
75
+ end
76
+
77
+ attr_reader :current_user, :params, :callback, :config
78
+ def initialize(current_user: nil, params: nil, callback: nil, config: nil)
79
+ @current_user = current_user
80
+ @params = params
81
+ @callback = callback
82
+ @config = config || SlackBot::Config.current_instance
83
+ end
84
+
85
+ def call
86
+ nil
87
+ end
88
+
89
+ private
90
+
91
+ def interaction_type
92
+ payload["type"]
93
+ end
94
+
95
+ def actions
96
+ payload["actions"]
97
+ end
98
+
99
+ def update_modal(view_name, context: nil)
100
+ return if callback.blank?
101
+
102
+ view_id = payload["view"]["id"]
103
+ args = callback.args
104
+ view =
105
+ self.class.view_klass.new(
106
+ args: args,
107
+ current_user: current_user,
108
+ params: params,
109
+ context: context,
110
+ config: config
111
+ )
112
+ payload = view.send(view_name)
113
+
114
+ self.class.update_modal(
115
+ view_id: view_id,
116
+ payload: payload,
117
+ callback_id: callback.id,
118
+ config: config
119
+ )
120
+ end
121
+
122
+ def update_callback_args(&block)
123
+ return if callback.blank?
124
+ return if actions.blank?
125
+
126
+ if block_given?
127
+ actions.each { |action| instance_exec(action, &block) }
128
+ else
129
+ callback.args.raw_args = actions.first["value"]
130
+ end
131
+
132
+ callback.save
133
+ end
134
+
135
+ def payload
136
+ @payload ||= JSON.parse(params[:payload])
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,10 @@
1
+ module SlackBot
2
+ class MenuOptions
3
+ attr_reader :current_user, :params, :config
4
+ def initialize(current_user:, params:, config: nil)
5
+ @current_user = current_user
6
+ @params = params
7
+ @config = config || SlackBot::Config.current_instance
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,30 @@
1
+ module SlackBot
2
+ class Pager
3
+ DEFAULT_PAGE = 1
4
+ DEFAULT_LIMIT = 10
5
+
6
+ attr_reader :source_cursor, :args, :limit, :page
7
+ def initialize(source_cursor, args:, limit: nil, page: nil)
8
+ @source_cursor = source_cursor
9
+ @args = args
10
+ @limit = limit || @args[:per_page]&.to_i || DEFAULT_LIMIT
11
+ @page = page || @args[:page]&.to_i || DEFAULT_PAGE
12
+ end
13
+
14
+ def total_count
15
+ source_cursor.count
16
+ end
17
+
18
+ def pages_count
19
+ (source_cursor.count.to_f / limit).ceil
20
+ end
21
+
22
+ def offset
23
+ (page - 1) * limit
24
+ end
25
+
26
+ def cursor
27
+ source_cursor.limit(limit).offset(offset)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,53 @@
1
+ require 'active_support/core_ext/hash/indifferent_access'
2
+
3
+ module SlackBot
4
+ class View
5
+ def self.pager_klass
6
+ SlackBot::Pager
7
+ end
8
+
9
+ def self.pager(klass)
10
+ define_singleton_method(:pager_klass) { klass }
11
+ end
12
+
13
+ attr_reader :args, :current_user, :params, :context, :config
14
+ def initialize(current_user:, params:, args: nil, context: nil, config: nil)
15
+ @current_user = current_user
16
+ @params = params
17
+ @config = config || SlackBot::Config.current_instance
18
+
19
+ @args = args
20
+ @context = context.with_indifferent_access if context.is_a?(Hash)
21
+ end
22
+
23
+ def text_modal
24
+ {
25
+ title: {
26
+ type: "plain_text",
27
+ text: context[:title]
28
+ },
29
+ blocks: [
30
+ { type: "section", text: { type: "mrkdwn", text: context[:text] } }
31
+ ]
32
+ }
33
+ end
34
+
35
+ private
36
+
37
+ def current_date
38
+ Date.current
39
+ end
40
+
41
+ def divider_block
42
+ { type: "divider" }
43
+ end
44
+
45
+ def command
46
+ params[:command]
47
+ end
48
+
49
+ def paginate(cursor)
50
+ SlackBot::Pager.new(cursor, args: args)
51
+ end
52
+ end
53
+ end
data/lib/slack_bot.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'slack_bot/dev_console'
2
+
3
+ require 'slack_bot/config'
4
+ require 'slack_bot/errors'
5
+
6
+ require 'slack_bot/args'
7
+ require 'slack_bot/api_client'
8
+
9
+ require 'slack_bot/callback_storage'
10
+ require 'slack_bot/callback'
11
+
12
+ require 'slack_bot/command'
13
+ require 'slack_bot/interaction'
14
+ require 'slack_bot/event'
15
+ require 'slack_bot/menu_options'
16
+ require 'slack_bot/view'
17
+
18
+ require 'slack_bot/pager'
19
+
20
+ require 'slack_bot/grape_extension'
21
+
22
+ module SlackBot
23
+ VERSION = '1.0.0'.freeze
24
+ end