13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/slack_profile/cli.rb', line 13
def interactive
client = get_client
prompt = TTY::Prompt.new
custom_fields = {}
begin
puts "🔄 Fetching available fields...\n\n"
custom_fields = client.get_team_profile
rescue => e
puts "⚠️ Could not fetch custom fields: #{e.message}\n\n"
end
loop do
puts "🚀 Slack Profile CLI - Interactive Mode\n\n"
action = prompt.select("What would you like to do?", filter: true, per_page: 10) do ||
.choice "Get user profile", :get
.choice "Set a single field for one user", :single
.choice "Set a single field for multiple users", :batch_single
.choice "Set multiple fields for one user", :multiple
.choice "Set multiple fields for multiple users", :batch_multiple
.choice "List available fields", :list
.choice "Exit", :exit
end
break if action == :exit
case action
when :get
user_id = prompt.ask("Enter the Slack user ID:") do |q|
q.validate(/^U\w+/, "User ID should start with 'U'")
q.required true
end
profile = client.get_profile(user_id)
puts "\n📋 Profile for user #{user_id}:"
puts JSON.pretty_generate(profile)
puts ""
when :list
puts "\n📋 Available profile fields:"
puts "\nStandard fields:"
standard_fields.each { |field| puts " • #{field}" }
if custom_fields.any?
puts "\nCustom fields:"
custom_fields.each do |id, field|
puts " • #{id} - #{field['label']} (#{field['type']})"
end
else
puts "\n📝 No custom fields configured."
end
puts ""
when :single, :batch_single
user_ids = if action == :batch_single
prompt.ask("Enter Slack user IDs (comma-separated):") do |q|
q.required true
end.split(",").map(&:strip)
else
[prompt.ask("Enter the Slack user ID:") do |q|
q.validate(/^U\w+/, "User ID should start with 'U'")
q.required true
end]
end
field_choices = standard_fields.map { |f| { name: f, value: f } }
custom_fields.each do |id, field|
field_choices << { name: "#{field['label']} (#{id})", value: id }
end
field_choices << { name: "Enter field ID manually", value: :custom }
field_name = prompt.select("Which field would you like to update?", field_choices, filter: true, per_page: 15)
if field_name == :custom
field_name = prompt.ask("Enter the field name or ID:")
end
value = prompt.ask("Enter the value for #{field_name} (leave empty to clear):", default: "")
if action == :single
client.set_single_field(user_ids[0], field_name, value)
puts "✅ Successfully updated #{field_name} for user #{user_ids[0]}\n\n"
else
puts "🚀 Updating #{field_name} for #{user_ids.length} users..."
success_count = 0
user_ids.each do |user_id|
begin
client.set_single_field(user_id, field_name, value)
puts "✅ Updated #{field_name} for user #{user_id}"
success_count += 1
rescue => e
puts "❌ Failed to update #{field_name} for user #{user_id}: #{e.message}"
end
end
puts "\n📊 Summary: #{success_count}/#{user_ids.length} users updated successfully\n\n"
end
when :multiple, :batch_multiple
user_ids = if action == :batch_multiple
prompt.ask("Enter Slack user IDs (comma-separated):") do |q|
q.required true
end.split(",").map(&:strip)
else
[prompt.ask("Enter the Slack user ID:") do |q|
q.validate(/^U\w+/, "User ID should start with 'U'")
q.required true
end]
end
puts "\n📝 You can set multiple fields using JSON format."
puts 'Example: {"first_name":"John","last_name":"Doe","title":"Developer"}'
profile_json = prompt.ask("Enter profile data as JSON:") do |q|
q.required true
q.validate ->(input) { JSON.parse(input); true rescue false }, "Invalid JSON format"
end
profile = JSON.parse(profile_json)
if action == :multiple
client.set_profile(user_ids[0], profile)
puts "✅ Successfully updated profile for user #{user_ids[0]}\n\n"
else
puts "🚀 Updating profiles for #{user_ids.length} users..."
success_count = 0
user_ids.each do |user_id|
begin
client.set_profile(user_id, profile)
puts "✅ Updated profile for user #{user_id}"
success_count += 1
rescue => e
puts "❌ Failed to update profile for user #{user_id}: #{e.message}"
end
end
puts "\n📊 Summary: #{success_count}/#{user_ids.length} users updated successfully\n\n"
end
end
end
puts "👋 Goodbye!"
rescue TTY::Reader::InputInterrupt
puts "\n\n👋 Goodbye!"
exit 0
end
|