-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram_cities_game.py
87 lines (71 loc) · 3.15 KB
/
telegram_cities_game.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
from aiogram import Bot, Dispatcher, executor, types
import wikipedia
import logging
from aiogram.utils.callback_data import CallbackData
import json
import random
API_TOKEN = 'API TOKEN' # API telegram token
logging.basicConfig(level=logging.INFO)
bot = Bot(token=API_TOKEN, parse_mode=types.ParseMode.HTML)
dp = Dispatcher(bot)
cb = CallbackData('fabcity', 'city_name')
wikipedia.set_lang("en")
@dp.message_handler(commands=['start'])
async def start(message: types.Message):
data[message.from_user.id] = {'bot_city': '',
'user_city': '',
'already_been': [],
}
await message.answer('Hi! My name is CityBot and you can play with me!\n'
'The rules are very simple: You say the city, '
'I say the city starting with the last letter, and then you do the same!\n\n'
'Say the name of any city!\n\n'
'To restart send /start')
@dp.message_handler(content_types=['text'])
async def get_user_city(message: types.Message):
user_id = message.from_user.id
if user_id in data:
data[message.from_user.id]['user_city'] = message.text
user_city = message.text
already_been = data[message.from_user.id]['already_been']
bot_city = data[message.from_user.id]['bot_city']
if user_city in cities_list:
if user_city not in already_been:
if bot_city == '' or user_city[0] == bot_city[-1].upper():
await answer_next_city(message, user_city, cities_list, already_been)
else:
await message.answer('This city has already been!')
else:
await message.answer("I don't think it's a city...")
else:
await start(message)
async def answer_next_city(message: types.Message, user, cities, already):
last_letter = user[-1]
possible_answers = list()
for city in cities:
if city not in already and last_letter.upper() == city[0]:
possible_answers.append(city)
answer = random.choice(possible_answers)
data[message.from_user.id]['bot_city'] = answer
keyboard = types.InlineKeyboardMarkup(row_width=1)
keyboard.add(types.InlineKeyboardButton(text='Find in Wiki', callback_data=cb.new(city_name=answer)))
await message.answer(answer, reply_markup=keyboard)
@dp.callback_query_handler(cb.filter())
async def callbacks(call: types.CallbackQuery, callback_data: dict):
city = callback_data['city_name']
await get_info_wiki(call.message, city)
async def get_info_wiki(message: types.Message, city):
try:
ny = wikipedia.page(city)
await message.answer(f'Here is info about {city}:\n\n'
f'{ny.summary}')
except Exception as e:
await message.answer(f'Ugh, name of the city is difficult to search! Try it yourself, please!')
def get_cities():
with open('cities.json', 'r') as read_file:
return json.load(read_file)
if __name__ == '__main__':
cities_list = get_cities()
cities_list.sort()
data = {}
executor.start_polling(dp, skip_updates=True)