5
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dnt.settings')
6
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
9
from telethon.sync import TelegramClient, events
10
from telegram.tg_bot.tg_bot import BotLogic
12
from authapp.models import AuthUser
13
from questions.operations import SettingRatingToQuestionByUser, UserLevelTooLow, NoUnratedQuestionsForUser
15
BOT_TOKEN = os.environ['BOT_TOKEN']
16
API_ID = os.environ['API_ID']
17
API_HASH = os.environ['API_HASH']
20
def work_with_chat(api_id: int, api_hash: str, bot_token: str, session_file='bot') -> None:
22
Основная функция-обработчик общения пользователей с ботом
25
bot = TelegramClient(os.path.join('sessions', session_file), api_id, api_hash).start(bot_token=bot_token)
27
@bot.on(events.NewMessage)
28
async def handler(event):
29
message = event.message
30
telegram_id = message.chat.id
32
bot_logic = BotLogic(bot=bot,
33
telegram_id=telegram_id,
34
telegram_username=message.chat.username)
36
if message.text == '/start':
38
AuthUser.objects.get(telegram_id=telegram_id)
40
await bot_logic.send_welcome_back()
42
except authapp.models.AuthUser.DoesNotExist:
44
await bot_logic.create_or_merge_account()
46
elif message.text == '/rate':
48
user = AuthUser.objects.get(telegram_id=telegram_id)
49
rating_process = SettingRatingToQuestionByUser(user)
50
await bot_logic.rate_questions(rating_process)
52
except UserLevelTooLow:
53
await bot.send_message(telegram_id, 'Твоего уровня недостаточно для оценки вопросов')
54
except NoUnratedQuestionsForUser:
55
await bot.send_message(telegram_id, 'Ты уже оценил все вопросы')
58
bot.run_until_disconnected()
61
if __name__ == "__main__":
64
work_with_chat(API_ID, API_HASH, BOT_TOKEN)
65
except Exception as e:
66
print(e.__class__.__name__, e)