-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
115 lines (84 loc) · 3.54 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import logging
import pyjokes
import time
from aiogram import Bot, Dispatcher, executor, types
API_TOKEN = '5882713162:AAFuJyyVERy_fIeJjyOaCwVFKOK6LyuY5Nk'
# Configure logging
logging.basicConfig(level=logging.INFO)
# Initialize bot and dispatcher
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
history = set()
category = 'neutral'
@dp.message_handler(commands=['start'])
async def msg_start(message: types.Message):
await message.answer("Welcome! Type /info for more information")
@dp.message_handler(commands=['info'])
async def msg_start(message: types.Message):
await message.answer(
"Hi! This bot is designed for giving unique(up to dictionary size ofc)"
" jokes for programmers and everyone related.\n"
"Jokes are also categorised, by default they are neutral"
"Type /help to see list of commands available")
@dp.message_handler(commands=['help'])
async def msg_help(message: types.Message):
await message.answer("List of commands:\n"
"/info - short info about bot"
"/start - basic start command\n"
"/help - shows list of commands\n"
"/joke - get free joke\n"
"/history - history of jokes (yes)\n"
"/reset - deletes history(auto-resets when necessary\n)"
"/category <NAME> - set category of jokes\n"
"/category_list - list of categories")
@dp.message_handler(commands=['category_list'])
async def msg_start(message: types.Message):
await message.answer("List of categories: chuck, neutral, all")
@dp.message_handler(commands=['reset'])
async def msg_start(message: types.Message):
history.clear()
await message.answer("Reset done")
@dp.message_handler(commands='category', regexp='chuck')
async def msg_start(message: types.Message):
global category
category = 'chuck'
# category.join('chuck')
await message.answer(f"Category set to {category}")
@dp.message_handler(commands='category', regexp='neutral')
async def msg_start(message: types.Message):
global category
category = 'neutral'
# category.join('chuck')
await message.answer(f"Category set to {category}")
@dp.message_handler(commands='category', regexp='all')
async def msg_start(message: types.Message):
global category
category = 'all'
# category.join('chuck')
await message.answer(f"Category set to {category}")
@dp.message_handler(commands='category')
async def msg_start(message: types.Message):
await message.answer("Wrong category name")
@dp.message_handler(commands=['joke'])
async def send_welcome(message: types.Message):
joke = pyjokes.get_joke('en', category)
reset = False
start_time = time.time()
while joke in history:
joke = pyjokes.get_joke('en', category)
if time.time() - start_time > 3:
reset = True
break
if reset:
await message.answer("Reset done, get your jokes again!")
history.clear()
history.add(joke)
await message.answer(joke)
@dp.message_handler(commands=None)
async def command_unknown(message: types.Message):
if message.text == "Why so serious":
await message.answer("Seems ur Joker fan, appreciate it")
else:
await message.answer("Unknown command, Type /help for list of commands")
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)