(server, test_loop=None)
Run the main loop

    server is a limbo Server object
    test_loop, if present, is a number of times to run the loop
Run the main loop
Run the main loop
def loop(server, test_loop=None):
    """Run the main loop

    server is a limbo Server object
    test_loop, if present, is a number of times to run the loop
    """
    try:
        loops_without_activity = 0
        while test_loop is None or test_loop > 0:
            start = time.time()
            loops_without_activity += 1

            events = server.slack.rtm_read()
            for event in events:
                loops_without_activity = 0
                logger.debug("got {0}".format(event))
                response = handle_event(event, server)

                # The docs (https://api.slack.com/docs/message-threading)
                # suggest looking for messages where `thread_ts` != `ts`,
                # but I can't see anywhere that it would make a practical
                # difference. If a message is part of a thread, respond to
                # that thread.
                thread_ts = None
                if "thread_ts" in event:
                    thread_ts = event["thread_ts"]

                while response:
                    # The Slack API documentation says:
                    #
                    # Clients should limit messages sent to channels to 4000
                    # characters, which will always be under 16k bytes even
                    # with a message comprised solely of non-BMP Unicode
                    # characters at 4 bytes each.
                    #
                    # but empirical testing shows that I'm getting disconnected
                    # at 4000 characters and even quite a bit lower. Use 1000
                    # to be safe
                    server.slack.rtm_send_message(
                        event["channel"], response[:1000], thread_ts
                    )
                    response = response[1000:]

            # Run the loop hook. This doesn't send messages it receives,
            # because it doesn't know where to send them. Use
            # server.slack.post_message to send messages from a loop hook
            run_hook(server.hooks, "loop", server)

            # The Slack RTM API docs say:
            #
            # > When there is no other activity clients should send a ping
            # > every few seconds
            #
            # So, if we've gone >5 seconds without any activity, send a ping.
            # If the connection has broken, this will reveal it so slack can
            # quit
            if loops_without_activity > 5:
                server.slack.ping()
                loops_without_activity = 0

            end = time.time()
            runtime = start - end
            time.sleep(max(1 - runtime, 0))

            if test_loop:
                test_loop -= 1
    except KeyboardInterrupt:
        if os.environ.get("LIMBO_DEBUG"):
            import ipdb

            ipdb.set_trace()
        raise
def loop ( server , test_loop = None ) : try : loops_without_activity = 0 while test_loop is None or test_loop > 0 : start = time . time ( ) loops_without_activity += 1 events = server . slack . rtm_read ( ) for event in events : loops_without_activity = 0 logger . debug ( "got {0}" . format ( event ) ) response = handle_event ( event , server ) # The docs (https://api.slack.com/docs/message-threading) # suggest looking for messages where `thread_ts` != `ts`, # but I can't see anywhere that it would make a practical # difference. If a message is part of a thread, respond to # that thread. thread_ts = None if "thread_ts" in event : thread_ts = event [ "thread_ts" ] while response : # The Slack API documentation says: # # Clients should limit messages sent to channels to 4000 # characters, which will always be under 16k bytes even # with a message comprised solely of non-BMP Unicode # characters at 4 bytes each. # # but empirical testing shows that I'm getting disconnected # at 4000 characters and even quite a bit lower. Use 1000 # to be safe server . slack . rtm_send_message ( event [ "channel" ] , response [ : 1000 ] , thread_ts ) response = response [ 1000 : ] # Run the loop hook. This doesn't send messages it receives, # because it doesn't know where to send them. Use # server.slack.post_message to send messages from a loop hook run_hook ( server . hooks , "loop" , server ) # The Slack RTM API docs say: # # > When there is no other activity clients should send a ping # > every few seconds # # So, if we've gone >5 seconds without any activity, send a ping. # If the connection has broken, this will reveal it so slack can # quit if loops_without_activity > 5 : server . slack . ping ( ) loops_without_activity = 0 end = time . time ( ) runtime = start - end time . sleep ( max ( 1 - runtime , 0 ) ) if test_loop : test_loop -= 1 except KeyboardInterrupt : if os . environ . get ( "LIMBO_DEBUG" ) : import ipdb ipdb . set_trace ( ) raise