Пример #1
0
def slack_post_message(current_skyline_app, channel, thread_ts, message):
    """
    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 message: message
    :type current_skyline_app: str
    :type channel: str
    :type thread_ts: str or None
    :type message: 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_post = {'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
        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_post

    if thread_ts:
        if thread_ts == 'None':
            thread_ts = None

    # slack_post = None

    # In terms of the generated Slack URLS for threads the
    # timestamps have no dots e.g.:
    # https://<an_org>.slack.com/archives/<a_channel>/p1543994173000700
    # However in terms of the sc.api_call the thread_ts
    # needs the format declared in the dict response e.g.
    # u'ts': u'1543994173.000700'}]} with the dot so in this
    # case '1543994173.000700'
    if thread_ts:
        try:
            # @modified 20200701 - Task #3612: Upgrade to slack v2
            if slack_version == '1.3':
                slack_post = sc.api_call(
                    'chat.postMessage',
                    channel=channel,
                    icon_emoji=icon_emoji,
                    text=message,
                    thread_ts=thread_ts
                )
            else:
                slack_post = sc.chat_postMessage(
                    channel=channel,
                    icon_emoji=icon_emoji,
                    text=message,
                    thread_ts=thread_ts
                )
        except:
            current_logger.error(traceback.format_exc())
            current_logger.error(
                'error :: slack_post_to_thread :: falied to post message to thread %s - %s' % (
                    thread_ts, message))
            # @modified 20200826 - Bug #3710: Gracefully handle slack failures
            # return False
            return slack_post
    else:
        try:
            # @modified 20200701 - Task #3612: Upgrade to slack v2
            if slack_version == '1.3':
                slack_post = sc.api_call(
                    'chat.postMessage',
                    channel=channel,
                    icon_emoji=icon_emoji,
                    text=message
                )
            else:
                slack_post = sc.chat_postMessage(
                    channel=channel,
                    icon_emoji=icon_emoji,
                    text=message
                )
        except:
            current_logger.error(traceback.format_exc())
            current_logger.error(
                'error :: slack_post_message :: falied to post message to thread %s - %s' % (
                    thread_ts, message))
            # @modified 20200826 - Bug #3710: Gracefully handle slack failures
            # return False
            return slack_post

    if slack_post['ok']:
        current_logger.info(
            'slack_post_message :: posted message to channel %s, thread %s - %s' % (
                channel, str(thread_ts), message))
    else:
        current_logger.error(
            'error :: slack_post_message :: falied to post message to channel %s, thread %s - %s' % (
                channel, str(thread_ts), message))
        current_logger.error(
            'error :: slack_post_message :: slack response dict follows')
        try:
            current_logger.error(str(slack_post))
        except:
            current_logger.error('error :: slack_post_message :: no slack response dict found')
        # @modified 20200826 - Bug #3710: Gracefully handle slack failures
        # return False
        return slack_post

    return slack_post