This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
340 lines (300 loc) · 13.5 KB
/
bot.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# coding: utf-8
import asyncio
import json
import re
from datetime import datetime
from urllib import parse
import aiohttp
import asyncio_redis
import telepot
import telepot.aio
from telepot.namedtuple import ReplyKeyboardMarkup, ReplyKeyboardRemove
from constants import GAME_CARD_TEMPLATE, NEWS_CARD_TEMPLATE, LANG, CC
from utils import SearchSuggestParser, cache_steam_response, group
class SteamBot(telepot.aio.Bot, telepot.helper.AnswererMixin):
def __init__(self, *args, config=None, **kwargs):
super(SteamBot, self).__init__(*args, **kwargs)
self._answerer = telepot.aio.helper.Answerer(self)
self.config = config
self.cache_time = self.config.get('cache_time', 10)
self.redis_conn = None
self.loop.create_task(self.initialize_redis())
self.routes = {
'/search': self.search_game,
'/app_': self.game_card_answer,
'/scr_': self.screenshots_answer,
'/news_': self.last_news_answer,
'/feedback': self.feedback_answer,
'/settings': self.settings_answer,
'/lang': self.set_lang,
'/cc': self.set_cc,
'/start': self.welcome_answer
}
async def initialize_redis(self):
self.redis_conn = await asyncio_redis.Pool.create(
host=self.config['redis']['ip'],
port=self.config['redis']['port'],
db=self.config['redis']['db'],
poolsize=5
)
@cache_steam_response
async def get_content_from_url(self, url, resp_format=None):
async with aiohttp.ClientSession(loop=self.loop) as client:
async with client.get(url) as resp:
if resp.status != 200:
return
if resp_format == 'text':
result = await resp.text()
elif resp_format == 'json':
result = await resp.json()
else:
result = await resp.content.read()
return result
async def get_search_results(self, term, settings):
search_url = u'https://store.steampowered.com/search/suggest?term={}&f=games&l={}&cc={}'.format(
parse.quote_plus(term),
settings.get('lang'),
settings.get('cc')
)
content = await self.get_content_from_url(search_url, resp_format='text')
parser = SearchSuggestParser()
parser.feed(content)
return parser.result
async def get_appdetails(self, appid, settings={}):
url = u'https://store.steampowered.com/api/appdetails/?appids={}&l={}&cc={}'.format(
appid,
settings.get('lang'),
settings.get('cc')
)
content = await self.get_content_from_url(url, resp_format='json')
return content[appid]['data'] if content else {}
async def get_news(self, appid, count=3):
url = u'https://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid={}&count={}&max_length=300&format=json'.format(
appid,
count
)
content = await self.get_content_from_url(url, resp_format='json')
return content['appnews']['newsitems'] if content else {}
@staticmethod
def get_command(msg):
if 'entities' in msg:
for entity in msg['entities']:
if entity['type'] == 'bot_command':
offset, length = entity['offset'], entity['length']
return msg['text'][offset:length], msg['text'][offset + length:].strip()
return None, None
@staticmethod
def get_games_message(entries):
msg_list = []
if len(entries) != 0:
for entry in entries:
msg = u"{cmd} {name} [steam]({href}) _{price}_".format(
name=entry['name'],
href=entry['href'],
price=entry['price'],
cmd=u'/app\_{}'.format(entry['appid'])
)
msg_list.append(msg)
return u'\n'.join(msg_list)
return u'Nothing found'
@staticmethod
def clean_html(html):
return re.sub('<[^<]+?>', '', html)
@staticmethod
def clean_markdown(text):
return text.replace('_', '\_').replace('*', '\*')
def get_game_card_message(self, appdetails):
return GAME_CARD_TEMPLATE.format(
appid=appdetails['steam_appid'],
name=appdetails['name'],
release_date=appdetails['release_date']['date'],
metacritic=u'\u2b50\ufe0f{} [metacritics]({})'.format(
appdetails['metacritic']['score'],
appdetails['metacritic']['url']
) if 'metacritic' in appdetails else '',
platforms=', '.join(
[x[0] for x in appdetails['platforms'].items() if x[1]]),
genres=', '.join(
[x['description'] for x in appdetails['genres']]) if 'genres' in appdetails else '',
publishers=', '.join(
appdetails['publishers']) if 'publishers' in appdetails else '',
price='{} {}'.format(appdetails['price_overview']['final'] / 100.0,
appdetails['price_overview']['currency']) if 'price_overview' in appdetails else '',
recommendations=appdetails['recommendations']['total'] if 'recommendations' in appdetails else '',
screenshotscount=len(
appdetails['screenshots']) if 'screenshots' in appdetails else '0',
about_the_game=self.clean_html(appdetails['about_the_game'])[:500]
)
async def on_callback_query(self, msg):
query_id, from_id, data = telepot.glance(msg, flavor='callback_query')
print('Callback query:', query_id, from_id, data)
self.route(from_id, data)
async def game_search_answer(self, term, chat_id):
user_info = await self.get_user(chat_id)
settings = user_info.get('settings')
msg = self.get_games_message(await self.get_search_results(term, settings))
await self.sendMessage(chat_id, msg, parse_mode='markdown', disable_web_page_preview=True)
async def game_card_answer(self, chat_id, command, args):
appid = command.replace('/app_', '').strip()
self.loop.create_task(self.sendChatAction(chat_id, 'typing'))
user_info = await self.get_user(chat_id)
settings = user_info.get('settings')
app_details = await self.get_appdetails(appid, settings)
await self.sendMessage(chat_id, self.get_game_card_message(app_details), parse_mode='markdown')
async def send_photo_from_url(self, url, photo_name, chat_id):
downloaded_file = await self.get_content_from_url(url)
await self.sendPhoto(chat_id, photo=(photo_name, downloaded_file))
async def screenshots_answer(self, chat_id, command, args):
appid = command.replace('/scr_', '').strip()
self.loop.create_task(self.sendChatAction(chat_id, 'upload_photo'))
app_details = await self.get_appdetails(appid)
for scr in app_details['screenshots']:
loop.create_task(self.send_photo_from_url(
scr['path_full'], 'scr-{}.jpg'.format(scr['id']), chat_id))
async def last_news_answer(self, chat_id, command, args):
appid = command.replace('/news_', '').strip()
self.loop.create_task(self.sendChatAction(chat_id, 'typing'))
news_items = await self.get_news(appid)
for item in news_items:
msg = NEWS_CARD_TEMPLATE.format(
title=item['title'],
url=item['url'],
pub_date=datetime.fromtimestamp(
int(item['date'])).strftime("%B %d, %Y"),
feedlabel=item['feedlabel'],
contents=self.clean_markdown(self.clean_html(item['contents'])).replace(
'\n', '').replace(' ', '')[:300],
author=item['author']
)
loop.create_task(self.sendMessage(
chat_id, msg, parse_mode='markdown'))
def get_user_key(self, user_id):
return 'user-{}'.format(user_id)
async def save_user_settings(self, user_id, new_settings):
key = self.get_user_key(user_id)
user = await self.get_user(user_id)
settings = user.get('settings', {})
settings.update(new_settings)
user['settings'] = settings
await self.redis_conn.set(key, json.dumps(user))
async def get_user(self, user_id):
return json.loads(await self.redis_conn.get(self.get_user_key(user_id)))
async def create_or_update_user(self, chat):
key = self.get_user_key(chat['id'])
user = await self.redis_conn.get(key)
if not user:
new_user = chat
default_settings = {
'lang': 'english',
'cc': 'US'
}
new_user_serialized = json.dumps(
{'info': new_user, 'settings': default_settings})
await self.redis_conn.set(key, new_user_serialized)
else:
user = json.loads(user)
if chat != user['info']:
user['info'] = chat
await self.redis_conn.set(key, json.dumps(user))
async def on_inline_query(self, msg):
async def compute_answer():
query_id, from_id, query_string = telepot.glance(
msg, flavor='inline_query')
print('inline query: {} from_id: {}'.format(query_string, from_id))
user_info = await self.get_user(from_id)
settings = user_info.get('settings')
results = await self.get_search_results(query_string, settings)
articles = []
for res in results:
articles.append({
'type': 'article',
'id': res['appid'],
'title': res['name'],
'message_text': u'{} {} {}'.format(
res['name'],
res['price'],
res['href']
),
# 'url': res['href'],
'description': res['price'],
'thumb_url': res['image']
})
return {'results': articles}
self._answerer.answer(msg, compute_answer)
async def on_chosen_inline_result(self, msg):
query_id, from_id, query_string = telepot.glance(
msg, flavor='chosen_inline_result')
print('Chosen Inline Result: {} {} from_id: {}'.format(
query_id, query_string, from_id))
await self.game_card_answer(query_id, from_id)
async def search_game(self, chat_id, command, args):
await self.sendChatAction(chat_id, 'typing')
await self.game_search_answer(args, chat_id)
async def set_lang(self, chat_id, command, args):
lang = args.strip() if args else None
if lang:
await self.save_user_settings(chat_id, {'lang': LANG.get(lang)})
await bot.sendMessage(chat_id, 'language saved', reply_markup=ReplyKeyboardRemove())
else:
markup = ReplyKeyboardMarkup(
keyboard=group(['/lang' + x for x in LANG.keys()], 2),
one_time_keyboard=True
)
await bot.sendMessage(chat_id, 'set language', reply_markup=markup)
async def set_cc(self, chat_id, command, args):
cc = args.strip() if args else None
if cc:
await self.save_user_settings(chat_id, {'cc': CC.get(cc)})
await bot.sendMessage(chat_id, 'region saved', reply_markup=ReplyKeyboardRemove())
else:
markup = ReplyKeyboardMarkup(
keyboard=group(['/cc' + x for x in CC.keys()], 3),
one_time_keyboard=True
)
await bot.sendMessage(chat_id, 'set region', reply_markup=markup)
async def feedback_answer(self, chat_id, command, args):
msg = args.replace('/feedback ', '').strip()
if msg:
await self.sendMessage(
self.config.get('admin_id'),
'feedback from: {}: {}'.format(chat_id, msg)
)
await self.sendMessage(chat_id, 'thank you for your feedback!')
else:
await self.sendMessage(chat_id, 'looks like your feedback is empty!')
async def settings_answer(self, chat_id, command, args):
await self.sendMessage(
chat_id,
"change region: /cc\n"
"change language: /lang\n"
)
async def welcome_answer(self, chat_id, command, args):
await self.sendMessage(
chat_id,
'Welcome! Just type / for view list of commands, also you can use this bot with inline mode.\n'
'For search a game just send message with game title'
)
def route(self, chat_id, command, args=None):
func = None
for cmd, fnc in self.routes.items():
if command.find(cmd) != -1:
func = fnc
break
if func:
self.loop.create_task(func(chat_id, command, args))
async def on_chat_message(self, msg):
content_type, chat_type, chat_id = telepot.glance(msg)
print(msg)
await self.create_or_update_user(msg.get('chat'))
command, args = self.get_command(msg)
if not command:
command, args = '/search', msg['text']
self.route(chat_id, command, args)
with open('conf/config.json') as f:
config = json.loads(f.read())
loop = asyncio.get_event_loop()
token = config.pop("telegram_token")
bot = SteamBot(token=token, config=config, loop=loop)
loop.create_task(bot.message_loop())
print('Listening ...')
loop.run_forever()