Created
November 2, 2022 01:14
-
-
Save Altonymous/96fb6c07294b86de0ea343ce007a4dbe to your computer and use it in GitHub Desktop.
slack-ruby-bot-server notes for Team's not getting extended.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# app/controllers/connect_controller.rb | |
# Handles connecting a Slackster User to a Slack Team | |
class ConnectController < ApplicationController | |
def index | |
return unless params&.key?(:code) | |
client = Slack::Web::Client.new | |
unless ENV.key?('SLACK_CLIENT_ID') && ENV.key?('SLACK_CLIENT_SECRET') | |
raise 'Missing SLACK_CLIENT_ID or SLACK_CLIENT_SECRET.' | |
end | |
options = { | |
client_id: ENV['SLACK_CLIENT_ID'], | |
client_secret: ENV['SLACK_CLIENT_SECRET'], | |
code: params[:code] | |
} | |
rc = client.send(SlackRubyBotServer.config.oauth_access_method, options) | |
activated_user_access_token = rc.authed_user.access_token | |
token = rc.access_token | |
activated_user_id = rc.authed_user&.id | |
bot_user_id = rc.bot_user_id | |
team_id = rc.team&.id | |
name = rc.team&.name | |
oauth_scope = rc.scope | |
oauth_version = ::SlackRubyBotServer::Config.oauth_version | |
team = Team.new( | |
token:, | |
oauth_version:, | |
oauth_scope:, | |
team_id:, | |
name:, | |
activated_user_id:, | |
activated_user_access_token:, | |
bot_user_id: | |
) | |
# If I call team.save! here it works fine. | |
# team.save! | |
slack_client = Slack::Web::Client.new(token: team.token) | |
response = slack_client.conversations_list(exclude_archived: true, types: 'public_channel,private_channel') | |
channels = response[:channels] | |
channels.each do |channel| | |
team.channels << Channel.new( | |
channel_id: channel[:id], | |
name: channel[:name], | |
creator: channel[:creator], | |
private: channel[:is_private] | |
# TODO: topic & purpose as new models | |
) | |
end | |
response = slack_client.users_list(include_locale: true) | |
slackers = response[:members] | |
slackers.each do |slacker| | |
team.slackers << Slacker.new( | |
slacker_id: slacker[:id], | |
name: slacker[:name], | |
real_name: slacker[:real_name], | |
tz: slacker[:tz], | |
admin: slacker[:is_admin], | |
owner: slacker[:is_owner], | |
primary_owner: slacker[:primary_owner], | |
has_2fa: slacker[:has_2fa] | |
# TODO: create profile as a new model | |
) unless slacker[:deleted] || slacker[:is_bot] || slacker[:is_app_user] | |
end | |
team.save! | |
{ team: } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
undefined method `channels' for #<Team _id: <redacted>, created_at: nil, updated_at: nil, team_id: "<redacted>", name: "<redacted>", domain: nil, token: "<redacted>", oauth_scope: "channels:read,users:read,groups:read,chat:write,incoming-webhook,commands,pins:read,mpim:read,files:read,channels:history,reactions:read,chat:write.public,users:read.email", oauth_version: "v2", active: true, bot_user_id: "<redacted>", activated_user_id: "<redacted>", activated_user_access_token: "<redacted>"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# app/models/team.rb | |
# Extends Slack Ruby Bot Server Team Model | |
class Team | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
has_many :channels | |
has_many :slackers | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment