Exemple #1
0
def slack_post_reaction(current_skyline_app, channel, thread_ts, emoji):
    """
    Post a message to a slack channel or thread.

    :param current_skyline_app: the skyline app using this function
    :param channel: the slack channel
    :param thread_ts: the slack thread timestamp
    :param emoji: emoji e.g. thumbsup
    :type current_skyline_app: str
    :type channel: str
    :type thread_ts: str
    :type emoji: str
    :return: slack response dict
    :rtype: dict

    """

    current_skyline_app_logger = str(current_skyline_app) + 'Log'
    current_logger = logging.getLogger(current_skyline_app_logger)

    # @added 20200826 - Bug #3710: Gracefully handle slack failures
    slack_response = {'ok': False}

    try:
        # @modified 20200701 - Task #3612: Upgrade to slack v2
        #                      Task #3608: Update Skyline to Python 3.8.3 and deps
        #                      Task #3556: Update deps
        # sc = SlackClient(token)
        if slack_version == '1.3':
            sc = SlackClient(token)
        else:
            sc = WebClient(token, timeout=10)
    except:
        current_logger.error(traceback.format_exc())
        current_logger.error(
            'error :: slack_post_message :: falied to connect slack')
        # @modified 20200826 - Bug #3710: Gracefully handle slack failures
        # return False
        return slack_response

    # slack_response = None

    try:
        # @modified 20200701 - Task #3612: Upgrade to slack v2
        #                      Task #3608: Update Skyline to Python 3.8.3 and deps
        #                      Task #3556: Update deps
        if slack_version == '1.3':
            slack_response = sc.api_call(
                'reactions.add',
                channel=channel,
                name=emoji,
                timestamp=thread_ts
            )
        else:
            slack_response = sc.reactions_add(
                channel=channel,
                name=emoji,
                timestamp=thread_ts
            )
    except:
        current_logger.error(traceback.format_exc())
        current_logger.error(
            'error :: slack_post_reaction :: falied to post reaction to thread %s - %s' % (
                thread_ts, emoji))
        # @modified 20200826 - Bug #3710: Gracefully handle slack failures
        # return False
        return slack_response
    if not slack_response['ok']:
        # @modified 20220214 - Bug #4448: Handle missing slack response
        # if str(slack_response['error']) == 'already_reacted':
        slack_response_error = None
        try:
            slack_response_error = slack_response['error']
        except KeyError:
            slack_response_error = slack_response['error']
            fail_msg = 'error :: create_features_profile :: no slack response'
            current_logger.error('%s' % fail_msg)
        if slack_response_error == 'already_reacted':
            current_logger.info(
                'slack_post_reaction :: already_reacted to channel %s, thread %s, ok' % (
                    channel, str(thread_ts)))
        else:
            current_logger.error(
                'error :: slack_post_reaction :: falied to post reaction to channel %s, thread %s - %s' % (
                    channel, str(thread_ts), emoji))
            current_logger.error(
                'error :: slack_post_reaction :: slack response dict follows')
            try:
                current_logger.error(str(slack_response))
            except:
                current_logger.error('error :: slack_post_reaction :: no slack response dict found')
            # @modified 20200826 - Bug #3710: Gracefully handle slack failures
            # return False
            return slack_response

    return slack_response