Alexei
2 years ago
5 changed files with 125 additions and 4 deletions
@ -0,0 +1,77 @@
|
||||
# -*- coding: utf8 -*- |
||||
# Общественное достояние 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru> |
||||
from bot_sys import user_bd, log, config |
||||
|
||||
from aiogram import types, Bot, Dispatcher |
||||
from aiogram.types import ReplyKeyboardRemove, ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton |
||||
from aiogram.dispatcher import Dispatcher |
||||
from aiogram.contrib.fsm_storage.memory import MemoryStorage |
||||
|
||||
storage = MemoryStorage() |
||||
bot = Bot(token=config.GetTelegramBotApiToken(), parse_mode=types.ParseMode.HTML) |
||||
dp = Dispatcher(bot, storage=storage) |
||||
|
||||
# --------------------------------------------------------- |
||||
# Сообщения |
||||
|
||||
start_message = ''' |
||||
<b>👋 | Добро пожаловать!</b> |
||||
|
||||
<b>Приятного пользования!</b> |
||||
''' |
||||
|
||||
profile_message = ''' |
||||
<b>Профиль:</b> |
||||
|
||||
<b>ID:</b> @user_id |
||||
<b>Имя:</b> @user_name |
||||
''' |
||||
|
||||
user_profile_button_name = "📰 Профиль" |
||||
back_to_start_menu_button_name = "◀ Назад" |
||||
|
||||
# --------------------------------------------------------- |
||||
# Работа с кнопками |
||||
|
||||
def GetStartKeyboardButtons(): |
||||
key = types.ReplyKeyboardMarkup(resize_keyboard=True) |
||||
prof_button = types.KeyboardButton(user_profile_button_name) |
||||
key.add(prof_button) |
||||
return key |
||||
|
||||
def GetProfileKeyboardButtons(): |
||||
key = types.ReplyKeyboardMarkup(resize_keyboard=True) |
||||
back_to_start_menu = types.KeyboardButton(back_to_start_menu_button_name) |
||||
key.add(back_to_start_menu) |
||||
return key |
||||
|
||||
# --------------------------------------------------------- |
||||
# |
||||
|
||||
# Первичное привестивие |
||||
async def Welcome(a_Message): |
||||
user_id = int(a_Message.from_user.id) |
||||
user_name = str(a_Message.from_user.username) |
||||
user_bd.AddUser(user_id, user_name) |
||||
log.Info(f'Пользователь {user_id} {user_name} авторизовался в боте') |
||||
await a_Message.answer(start_message, reply_markup=GetStartKeyboardButtons(), parse_mode='HTML') |
||||
|
||||
# Отображение профиля пользователя |
||||
async def ProfileOpen(a_Message): |
||||
user_id = str(a_Message.from_user.id) |
||||
user_info = user_bd.GetUserInfo(user_id) |
||||
msg = profile_message |
||||
if not user_info is None: |
||||
msg = msg.replace('@user_id', str(user_info[0])).replace('@user_name', str(user_info[1])) |
||||
await bot.send_message(user_id, msg, reply_markup=GetProfileKeyboardButtons()) |
||||
|
||||
# Возврат в основное меню |
||||
async def BackToStartMunu(a_Message : types.Message): |
||||
await Welcome(a_Message) |
||||
|
||||
def RegisterHandlers(dp : Dispatcher): |
||||
dp.register_message_handler(Welcome, commands=['start']) |
||||
dp.register_message_handler(ProfileOpen, text=user_profile_button_name) |
||||
dp.register_message_handler(BackToStartMunu, text=back_to_start_menu_button_name) |
||||
|
||||
|
@ -0,0 +1,29 @@
|
||||
#-*-coding utf-8-*- |
||||
# Общественное достояние 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru> |
||||
|
||||
from bot_sys import bot_bd |
||||
|
||||
import sqlite3 |
||||
|
||||
# Работа с базой данных пользователей |
||||
|
||||
# Добавление пользователя, если он уже есть, то игнорируем |
||||
def AddUser(a_UserID, a_UserName): |
||||
db = sqlite3.connect(bot_bd.GetBDFileName()) |
||||
cursor = db.cursor() |
||||
cursor.execute("INSERT OR IGNORE INTO users (user_id, userName) VALUES (?, ?);", (a_UserID, a_UserName)); |
||||
db.commit() |
||||
cursor.close() |
||||
db.close() |
||||
|
||||
# Добавление пользователя, если он уже есть, то игнорируем |
||||
def GetUserInfo(a_UserID): |
||||
db = sqlite3.connect(bot_bd.GetBDFileName()) |
||||
cursor = db.cursor() |
||||
user_info = cursor.execute('SELECT * FROM users WHERE user_id = ?', ([a_UserID])).fetchall() |
||||
cursor.close() |
||||
db.close() |
||||
if len(user_info) != 0: |
||||
return user_info[0] |
||||
return None |
||||
|
Loading…
Reference in new issue