def message_handler(data): event = data["event"] if event.get("bot_id") or event.get("subtype") == "bot_message": return None user_messages_bot = event["channel_type"] == "im" if user_messages_bot: params = {"workspace_id": data["team_id"]} installation = SlackInstallation.query.filter_by(**params).first() bot_recieves_configuration_information = ( installation.state == "config_requested" and installation.authorizing_user_id == event["user"]) if bot_recieves_configuration_information: entered_time = event["text"] workflow = SlackInstallationOnboardUserWorkflow( installation, payload=entered_time) workflow.advance() return None logger.info("[Busy Beaver] Slack -- Unknown command") slack = SlackClient(installation.bot_access_token) slack.post_message(HELP_TEXT, channel=data["event"]["channel"]) return None
def fetch_github_summary_post_to_slack(installation_id, boundary_dt): slack_installation = SlackInstallation.query.get(installation_id) channel = slack_installation.github_summary_config.channel slack = SlackClient(slack_installation.bot_access_token) channel_members = slack.get_channel_members(channel) users: List[GitHubSummaryUser] = GitHubSummaryUser.query.filter( and_( GitHubSummaryUser.installation_id == installation_id, GitHubSummaryUser.slack_id.in_(channel_members), GitHubSummaryUser.github_username.isnot(None), )).all() random.shuffle(users) message = "" num_users = len(users) for idx, user in enumerate(users): logger.info("[Busy Beaver] Compiling stats for {0}".format(user)) user_events = GitHubUserEvents(user, boundary_dt) message += user_events.generate_summary_text() set_task_progress(idx / (num_users + 1) * 100) if not message: message = ( '"If code falls outside version control, and no one is around to read it, ' 'does it make a sound?" - Zax Rosenberg') slack.post_message(message=message, channel=channel, unfurl_links=False, unfurl_media=False) set_task_progress(100)
def save_configuration(installation: SlackInstallation, time_to_post: time): slack = SlackClient(installation.bot_access_token) user_id = installation.authorizing_user_id tz = slack.get_user_timezone(user_id) github_summary_config = installation.github_summary_config github_summary_config.time_to_post = str(time_to_post) github_summary_config.timezone_info = tz._asdict() db.session.add(github_summary_config) db.session.commit() channel = github_summary_config.channel slack.dm(ACTIVE_MESSAGE.format(time=str(time_to_post), channel=channel), user_id=user_id)
def member_joined_channel_handler(data): workspace_id = data["team_id"] user_id = data["event"]["user"] channel = data["event"]["channel"] installation = SlackInstallation.query.filter_by( workspace_id=workspace_id).first() bot_invited_to_channel = user_id == installation.bot_user_id if bot_invited_to_channel: github_summary_configured = installation.github_summary_config if not github_summary_configured: github_summary_config = GitHubSummaryConfiguration(channel=channel) github_summary_config.slack_installation = installation db.session.add(github_summary_config) db.session.commit() db.session.refresh(installation) workflow = SlackInstallationOnboardUserWorkflow(installation) workflow.advance() else: # bot was invited to a different channel # TODO add to table of channels for that workspace pass return None user_joins_github_summary_channel = ( installation.github_summary_config.channel == channel and installation.state == "active") if user_joins_github_summary_channel: slack = SlackClient(installation.bot_access_token) slack.post_ephemeral_message( GITHUB_SUMMARY_CHANNEL_JOIN_MESSAGE.format(channel=channel), channel=channel, user_id=user_id, ) return None
def app_home_handler(data): """Display App Home Currently we do show first-time viewers a separate screen... should we? """ logger.info("app_home_opened Event", extra=data) workspace_id = data["team_id"] user_id = data["event"]["user"] tab_opened = data["event"]["tab"] if tab_opened != "home": return None installation = SlackInstallation.query.filter_by( workspace_id=workspace_id).first() params = {"installation_id": installation.id, "slack_id": user_id} user = SlackAppHomeOpened.query.filter_by(**params).first() if user: user.count += 1 else: user = SlackAppHomeOpened(**params) db.session.add(user) db.session.commit() # TODO would be smart to use the state machine to figure out what to post github_summary_configured = installation.github_summary_config if github_summary_configured: channel = installation.github_summary_config.channel if workspace_id in FULL_INSTALLATION_WORKSPACE_IDS: app_home = AppHome(channel=channel, meetup_group=MEETUP_GROUP_NAME) else: app_home = AppHome(channel=channel) else: app_home = AppHome() slack = SlackClient(installation.bot_access_token) slack.display_app_home(user_id, view=app_home.to_dict()) return None
YouTubeClient, ) from busy_beaver.extensions import db, rq # noqa from busy_beaver.models import * # noqa # create flask application context app = create_app() ctx = app.app_context() ctx.push() # configure adapters OAUTH_TOKEN = os.getenv("GITHUB_OAUTH_TOKEN") github = GitHubClient(OAUTH_TOKEN) SLACK_TOKEN = os.getenv("SLACK_BOTUSER_OAUTH_TOKEN") slack = SlackClient(SLACK_TOKEN) TWITTER_CONSUMER_KEY = os.getenv("TWITTER_CONSUMER_KEY") TWITTER_CONSUMER_SECRET = os.getenv("TWITTER_CONSUMER_SECRET") TWITTER_ACCESS_TOKEN = os.getenv("TWITTER_ACCESS_TOKEN") TWITTER_ACCESS_TOKEN_SECRET = os.getenv("TWITTER_ACCESS_TOKEN_SECRET") twitter = TwitterClient( TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET, ) YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY") youtube = YouTubeClient(api_key=YOUTUBE_API_KEY)
def send_configuration_message(installation: SlackInstallation): slack = SlackClient(installation.bot_access_token) user_id = installation.authorizing_user_id channel = installation.github_summary_config.channel slack.dm(CONFIRMED_MESSAGE.format(channel=channel), user_id=user_id)
def send_welcome_message(installation: SlackInstallation): slack = SlackClient(installation.bot_access_token) user_id = installation.authorizing_user_id slack.dm(ONBOARDING_MESSAGE.format(slack_id=user_id), user_id=user_id)