def setUp(self): self.app = api.app.test_client() self.pg_web = pg_connect(SETTINGS['DATABASE_WEB_URL']) self.pg_data = pg_connect(SETTINGS['DATABASE_URL']) self.redis_data = redis.StrictRedis.from_url(SETTINGS['REDIS_URL']) # turn on autocommit self.pg_data.autocommit = True self.pg_web.autocommit = True # always start with an empty click database cursor = self.pg_data.cursor() cursor.execute('DELETE FROM clicks;') cursor.execute('DELETE FROM urls;') cursor.execute('DELETE FROM comments;') cursor.close() # make sure the all test users start with 0 balance, no free credit cursor = self.pg_web.cursor() cursor.execute( "UPDATE users SET balance_paid=0, balance_free=100, total_given=0") cursor.execute("SELECT id FROM users WHERE uuid=%s", ("3dd80d107941012f5e2c60c5470a09c8", )) result = cursor.fetchone() self.user_id = result[0] cursor.execute('DELETE FROM payments') # insert and process a valid click message = '{"uuid":"a2afb8a0-fc6f-11e1-b984-eff95004abc9", "user_uuid":"3dd80d107941012f5e2c60c5470a09c8", "button_uuid":"a4b16a40dff9012f5efd60c5470a09c8", "amount":25, "referrer_user_uuid":null, "referrer":"http://localhost:3000/thisisfrancis", "user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1", "ip_address":"127.0.0.1", "created_at":"' + datetime.utcnow( ).isoformat() + '"}' processor.process_message(message) # clear redis self.redis_data.flushdb()
def telegram_poller(): """ - continuously listen to the messages on the telegram chat - create tasks for the messages - submit to the thread pool - wait 3 seconds and re-do the same """ threading.current_thread().setName("Telegram") try: log.info("Starting Telegram Poller") while True: last_updated_id = db.config(Const.TELEGRAM_UPDATE_ID) msgs = app.get_messages(last_updated_id, timeout=15) for message in msgs: current_time = int(time.time()) message.source = "telegram" # Don't process Old messages last_updated_id = message.update_id if not message['text'] or message.date and message.date < ( current_time - 11): log.info('discarding: %s ', message) db.set_config(Const.TELEGRAM_UPDATE_ID, last_updated_id + 1) continue db.set_config(Const.TELEGRAM_UPDATE_ID, last_updated_id + 1) processor.process_message(message) except Exception as e1: log.exception(e1) app.send_msg("Error occured !", accessControl.adminChatId)
def process_shell_command(text): if text == 'help': log.info('-' * 30) for x in cm.commands: log.info(x) log.info('-' * 30) else: if len(text.strip()) > 0: local_message = DotMap() local_message.chat.id = accessControl.adminChatId local_message.text = "/" + text local_message.chat.username = accessControl.adminUserId local_message.source = "terminal" processor.process_message(local_message, use_executor=True)
def cm_price_reach_condition(task, condition): symbol = app.symbol_with_currency(task.params[0]) price = float(task.params[1]) tickers = app.tickers() log.debug(f"Checking if Price is {condition} : {symbol} {price}") curr_price = tickers[symbol] test = curr_price > price if condition == 'above' else curr_price < price if test: log.info(f"target price reached: {symbol} {condition} {price}") command = task.params[2:] command_str = "/" + " ".join(command) new_message = DotMap(task.message.toDict()) new_message.text = command_str processor.process_message(new_message) if task.message.scheduletag: log.info("clearing the schedule of Price Condition") schedule.clear(task.message.scheduletag)
def test_process_message(self): cursor_web = self.pg_web.cursor() cursor_data = self.pg_data.cursor() # invalid user_id, should be dropped message = '{"uuid":"a2afb8a0-fc6f-11e1-b984-eff95004abc9", "user_uuid":"invaliduuid", "button_uuid":"a4b16a40dff9012f5efd60c5470a09c8", "amount":25, "referrer_user_uuid":null, "referrer":"http://localhost:3000/thisisfrancis", "user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1", "ip_address":"127.0.0.1", "created_at":"' + datetime.utcnow( ).isoformat() + '"}' processor.process_message(message) # balance still 0 at this point cursor_web.execute( 'SELECT balance_paid, balance_free, total_given FROM users WHERE uuid=%s', ("3dd80d107941012f5e2c60c5470a09c8", )) result = cursor_web.fetchone() self.assertEqual((0, 100, 0), result) # valid message, balance should be deducted and state changed message = '{"uuid":"a2afb8a0-fc6f-11e1-b984-eff95004abc9", "user_uuid":"3dd80d107941012f5e2c60c5470a09c8", "button_uuid":"a4b16a40dff9012f5efd60c5470a09c8", "amount":25, "referrer_user_uuid":null, "referrer":"http://localhost:3000/thisisfrancis", "user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1", "ip_address":"127.0.0.1", "created_at":"' + datetime.utcnow( ).isoformat() + '"}' processor.process_message(message) cursor_web.execute( 'SELECT balance_paid, balance_free, total_given FROM users WHERE uuid=%s', ("3dd80d107941012f5e2c60c5470a09c8", )) result = cursor_web.fetchone() self.assertEqual((0, 75, 25), result) # process again, balance and state should be unchanged processor.process_message(message) cursor_web.execute( 'SELECT balance_paid, balance_free, total_given FROM users WHERE uuid=%s', ("3dd80d107941012f5e2c60c5470a09c8", )) result = cursor_web.fetchone() self.assertEqual((0, 75, 25), result) # process a new message that changes the amount message = '{"uuid":"a2afb8a0-fc6f-11e1-b984-eff95004abc9", "user_uuid":"3dd80d107941012f5e2c60c5470a09c8", "button_uuid":"a4b16a40dff9012f5efd60c5470a09c8", "amount":50, "referrer_user_uuid":null, "referrer":"http://localhost:3000/thisisfrancis", "user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1", "ip_address":"127.0.0.1", "created_at":"' + datetime.utcnow( ).isoformat() + '"}' processor.process_message(message) cursor_web.execute( 'SELECT balance_paid, balance_free, total_given FROM users WHERE uuid=%s', ("3dd80d107941012f5e2c60c5470a09c8", )) result = cursor_web.fetchone() self.assertEqual((0, 50, 50), result)
def on_message(ws, event_message): process_message(event_message)