def restart_receive_slack_thread(): from boot_sns import StipSnsBoot th = StipSnsBoot.get_slack_thread() slack_rtm_client = StipSnsBoot.get_slack_rtm_client() slack_web_client = StipSnsBoot.get_slack_web_client() slack_token = SNSConfig.get_slack_bot_token() if th: th.end() th.join() if not slack_rtm_client: loop = asyncio.new_event_loop() slack_rtm_client = slack.RTMClient(token=slack_token, loop=loop) else: slack_rtm_client = slack.RTMClient(token=slack_token, loop=slack_rtm_client._event_loop) th = SlackThread(slack_rtm_client) return slack_web_client, slack_rtm_client, th
def download_stix_id(command_stix_id): wc = StipSnsBoot.get_slack_web_client() # cache の STIX を返却 stix_file_path = Feed.get_cached_file_path( command_stix_id.replace(':', '--')) file_name = '%s.xml' % (command_stix_id) post_slack_channel = SNSConfig.get_slack_bot_chnnel() wc.files_upload(initial_comment='', channels=post_slack_channel, file=open(stix_file_path, 'rb'), filename=file_name) return
def post_stip_from_slack(receive_data, slack_bot_channel_name, slack_user): wc = StipSnsBoot.get_slack_web_client() POST_INDEX_TITLE = 0 if 'subtype' in receive_data: # 投稿以外のメッセージなので対象外 return # user_id から user_info 取得 try: user_id = receive_data['user'] except KeyError: return user_profile = get_user_profile(user_id) # bot からの発言は対象外 if 'bot_id' in user_profile: return # S-TIP 投稿データを取得する text = receive_data['text'] stip_params = get_stip_params(text, user_profile['display_name']) if stip_params is None: return # 添付file データ取得 files_for_cti_extractor, files_for_stip_post = get_attached_files( receive_data) # CTI Element Extractor 追加 stip_params = set_extractor_info(stip_params, files_for_cti_extractor, user_profile['display_name']) # 本文に各種 footer 情報を追加 post = stip_params[STIP_PARAMS_INDEX_POST] # command 関連 command_stix_id = get_command_stix_id(post) # :stix <stix_id> if command_stix_id is not None: download_stix_id(command_stix_id) return # <!here> が含まれている場合 post = post.replace('<!here>', '@here') # <!channel> が含まれている場合 post = post.replace('<!channel>', '@channel') # <@user_id> が含まれている場合 post = convert_user_id(post) # <#channel_id|channnel_name> が含まれている場合 post = convert_channel_info(post) post = post.replace('&', '&') post = post.replace('<', '<') post = post.replace('>', '>') post += '\n----------Slack Message Info----------\n' # 1行目がtitle stip_params[STIP_PARAMS_INDEX_TITLE] = post.splitlines()[POST_INDEX_TITLE] # channle_id から channel 情報取得 try: channel_id = receive_data['channel'] channel_name = None try: resp = wc.conversations_info(channel=channel_id) # public channel channel_name = resp['channel']['name'] except slack.errors.SlackApiError: # private channnel resp = wc.groups_info(channel=channel_id) channel_name = resp['group']['name'] except Exception: # チャンネル名が取れないので skip pass if channel_name != slack_bot_channel_name: # 該当チャンネルではないの skip return post += ('%s: %s\n' % ('Channel', channel_name)) except KeyError: # チャンネル名が取れないので skip return # アカウント名 try: post += ('Full name: %s\n' % (user_profile['real_name'])) except KeyError: # アカウント名が取れないので skip return # メッセージ id if 'client_msg_id' in receive_data: post += ('%s: %s\n' % ('Message ID', receive_data['client_msg_id'])) if 'ts' in receive_data: ts = receive_data['ts'] dt = datetime.datetime(*time.gmtime(float(ts))[:6], tzinfo=pytz.utc) post += ('%s: %s\n' % ('Timestamp', dt.strftime('%Y-%m-%dT%H:%M:%S.%f%z'))) stip_params[STIP_PARAMS_INDEX_POST] = post # ここから SNS に投稿する try: request = HttpRequest() request.method = 'POST' request.POST = stip_params request.FILES = files_for_stip_post request.META['SERVER_NAME'] = 'localhost' request.user = slack_user post_common(request, slack_user) except Exception as e: import traceback traceback.print_exc() raise e return
def get_user_profile(user_id): wc = StipSnsBoot.get_slack_web_client() return wc.users_info(user=user_id)['user']['profile']