def handle_state(context, message_template, target, target_type, as_user=True): """ Send a Slack message without expecting a response """ if not all([target_type, target, message_template]): raise Exception("Incomplete parameters supplied. Please supply target, target_type and message_template") target_id = get_channel_id(target, target_type) message = socless_template_string(message_template, context) resp = slack_client.chat_postMessage(channel=target_id, text=message, as_user=as_user) return {"response": resp.data, "slack_id": target_id}
def handle_state(context, target, target_type, message_template, receiver="", response_desc='[response]'): """ Send a Slack Message and store the message id for the message """ if not message_template: raise Exception("No text was supplied to Slack message") USE_NEW_INTERACTION = 'task_token' in context message_id = gen_id(6) context['_message_id'] = message_id extended_template = "{message_template}\n```{slash_command} {context[_message_id]} {response_desc}```\n".format( message_template=message_template, slash_command=SLACK_SLASH_COMMAND, context=context, response_desc=response_desc) message = socless_template_string(extended_template, context) target_id = get_channel_id(target, target_type) if USE_NEW_INTERACTION: init_human_interaction(context, message, message_id) r = slack_client.chat_postMessage(channel=target_id, text=message, as_user=True) if not r.data['ok']: raise Exception( f"Human Reponse workflow failed to initiate because slack_client failed to send message: {r.data}" ) if not USE_NEW_INTERACTION: investigation_id = context['artifacts']['event']['investigation_id'] execution_id = context.get('execution_id') socless_dispatch_outbound_message(receiver, message_id, investigation_id, execution_id, message) return { "response": r.data, "message_id": message_id, "slack_id": target_id }
def handle_state(context, target_type, target, text, receiver='', prompt_text='', yes_text='Yes', no_text='No'): """ Send a Slack Message and store the message id for the message """ USE_NEW_INTERACTION = 'task_token' in context if not all([target_type, target, text]): raise Exception( "Incomplete inputs: target, target_type and text must be supplied") target_id = get_channel_id(target, target_type) ATTACHMENT_YES_ACTION = { "name": "yes_text", "style": "default", "text": "", "type": "button", "value": "true" } ATTACHMENT_NO_ACTION = { "name": "no_text", "style": "danger", "text": "", "type": "button", "value": "false" } ATTACHMENT_TEMPLATE = { "text": "", "mrkdwn_in": ["text"], "fallback": "New message", "callback_id": "", "color": "#3AA3E3", "attachment_type": "default", "actions": [] } message_id = gen_id(6) context['_message_id'] = message_id text = socless_template_string(text, context) prompt_text = socless_template_string(prompt_text, context) ATTACHMENT_TEMPLATE['text'] = "*{}*".format(prompt_text) ATTACHMENT_TEMPLATE['callback_id'] = message_id ATTACHMENT_YES_ACTION['text'] = yes_text ATTACHMENT_NO_ACTION['text'] = no_text ATTACHMENT_TEMPLATE['actions'] = [ ATTACHMENT_YES_ACTION, ATTACHMENT_NO_ACTION ] payload = {"text": text, "ATTACHMENT_TEMPLATE": ATTACHMENT_TEMPLATE} if USE_NEW_INTERACTION: init_human_interaction(context, payload, message_id) resp = slack_client.chat_postMessage(channel=target_id, text=text, attachments=[ATTACHMENT_TEMPLATE], as_user=True) if not resp.data['ok']: raise Exception(resp.data['error']) if not USE_NEW_INTERACTION: investigation_id = context['artifacts']['event']['investigation_id'] execution_id = context.get('execution_id') socless_dispatch_outbound_message(receiver, message_id, investigation_id, execution_id, payload) return { 'response': resp.data, "message_id": message_id, "slack_id": target_id }