예제 #1
0
def move_to_main( coin_properties: CoinProperties, user, wallet_balance ):
    if wallet_balance <= 0:
        return

    if clientcommandprocessor.run_client_command( coin_properties.rpc_configuration, 'move', None, user, '', wallet_balance ):
        connection = database.create_connection()
        with connection:
            database.execute_query( connection, Statements[ 'INSERT_USER' ], (user,) )
            database.execute_query( connection, Statements[ 'UPDATE_USER_BALANCE' ], (user, coin_properties.ticker, str( wallet_balance ),) )
    else:
        raise Exception( f'Failed to move {user} balance {wallet_balance} to main account.' )
예제 #2
0
def main():
    bot_configuration = BotConfiguration(Configuration)
    connection = database.create_connection()

    with connection:
        database.init_database(connection)
        database.execute_query(connection, Statements['INSERT_USER'],
                               (bot_configuration.telegram_bot_name, ))
        for coin in bot_configuration.coins:
            database.execute_query(connection, Statements['INSERT_COIN'], (
                coin.name,
                coin.ticker,
            ))

    activity_tracker = ActivityTracker()
    updater = Updater(token=bot_configuration.telegram_bot_token,
                      request_kwargs=None)
    dispatcher = updater.dispatcher

    for coin in bot_configuration.coins:
        dispatcher.add_handler(
            MessageHandler(~Filters.command, activity_tracker.track_activity))
        dispatcher.add_handler(
            CommandHandler(f'{coin.ticker}commands'.lower(),
                           Command(commands.commands, coin)))
        dispatcher.add_handler(
            CommandHandler(f'{coin.ticker}start'.lower(),
                           Command(commands.help, coin)))
        dispatcher.add_handler(
            CommandHandler(f'{coin.ticker}help'.lower(),
                           Command(commands.help, coin)))
        dispatcher.add_handler(
            CommandHandler(f'{coin.ticker}balance'.lower(),
                           Command(commands.balance, coin)))
        dispatcher.add_handler(
            CommandHandler(f'{coin.ticker}deposit'.lower(),
                           Command(commands.deposit, coin)))
        dispatcher.add_handler(
            CommandHandler(f'{coin.ticker}withdraw'.lower(),
                           Command(commands.withdraw, coin)))
        dispatcher.add_handler(
            CommandHandler(f'{coin.ticker}market'.lower(),
                           Command(commands.market, coin, activity_tracker)))
        dispatcher.add_handler(
            CommandHandler(f'{coin.ticker}tip'.lower(),
                           Command(commands.tip, coin, activity_tracker)))
        dispatcher.add_handler(
            CommandHandler(f'{coin.ticker}rain'.lower(),
                           Command(commands.rain, coin, activity_tracker)))

    updater.start_polling()

    logger.info('Bot started.')
예제 #3
0
 def getLastRecords(self, count):
     with execute_query(
             self.conn,
             "select id, feed_id, date, link, title, description, state from record where state<>%(state)s order by date desc limit %(count)s",
             state=StateType.Readed,
             count=count) as qry:
         return self.mapResult(qry.cursor())
예제 #4
0
    def track_activity(self, bot, update):
        try:
            chat_id = update.message.chat_id
            now = datetime.now()
            current_time = datetime.timestamp(now)
            user = commonhelper.get_username(update)
            connection = database.create_connection()

            with connection:
                user_id = database.fetch_result(
                    connection,
                    Statements['SELECT_USER_ID_BY_TELEGRAM_USERNAME'],
                    (user, ))

                if user_id is None:
                    user_id = database.execute_query(connection,
                                                     Statements['INSERT_USER'],
                                                     (user, ))

                database.execute_query(connection,
                                       Statements['UPDATE_USER_ACTIVITY'], (
                                           user_id,
                                           chat_id,
                                           current_time,
                                       ))

            if chat_id not in self.active_users_cache:
                self.active_users_cache.update({chat_id: {user: current_time}})
            else:
                if user not in self.active_users_cache[chat_id]:
                    self.active_users_cache[chat_id].update(
                        {user: current_time})
                else:
                    self.active_users_cache[chat_id][user] = current_time

            logger.info(
                f'@{user} spoke @{chat_id} at {self.active_users_cache[ chat_id ][ user ]}.'
            )
        except BotUserError as e:
            logger.info(e)
예제 #5
0
def tip(update, coin_properties: CoinProperties):
    ticker = coin_properties.ticker
    arguments = update.message.text.split(' ')

    if len(arguments) < 3:
        raise BotUserError(messages.tip_error(ticker.lower()))

    user = commonhelper.get_username(update)
    target_user = arguments[1]
    amount = arguments[2]

    if '@' not in target_user:
        raise BotUserError(f'That user ´{target_user}´ is not applicable.')

    target_user = target_user[1:]
    user_balance, wallet_balance = commonhelper.get_user_balance(
        user, coin_properties)
    amount = commonhelper.get_validated_amount(amount, user, user_balance)

    commonhelper.move_to_main(coin_properties, user, wallet_balance)

    if target_user == user:
        raise BotUserError('You can not tip Yourself.')

    users_balance_changes = [(user, ticker, str(-amount)),
                             (target_user, ticker, str(amount))]

    connection = database.create_connection()

    with connection:
        database.execute_query(connection, Statements['INSERT_USER'],
                               (target_user, ))
        database.execute_many(connection, Statements['UPDATE_USER_BALANCE'],
                              users_balance_changes)

    return f'@{user} tipped @{target_user} of {amount} {ticker}',
예제 #6
0
 def getAll(self):
     with execute_query(self.conn, "select id, url from feed") as qry:
         return self.mapResult(qry.cursor())
예제 #7
0
 def getRecordById(self, id):
     with execute_query(
             self.conn,
             "select id, feed_id, link, guid, date, link, title, description, state from record where id=%(id)s",
             id=id) as qry:
         return self.mapResult(qry.cursor())