def get_channel_by_name(bot, channel): """ Get a channel, from its channel name. This function will only fetch from cache. If the channel isn't in cache, it will return None. """ if channel.startswith('#'): channel = channel[1:] redis_client = omniredis.get_redis_client() channel_id = redis_client.hget( 'channelsmap:{}'.format(bot.team.name), channel ) if channel_id: return _get_channel_name_from_cache( 'channels', bot.team.name, channel_id ) group_id = redis_client.hget('groupsmap:{}'.format(bot.team.name), channel) if group_id: return _get_channel_name_from_cache('groups', bot.team.name, group_id) im_id = redis_client.hget('imsmap:{}'.format(bot.team.name), channel) if im_id: return _get_channel_name_from_cache('ims', bot.team.name, im_id) mpim_id = redis_client.hget('mpimsmap:{}'.format(bot.team.name), channel) if mpim_id: return _get_channel_name_from_cache('mpims', bot.team.name, mpim_id) return None
def _get_channel_name_from_cache(key, bot_name, value): redis_client = omniredis.get_redis_client() ret = redis_client.hget('{}:{}'.format(key, bot_name), value) if ret is None: return None else: return json.loads(ret)
def update_user(bot, user): if user['is_bot']: return if user.get('deleted', False): return profile = user.get('profile') if not profile: return email = profile.get('email') if not email: return name = get_name_from_user(user) redis_client = omniredis.get_redis_client() redis_client.hset('users:{}'.format(bot.team.name), user['id'], json.dumps(user)) redis_client.hset( 'usersmap:name:{}'.format(bot.team.name), name, user['id'], ) redis_client.hset( 'usersmap:email:{}'.format(bot.team.name), email, user['id'], )
def watch_channels(): try: redis_client = omniredis.get_redis_client(decode_responses=False) last_run_key = "watch:channels:last_run_datetime" if not _is_allowed_to_run(redis_client, last_run_key): return statsd = stats.get_statsd_client() with redis_lock.Lock(redis_client, 'watch_channels', expire=LOCK_EXPIRATION, auto_renewal=True): with statsd.timer('watch.channels'): for team_name, bot_name in settings.PRIMARY_SLACK_BOT.items(): logger.info( 'Updating slack channel list.', extra={ 'team': team_name, 'bot': bot_name }, ) team = Team.get_team_by_name(team_name) bot = Bot.get_bot_by_name(team, bot_name) slack.update_channels(bot) redis_client.set(last_run_key, datetime.now().isoformat()) except Exception: logger.exception('Failed to update slack channel list.', exc_info=True) finally: return gevent.spawn_later(settings.WATCHER_SPAWN_WAIT_TIME_IN_SEC, watch_channels)
def update_im(bot, im): redis_client = omniredis.get_redis_client() redis_client.hset('ims:{}'.format(bot.team.name), im['id'], json.dumps(im)) redis_client.hset( 'imsmap:{}'.format(bot.team.name), im['user'], im['id'], )
def update_mpim(bot, mpim): redis_client = omniredis.get_redis_client() redis_client.hset('mpims:{}'.format(bot.team.name), mpim['id'], json.dumps(mpim)) redis_client.hset( 'mpimsmap:{}'.format(bot.team.name), mpim['name'], mpim['id'], )
def update_group(bot, group): redis_client = omniredis.get_redis_client() redis_client.hset('groups:{}'.format(bot.team.name), group['id'], json.dumps(group)) redis_client.hset( 'groupsmap:{}'.format(bot.team.name), group['name'], group['id'], )
def update_channel(bot, channel): redis_client = omniredis.get_redis_client() redis_client.hset('channels:{}'.format(bot.team.name), channel['id'], json.dumps(channel)) redis_client.hset( 'channelsmap:{}'.format(bot.team.name), channel['name'], channel['id'], )
def get_user_by_email(bot, email): redis_client = omniredis.get_redis_client() user_id = redis_client.hget('usersmap:email:{}'.format(bot.team.name), email) if user_id: user_data = redis_client.hget('users:{}'.format(bot.team.name), user_id) if user_data: return json.loads(user_data) return {}
def get_user_by_name(bot, username): if username.startswith('@'): username = username[1:] redis_client = omniredis.get_redis_client() user_id = redis_client.hget('usersmap:name:{}'.format(bot.team.name), username) if user_id: user_data = redis_client.hget('users:{}'.format(bot.team.name), user_id) if user_data: return json.loads(user_data) return {}
def run(self): redis_client = omniredis.get_redis_client() for team in settings.SLACK_TEAMS: redis_client.delete('channels:{}'.format(team)) redis_client.delete('channelsmap:{}'.format(team)) redis_client.delete('groups:{}'.format(team)) redis_client.delete('groupsmap:{}'.format(team)) redis_client.delete('ims:{}'.format(team)) redis_client.delete('imsmap:{}'.format(team)) redis_client.delete('mpims:{}'.format(team)) redis_client.delete('mpimsmap:{}'.format(team)) redis_client.delete('users:{}'.format(team)) redis_client.delete('teams')
def _get_channel_from_cache(bot, channel): redis_client = omniredis.get_redis_client() channel_data = redis_client.hget('channels:{}'.format(bot.team.name), channel) if channel_data: return json.loads(channel_data) group_data = redis_client.hget('groups:{}'.format(bot.team.name), channel) if group_data: return json.loads(group_data) im_data = redis_client.hget('ims:{}'.format(bot.team.name), channel) if im_data: return json.loads(im_data) mpim_data = redis_client.hget('mpims:{}'.format(bot.team.name), channel) if mpim_data: return json.loads(mpim_data) return None
def get_user(bot, user_id): """ Get a user, from its user id """ redis_client = omniredis.get_redis_client() user = redis_client.hget('users:{}'.format(bot.team.name), user_id) if user: return json.loads(user) user = client(bot).api_call('users.info', user=user_id) if user['ok']: update_user(bot, user['user']) return user['user'] else: logger.warning('Failed to find user', extra={ 'user': user_id, 'bot': bot.name, }) return {}
def get_im_channel_id(bot, user_id): redis_client = omniredis.get_redis_client() imsmap_id = redis_client.hget('imsmap:{}'.format(bot.team.name), user_id) if imsmap_id: raw_im = redis_client.hget('ims:{}'.format(bot.team.name), imsmap_id) if raw_im: im = json.loads(raw_im) if not im.get('is_user_deleted', False): return im['id'] retry = 0 while True: users = user_id conversation_data = client(bot).api_call( 'conversations.open', users=users ) if conversation_data['ok']: return conversation_data['channel']['id'] else: # TODO: split this retry logic into a generic retry function retry = retry + 1 if retry >= MAX_RETRIES: logger.error( 'Exceeded max retries when calling conversations.open.', extra=bot.logging_context, ) break logger.warning( 'Call to conversations.open failed, attempting retry', extra=merge_logging_context( {'retry': retry}, _get_failure_context(conversation_data), bot.logging_context, ), ) gevent.sleep(GEVENT_SLEEP_TIME) continue return None
def get_channels(bot): redis_client = omniredis.get_redis_client() return redis_client.hscan_iter('channels:{}'.format(bot.team.name))
def update_emoji(bot): redis_client = omniredis.get_redis_client() for k, v in _get_emoji(bot).items(): redis_client.hset('emoji:{}'.format(bot.team.name), k, v)
def get_emoji(bot, name): redis_client = omniredis.get_redis_client() return redis_client.hget('emoji:{}'.format(bot.team.name), name)
def get_users(bot): redis_client = omniredis.get_redis_client() return redis_client.hscan_iter('users:{}'.format(bot.team.name))