This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
70 lines (54 loc) · 1.87 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
# coding=utf-8
import webapp2
from webapp2_extras import json
import messages
from message import Message
__author__ = 'm_messiah'
CREDENTIALS = {}
SESSIONS = {}
class MainPage(webapp2.RequestHandler):
def get(self):
return self._show_error()
def _get_update(self):
if 'Content-Type' not in self.request.headers:
return {}
if 'application/json' not in self.request.headers['Content-Type']:
return {}
try:
return json.decode(self.request.body)
except Exception:
return {}
def _answer(self, response):
self.response.headers['Content-Type'] = 'application/json'
self.response.write(json.encode(response))
def _show_error(self):
self._answer({'result': "Info", "name": messages.DEFAULT_ANSWER})
def _inline_query(self, query):
return self._answer({
'method': 'answerInlineQuery',
'inline_query_id': query['id'],
'cache_time': 60,
'results': json.encode([{
'type': 'article',
'id': "1",
'title': "405",
'message_text': messages.INLINE_TEXT,
'description': messages.INLINE_DESCRIPTION,
}])
})
def _get_message(self, update):
if 'message' in update:
return Message(update['message'])
elif 'edited_message' in update:
return Message(update['edited_message'])
def post(self):
update = self._get_update()
if 'inline_query' in update:
return self._inline_query(update['inline_query'])
message = self._get_message(update)
if message:
return self._answer(message.handle() or {})
return self._show_error()
app = webapp2.WSGIApplication([('/', MainPage)])
if __name__ == '__main__': # pragma: no cover
app.run()