-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
71 lines (51 loc) · 1.78 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import logging
import os
import openai
from aiogram import Bot, Dispatcher, executor, types
from dotenv import load_dotenv
from engine.game import CrimeGame
logging.basicConfig(level=logging.DEBUG)
load_dotenv()
telegram_bot_token = os.getenv("TELEGRAM_BOT_TOKEN")
openai_api_key = os.getenv("OPENAI_API_KEY")
bot = Bot(token=telegram_bot_token)
openai.api_key = openai_api_key
dp = Dispatcher(bot)
game = CrimeGame()
@dp.message_handler(commands=["start"])
async def start(message: types.Message):
intro_text = game.intro()
await message.bot.send_message(
message.chat.id, intro_text, parse_mode="Markdown"
)
scene = await game.new_story()
await _display_story(message, scene)
async def _display_story(message: types.Message, scene):
actions = scene["display"]["available_actions"]
keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
keyboard.add(*actions)
text = scene["display"]["text"]
current_scene = scene["display"]["taken_actions"]
total_scenes = scene["display"]["total_actions"]
percentage = round((current_scene / total_scenes) * 100)
await message.bot.send_message(
message.chat.id,
f"*Progress*: {percentage}%\n\n{text}",
parse_mode="Markdown",
reply_markup=keyboard,
)
await message.bot.send_photo(
message.chat.id, scene["display"]["image"], reply_markup=keyboard
)
@dp.message_handler(commands=["restart"])
async def reset(message: types.Message):
game.restart()
await start(message)
@dp.message_handler(content_types=types.ContentType.TEXT)
async def take_option(message: types.Message):
scene = await game.take_option(message.text)
await _display_story(message, scene)
def main():
executor.start_polling(dp)
if __name__ == "__main__":
main()