import sqlite3
import asyncio
from aiogram import Bot, Dispatcher, types
from aiogram.filters import Command
API_TOKEN = 'ТОКЕН'
bot = Bot(token=API_TOKEN)
dp = Dispatcher()
conn = sqlite3.connect('bot.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
user_id INTEGER PRIMARY KEY,
username TEXT,
points INTEGER DEFAULT 0,
invited_by INTEGER
)
''')
conn.commit()
def add_user(user_id, username, invited_by=None):
cursor.execute('SELECT * FROM users WHERE user_id=?', (user_id,))
if cursor.fetchone() is None:
cursor.execute('INSERT INTO users (user_id, username, invited_by) VALUES (?, ?, ?)',
(user_id, username, invited_by))
conn.commit()
def add_points(user_id, points):
cursor.execute('UPDATE users SET points = points + ? WHERE user_id = ?', (points, user_id))
conn.commit()
@dp.message(Command('start'))
async def send_welcome(message: types.Message):
command = message.get_command()
args = message.get_args()
user_id = message.from_user.id
username = message.from_user.username
invited_by = None
if args:
try:
invited_by = int(args)
except ValueError:
pass
add_user(user_id, username, invited_by)
if invited_by:
add_points(invited_by, 10)
await bot.send_message(invited_by, f"Вас пригласил пользователь @{username}. Вы получили 10 очков!")
markup = types.InlineKeyboardMarkup()
markup.add(types.InlineKeyboardButton("Реферальная ссылка", callback_data="referral_link"))
await message.answer("Добро пожаловать! Нажмите кнопку ниже, чтобы получить реферальную ссылку:",
reply_markup=markup)
@dp.callback_query(lambda callback_query: callback_query.data == "referral_link")
async def process_referral_link(callback_query: types.CallbackQuery):
user_id = callback_query.from_user.id
referral_link = f"https://t.me/YOUR_BOT_USERNAME?start={user_id}"
await bot.send_message(user_id, f"Ваша реферальная ссылка: {referral_link}")
async def main():
await dp.start_polling()
if __name__ == '__main__':
asyncio.run(main())
错误:
ValueError: At least one bot instance is required to start polling.