-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
87 lines (73 loc) · 2.81 KB
/
handlers.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
import logging
import telegram
from flask import request, redirect
import requests
from domain.hook_update import HookUpdate, HookObject
from main import app, bot
from models import User
from services import products, spots
from services import account as auth
from services.settings import settings
@app.route('/hook', methods=['POST'])
def hook():
update = telegram.Update.de_json(request.json, bot)
try:
user = User.get_by_id(update.message.chat_id)
if not user:
user = User(
id=update.message.chat_id,
username=update.message.chat.username,
first_name=update.message.chat.first_name,
last_name=update.message.chat.last_name,
type=update.message.chat.type,
).put()
logging.info(request.json)
for h in app.handler_registry:
if h.check_update(update) and (not h.state or user.state == h.state):
h.callback(update, user)
break
except Exception as ex:
return 'fail'
return 'ok'
# web hook for updating data for poster
@app.route('/webhook', methods=['POST'])
def poster_webhook():
data = request.json
hook_update = HookUpdate.deserialize(data)
token = auth.get_access_token(hook_update.account)
if hook_update.object == HookObject.PRODUCT:
products.update_by_hook(hook_update, token)
elif hook_update.object == HookObject.SPOT:
spots.update_by_hook(hook_update, token)
return 'ok'
@app.route('/connect', methods=['GET'])
def connect_new_poster_account():
auth_url = 'https://{}.joinposter.com/api/auth?application_id={}&redirect_uri={}&response_type=code'.format(
request.args.get('poster_url'),
settings.POSTER_APP_ID,
settings.POSTER_REDIRECT_URI
)
return redirect(auth_url)
@app.route('/welcome', methods=['GET'])
def welcome():
code = request.args.get('code')
account = request.args.get('account')
data = {
'application_id': settings.POSTER_APP_ID,
'application_secret': settings.POSTER_APP_SECRET,
'grant_type': 'authorization_code',
'redirect_uri': settings.POSTER_REDIRECT_URI,
'code': code
}
r = requests.post('https://{}.joinposter.com/api/auth/access_token'.format(account), data=data)
logging.debug(r.text)
logging.debug(r.content)
access_token = r.json().get('access_token')
auth.save_account(account, access_token)
return redirect('https://{}.joinposter.com/manage/applications/info/che-pozhevat'.format(account))
@app.route('/disconnect', methods=['GET'])
def disconnect():
account = request.args.get('account_url')
auth.delete_account(account)
logging.debug(request.json)
return redirect('https://{}.joinposter.com/manage/applications/info/che-pozhevat'.format(account))