def run_sync_deleting() -> None: # no test coverage conn = database.connect() slack_client = slack.WebClient(config.slack_bot_user_token) try: delete_sync_reminders(conn, slack_client) finally: conn.close()
def setup() -> t.Tuple[SlackClient, Dropbox, connection]: conn = database.connect() dbx = pictures.connect_dbx() slack_client = SlackClient(config.slack_bot_user_token) connected = slack_client.rtm_connect() if not connected: raise Exception("Connection failed. Invalid Slack token or bot ID?") return slack_client, dbx, conn
def connection_context( conn=t.Optional[connection], ) -> t.Generator[connection, None, None]: if conn is not None: yield conn elif current_app: # no test coverage with current_app.pool.get_connection() as conn: yield conn else: # no test coverage conn = database.connect() try: yield conn finally: conn.close()
def run_sync_reminding() -> None: # no test coverage conn = database.connect() current_date = pendulum.now() date = current_date.subtract(days=1) data = activity(conn, date) if data is None: return steps_data, body_reports = data slack_client = slack.WebClient(config.slack_bot_user_token) try: send_sync_reminders(conn, slack_client, steps_data) finally: conn.close()
def send_congrats() -> None: # no test coverage dbx = pictures.connect_dbx() conn = database.connect() recipients = todays_recipients(conn) if not recipients: return log.info(f"Recipients today {recipients}") slack_client = slack.WebClient(config.slack_bot_user_token) for recipient in recipients: greet = formulate_congrat(recipient, conn, dbx) commands.send_response(slack_client, greet, channel=config.main_channel)
def connection_context( conn=t.Optional[db.connection], ) -> t.Generator[db.connection, None, None]: if conn is not None: yield conn elif current_app: with current_app.pool.get_connection() as conn: yield conn else: conn = db.connect() try: yield conn finally: conn.close()
def main() -> None: log.info("GargBot 3000 greeter starter") try: while True: event = Event.next() log.info(f"Next greeting check at: {event.until.end}, " f"sleeping for {event.until.in_words()}") time.sleep(event.until.total_seconds()) try: slack_client = SlackClient(config.slack_bot_user_token) conn = database.connect() event.func(conn, slack_client) except Exception: log.error("Error in command execution", exc_info=True) finally: conn.close() except KeyboardInterrupt: sys.exit()
def main(): slack_client, dbx, conn = setup() log.info("GargBot 3000 task operational!") try: while True: time.sleep(1) text, channel, user, thread_ts = wait_for_slack_output( slack_client) try: command_str, *args = text.split() except ValueError: command_str = "" args = [] command_str = command_str.lower() command_func = partial(commands.execute, command_str=command_str, args=args, conn=conn, dbx=dbx) try: response = command_func() except psycopg2.OperationalError: conn = database.connect() try: response = command_func() except Exception as exc: # OperationalError not caused by connection issue. log.error("Error in command execution", exc_info=True) response = commands.cmd_panic(exc) send_response(slack_client, response, channel, thread_ts) except KeyboardInterrupt: sys.exit() finally: conn.close()