def _post_to_slack(channel_name, tweets, twitter_username): """Twitter Slack app unfurls URLs in Slack to show tweet details""" url = "https://twitter.com/{username}/statuses/{id}" for tweet in tweets: tweet_url = url.format(username=twitter_username, id=tweet.id) slack.post_message(tweet_url, channel=channel_name) kv_store.put_int(LAST_TWEET_KEY, tweet.id)
def save_and_post_video(self, video): video_title = video["snippet"]["title"] video_id = video["id"]["videoId"] video_url = f"https://www.youtube.com/watch?v={video_id}" msg = f"[Busy Beaver] YouTube Video Poll -- posting {video_title}" logger.info(msg) youtube_video = YouTubeVideo( youtube_id=video_id, title=video_title, published_at=video["snippet"]["publishedAt"], description=video["snippet"]["description"], ) db.session.add(youtube_video) db.session.commit() slack_msg = f"A new video has been released: {video_url}" slack.post_message(slack_msg, channel=self.channel)
def post(self): data = request.json logger.info("[Busy Beaver] Received event from Slack", extra={"req_json": data}) verification_request = data["type"] == "url_verification" if verification_request: logger.info("[Busy Beaver] Slack -- API Verification") return jsonify({"challenge": data["challenge"]}) # bot can see its own DMs event = data["event"] msg_from_bot = event.get("subtype") == "bot_message" if event["type"] == "message" and msg_from_bot: return jsonify(None) dm_to_bot = event["channel_type"] == "im" if event["type"] == "message" and dm_to_bot: slack.post_message(HELP_TEXT, channel_id=event["channel"]) return jsonify(None)
def fetch_github_summary_post_to_slack(channel_name, boundary_dt): channel_info = slack.get_channel_info(channel_name) users: List[User] = User.query.filter( and_(User.slack_id.in_(channel_info.members), User.github_username.isnot(None))).all() 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_id=channel_info.id) set_task_progress(100)
def reply_to_user_with_github_login_link(message): """Todo break this up... Research spike to find a good abstraction""" chat_text = str.lower(message["text"]) slack_id = message["user"] channel_id = message["channel"] if chat_text not in ALL_LINK_COMMANDS: logger.info("[Busy-Beaver] Unknown command") slack.post_message(UNKNOWN_COMMAND, channel_id=channel_id) return user_record = User.query.filter_by(slack_id=slack_id).first() if user_record and chat_text in SEND_LINK_COMMANDS: logger.info("[Busy-Beaver] Slack acount already linked to GitHub") slack.post_message(ACCOUNT_ALREADY_ASSOCIATED, channel_id=channel_id) return # generate unique identifer to track user during authentication process state = str(uuid.uuid4()) if user_record and chat_text in RESEND_LINK_COMMANDS: logger.info("[Busy-Beaver] Relinking GitHub account.") user_record.github_state = state db.session.add(user_record) db.session.commit() if not user_record: logger.info("[Busy-Beaver] New user. Linking GitHub account.") user = User() user.slack_id = slack_id user.github_state = state db.session.add(user) db.session.commit() data = { "client_id": GITHUB_CLIENT_ID, "redirect_uri": GITHUB_REDIRECT_URI, "state": state, } query_params = urlencode(data) url = f"https://github.com/login/oauth/authorize?{query_params}" attachment = [{ "fallback": url, "attachment_type": "default", "actions": [{ "text": "Associate GitHub Profile", "type": "button", "url": url }], }] slack.post_message(VERIFY_ACCOUNT, channel_id=channel_id, attachments=attachment)