Exemplo n.º 1
0
def delete_goal(context: BoltContext, payload: Dict[str, Any], ack: Ack,
                client: WebClient):
    ack()
    goal_store: GoalStore = context[SlackMiddleware.GOAL_STORE_KEY]
    goal_store.delete_goal(id=UUID(payload["value"]))
    LOG.info(f"Deleted goal {payload['value']}")
    client.chat_postEphemeral(
        text="Goal deleted",
        user=context["user_id"],
        channel=context["channel_id"],
    )
 def test_missing_text_warnings_chat_postEphemeral(self):
     client = WebClient(base_url="http://localhost:8888",
                        token="xoxb-api_test")
     resp = client.chat_postEphemeral(channel="C111",
                                      user="******",
                                      blocks=[])
     self.assertIsNone(resp["error"])
Exemplo n.º 3
0
def post_ephemeral(channel, text, user, username=None):
    """
    Send an ephemeral message to a user in a channel. This message will only be visible to the target user.

    :param channel: The identifier of the Slack conversation to post to
    :param text: Message text (Formatting: https://api.slack.com/reference/surfaces/formatting)
    :param user: The identifier of the specified user
    :param username: Name displayed by the bot
    :return: Response object (Dictionary)
    """

    if not settings.SLACK_TOKEN:
        return {'ok': False, 'error': 'config_error'}

    client = WebClient(token=settings.SLACK_TOKEN)

    try:
        response = client.chat_postEphemeral(channel=channel,
                                             text=text,
                                             user=user,
                                             username=username)
        assert response['ok'] is True
        return response
    except SlackApiError as e:
        assert e.response['ok'] is False
        return e.response
Exemplo n.º 4
0
def command_handler(message, _context):
    """
    Handles commands sent to slackbot
    """
    team_id = message["team_id"]
    user_id = message["user_id"]
    command = message["text"]
    trigger_id = message.get("trigger_id")
    channel_id = message.get("channel_id")
    slack_access_token = DynamoUtils.get_slack_access_token(team_id)
    client = WebClient(token=slack_access_token)
    slack_commands_handler = SlackCommandsHandler(team_id)
    if "register" in command:
        client.chat_postEphemeral(
            channel=channel_id,
            user=user_id,
            blocks=slack_commands_handler.build_register_response(user_id).get(
                "blocks"),
        )
    elif "responders" in command:
        if "add" in command:
            responders_list_users = re.findall(USER_ID_REGEX, command)
            responders_list_channels = re.findall(CHANNEL_ID_REGEX, command)
            responders_list = responders_list_users + responders_list_channels
            if len(responders_list) == 0:
                client.chat_postEphemeral(
                    channel=channel_id,
                    user=user_id,
                    text="List of responders cannot be empty!",
                )
            response = slack_commands_handler.add_responders(responders_list)
            client.chat_postEphemeral(channel=channel_id,
                                      user=user_id,
                                      text=response)
        elif "remove" in command:
            responders_list_users = re.findall(USER_ID_REGEX, command)
            responders_list_channels = re.findall(CHANNEL_ID_REGEX, command)
            responders_list = responders_list_users + responders_list_channels
            response = slack_commands_handler.remove_responders(
                responders_list)
            client.chat_postEphemeral(channel=channel_id,
                                      user=user_id,
                                      text=response)
        elif "list" in command:
            response = slack_commands_handler.list_responders()
            client.chat_postEphemeral(channel=channel_id,
                                      user=user_id,
                                      text=response)
        else:
            client.chat_postEphemeral(
                channel=channel_id,
                user=user_id,
                text=
                "Sorry I did not understand the command: options are add, remove, list",
            )
    elif "oncall" in command:
        if "set" in command:
            user_matched = re.search(USER_ID_REGEX, command)
            if user_matched is not None:
                user_id_to_set = user_matched.group(1)
                slack_commands_handler.set_oncall(user_id_to_set)
                client.chat_postEphemeral(
                    channel=channel_id,
                    user=user_id,
                    text="<@%s> set as oncall" % user_id_to_set,
                )
            else:
                client.chat_postEphemeral(
                    channel=channel_id,
                    user=user_id,
                    text="Sorry, wrong format. Do `/sereno set oncall <user>`",
                )
    elif "help" in command:
        formatter = HelpFormatter()
        client.chat_postEphemeral(channel=channel_id,
                                  user=user_id,
                                  blocks=formatter.format().get("blocks"))
    elif "close incident" in command:
        slack_commands_handler.show_close_incident_modal(
            team_id, channel_id, trigger_id)
    else:
        client.chat_postEphemeral(
            channel=channel_id,
            user=user_id,
            text="Sorry I did not understand the command. "
            "Type `/sereno help` to see a list of available commands",
        )